繁体   English   中英

PyQt5(MacOS)如何在不使用exec的情况下从子类化QFileDialog返回文件名和路径?

[英]PyQt5, (MacOS) how to return filenames and paths from subclassed QFileDialog without using exec?

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

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

如果我通过if diag.exec():等待用户确认if diag.exec():我可以获取所选文件,但工作表窗口完全放错了位置(即未从应有的位置出现)

在此处输入图片说明

如果我改用if diag.open():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_())

试试吧:

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_())

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM