sat0408
sat0408

Reputation: 73

Convert QstringList to Qstring in PyQt

I am trying to get a list of filenames using QFileDialog and wanted to be displayed in a QLineEdit (in Python 2.7).

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

It expects (as the error says) a QString:

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

Could someone help me with converting a QStringList into a QString.

Thanks in advance

Upvotes: 2

Views: 1533

Answers (2)

Brendan Abel
Brendan Abel

Reputation: 37569

Assuming you're using a fairly recent version of pyqt, you can also tell pyqt to use the newer api for qstrings

import sip
sip.setapi('QString', 2)

# Do pyqt imports afterwards
from PyQt4 import QtCore, QtGui

Then you just use the regular str and list methods.

Upvotes: 1

danidee
danidee

Reputation: 9634

The value you want is the string in the QStringList not the list itself

You can use the QStringList.join method to join the elements in the list together and then call split on it to get a native python list

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

In python 3 QStringList and QString are mapped to native python list and strings respectively.

Upvotes: 2

Related Questions