我正在尝试使用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/

10-13 06:59