【问题标题】:PyQt5, (MacOS) how to return filenames and paths from subclassed QFileDialog without using exec?PyQt5,(MacOS)如何在不使用 exec 的情况下从子类 QFileDialog 返回文件名和路径?
【发布时间】:2018-11-07 01:48:53
【问题描述】:

我不知道这是一个错误还是我没有正确使用该功能。

我想制作一个 MacOS 样式表窗口,并且我将 QFileDialog 子类化以设置 WindowModality 和 Parent。

如果我用if diag.exec(): 等待用户确认,我可以获得选定的文件,但是,工作表窗口完全放错了位置(即没有出现在它应该出现的位置)

如果我改为使用 if diag.open():,则工作表会按原样显示,但我似乎没有返回任何文件名或路径。

是我做错了什么,还是碰巧坏了?

示例代码:

from PyQt5.Qt import *
import sys
import time

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        self.centralwidget = QWidget(MainWindow)
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setText("CLICK ME")
        MainWindow.setCentralWidget(self.centralwidget)
        self.pushButton.clicked.connect(self.test)


    def test(self):
        diag = OpenSheet()
        if diag.exec(): # Replace with diag.open() to prevent weird bug, but doesn't return any filenames now
            fileNames = diag.selectedFiles()
            print(fileNames)

class OpenSheet(QFileDialog):
    def __init__(self):
        super().__init__()
        self.setWindowModality(True)
        self.setParent(mainwindow)
        self.setFileMode(self.ExistingFiles)
        self.setAcceptMode(QFileDialog.AcceptOpen)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(mainwindow)
    mainwindow.show()

    sys.exit(app.exec_())

【问题讨论】:

    标签: python macos qt pyqt pyqt5


    【解决方案1】:

    试试看:

    import sys
    import time
    from PyQt5.Qt import *
    
    class Ui_MainWindow(object):
    
        def setupUi(self, MainWindow):
            self.centralwidget = QWidget(MainWindow)
            self.pushButton    = QPushButton(self.centralwidget)
            self.pushButton.setText("CLICK ME")
            MainWindow.setCentralWidget(self.centralwidget)
            self.pushButton.clicked.connect(self.test)
    
        def test(self):
            diag = OpenSheet()
            #if diag.exec(): # Replace with diag.open() to prevent weird bug, but doesn't return any filenames now
            #    fileNames = diag.selectedFiles()
            #    print("\n", fileNames)
            options = diag.Options()
            options |= diag.DontUseNativeDialog
            files, _ = diag.getOpenFileNames(None, "diag.getOpenFileNames()", "",
                                            "All Files (*);;Python Files (*.py)", options=options)
            if files:
                print("Selected files: ", files)        
    
    
    class OpenSheet(QFileDialog):
        def __init__(self):
            super().__init__()
            self.setWindowModality(True)
            self.setParent(mainwindow)
            self.setFileMode(self.ExistingFiles)
            self.setAcceptMode(QFileDialog.AcceptOpen)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mainwindow = QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(mainwindow)
        mainwindow.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 返回一个非常非本地的对话框,这是一种截然相反的:(
    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 2018-04-18
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多