gpt4 book ai didi

python - 在 PyQt 中将 QstringList 转换为 Qstring

转载 作者:太空宇宙 更新时间:2023-11-04 00:52:47 26 4
gpt4 key购买 nike

我正在尝试使用 QFileDialog 获取文件名列表,并希望在 QLineEdit 中显示(在 Python 2.7 中)。

self.resLine = QLineEdit()
xxres_file = (QFileDialog.getOpenFileNames(self, 'Select File', '', '*.txt'))
self.resLine.setText(xxres_file)

它期望(如错误所说)一个 QString:

TypeError: QLineEdit.setText(QString): argument 1 has unexpected type 'QStringList'

谁能帮我把 QStringList 转换成 QString。

提前致谢

最佳答案

您想要的值是 QStringList 中的字符串,而不是列表本身

您可以使用 QStringList.join 方法将列表中的元素连接在一起,然后对其调用 split 以获得原生 python 列表

strlist = xxres_file.join(",") # this returns a string of all the elements in the QStringList separated by comma's

strlist = strlist.split(",") # you can optionally split the string to get all the elements in a python list

self.resLine.setText(strlist[0]) # your list contains only one string in this case

在 python 3 中,QStringListQString 分别映射到原生 python 列表和字符串。

关于python - 在 PyQt 中将 QstringList 转换为 Qstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36447394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com