Is there a difference between QFileDialog strings in PyQt4 and PyQt5?

I have a block of code that opens a QFileDialog using Python3 and PyQt5:

from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog
import sys


class MCVE(QWidget):

    def __init__(self):
        super().__init__()
        self.initialize()

    def initialize(self):
        self.setWindowTitle('MCVE')
        self.setGeometry(50, 50, 400, 200)
        btn = QPushButton('Example', self)
        btn.clicked.connect(self.clicked)

        self.show()

    def clicked(self):
        filename = QFileDialog.getOpenFileName(
            self, "Open Template", "c:\\",
            "Templates (*.xml);;All Files (*.*)")

        print(filename)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MCVE()
    sys.exit(app.exec_())

In Python 2 using PyQt4 the print(filename) statement, after pressing the cancel button, outputs as an empty string. When I run the same code in Python 3 using PyQt5 I get:

('', '')

NOTE: The quotes are Single Quotes

Can someone explain what is going on? I couldn't find anything under the documentation between PyQt4 and PyQt5. I know that strings changed between Python 2 and Python 3, but I'm not sure those changes would cause an issue like this. Thanks!


Solution 1:

The getOpenFileName function in PyQt4 returns a string that is the name of the selected file, and if none is selected then it returns an empty string.

filename = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")

However in PyQt5 this returns a tuple of 2 elements where the first one is a string that has the same behavior as in PyQt4, and the second element is the filter used.

filename, filters = QFileDialog.getOpenFileName(self, "Open Template", "c:\\", "Templates (*.xml);;All Files (*.*)")

Note: The majority of documentation of PyQt5 is in Qt5, since in general the names of the methods, the inputs and the result are similar.