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

夙愿已清 提交于 2019-11-26 06:08:17

问题


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!


回答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.



来源:https://stackoverflow.com/questions/47041271/is-there-a-difference-between-qfiledialog-strings-in-pyqt4-and-pyqt5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!