7

I'm working with Plugins Builder to create a simple plugin with QGIS3 "SaveAttributes", I'm following the official tutorial on QGIS Tutorials.

I'm installed Python 3 and Python 2 is already installed with ArcGIS, I have installed also the PyQt5 and Qt Creator. (I give details because I think that the error was not provided by the code)

The error I got:

TypeError: setText(self, str): argument 1 has unexpected type 'tuple' Traceback (most recent call last): File "C:/Users/REDA DRISSI/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\reda\module.py", line 190, in select_output_file self.dlg.lineEdit.setText(filename) TypeError: setText(self, str): argument 1 has unexpected type 'tuple'

For this code:

def select_output_file(self):
    filename = QFileDialog.getSaveFileName(self.dlg, "Select output file ","", '*.txt')
    self.dlg.lineEdit.setText(filename)

I tried to use str() function :

self.dlg.lineEdit.setText(str(filename))

Gives this Error:

OSError: [Errno 22] Invalid argument: "('C:/Users/REDA DRISSI/Desktop/DATA/finaldd/reda.txt', '*.txt')"

I think the problem is related to the default version of Python used by windows and QVariant, but I don't know how to resolve the problem.

2 Answers 2

8

Try

self.dlg.lineEdit.setText(filename[0])

instead of

self.dlg.lineEdit.setText(filename)
4
  • Thanks for the answer. Can you elaborate on why this works better than the original code? A link to the relevant docs and an explanation of the return value of getSaveFileName would be really good. Apr 30, 2018 at 10:20
  • for my test case it stored ('E:/test.txt', '*.txt') in filename but what you wanted was probably 'E:/test.txt' which could be accessed by filename[0]
    – Asad Abbas
    Apr 30, 2018 at 10:44
  • if this answers your question please close this question by accepting this answer
    – Asad Abbas
    Apr 30, 2018 at 10:46
  • Thanks a lot for your help,i tried to change in the resources.py PyQt5 instead of PyQt4, i used the documentation of the PyQt5 to solve the problem. Apr 30, 2018 at 14:24
6

QGIS 2 and QGIS 3 use PyQt4 and PyQt5, respectively. In PyQt4, QFileDialog.getSaveFileName() method returns filename string like "c:/path/to/file.txt". In PyQt5, that method returns a tuple contains file path and filter string like ("c:/path/to/file.txt", "*.txt"). So to get filename, you should use filename[0] in setText() method:

self.dlg.lineEdit.setText(filename[0])

Or, because getSaveFileName() method returns a tuple with two elements, you can use related line as @Matthias states in comments:

def select_output_file(self):
    filename, filter_string = QFileDialog.getSaveFileName(self.dlg, "Select output file ","", '*.txt')
    self.dlg.lineEdit.setText(filename) # without [0]

Strangely, Qt Documentation doesn't state that.

3
  • The PyQt signature differs from the Qt signature for this method, that's the reason for not stating that. What's not accurate here is the PyQt5 documentation for QFileDialog. Apr 30, 2018 at 11:15
  • I usually use Qt Documentation and I was not aware of PyQt Docs. But I noticed that PyQt Docs is redirecting into Qt Docs. According to this statement, getSaveFileName() are not supported in PyQt5, but it works in QGIS 3. I'm confused. Apr 30, 2018 at 11:39
  • 1
    I don't like the PyQt5 documentation very much, so I also always refer to the Qt documentation which is 90% accurate, but still cannot be blamed for not being accurate on derived products ;) I think the statement you linked to basically said that the PyQt5 getSaveFileName() method was already available as getSaveFileNameAndFilter() in PyQt4. Sidenote, I'd probably fix this by using filename, filter_string = QFileDialog.getSaveFileName. Apr 30, 2018 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.