在PyQt中将QstringList转换为Qstring

时间:2016-04-06 09:46:05

标签: python pyqt qstring

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

提前致谢

2 个答案:

答案 0 :(得分:2)

您想要的值是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列表和字符串。

答案 1 :(得分:1)

假设您使用的是相当新版本的pyqt,您还可以告诉pyqt使用较新的api进行qstrings

["402", "tyson@ledner.name"]

然后,您只需使用常规import sip sip.setapi('QString', 2) # Do pyqt imports afterwards from PyQt4 import QtCore, QtGui str方法。