如何创建信号以在 Qt Designer 中打开 QFileDialog?

How do I create a signal to open a QFileDialog in Qt Designer?

在 Qt Designer 5 中,如何创建打开 QFileDialog 的信号?我正在使用 Python 和 PyQt。我已经尝试使用 "Edit Signals/Slots" 创建信号并且我可以 select 我想要的按钮作为发送者,但我不能 select 任意函数作为接收者,只有现有的小部件可用列表。

为了创建 custom Signal/Slots 以便稍后在您的 Python 应用程序中使用,您需要添加它们,右键单击小部件并单击在 上更改 signals/slots...,如下图所示:

您需要添加所需的 slots,如此处所示 mybutton_clicked() 函数:

到目前为止,插槽已创建,可以在 信号和插槽编辑器 选项卡中使用它。进入此选项卡后,单击 + 按钮,如果正确完成上一步,就会出现 Receiver 插槽,如下所示:

最后,将请求的QFileDialog集成到按钮按下方法中,就这么简单:

from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5 import uic
import sys


form_class = uic.loadUiType("mainWindow.ui")[0]  # Load the UI

class MyWindowClass(QMainWindow, form_class):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)

    def mybutton_clicked(self):
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*)", options=options)
        if fileName:
            print(fileName)

app = QApplication(sys.argv)
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()