QFileDialog.getSaveFileName() error in Plugin code for QGIS

Try

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

instead of

self.dlg.lineEdit.setText(filename)

Hope this answers your question.


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.