简体   繁体   English

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

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

In Qt Designer 5, how do I create a signal to open a QFileDialog ? 在Qt Designer 5中,如何创建打开QFileDialog的信号? I'm using Python and PyQt. 我正在使用Python和PyQt。 I've tried creating signals with "Edit Signals/Slots" and I can select the button I want as sender, but I can't select an arbitrary function as the receiver, only existing widgets are available in the list. 我尝试使用“编辑信号/插槽”创建信号,并且可以选择要作为发送器的按钮,但是我不能选择任意功能作为接收器,列表中仅现有的小部件可用。

In order to create custom Signal/Slots to later use in your Python application you need to add them doing a right click on the widget and clicking on Change signals/slots... , as shown in the next image: 为了创建自定义信号/插槽以供以后在Python应用程序中使用,您需要添加它们,然后在小部件上单击鼠标右键,然后单击“ 更改信号/插槽...” ,如下图所示:

You'll need to add the desired slots , like shown here with the mybutton_clicked() function: 您需要添加所需的广告 ,如下面的mybutton_clicked()函数所示:

Thus far, the slots is created and it is possible to use it in the Signals & Slots Editor tab. 至此,已经创建了插槽,并且可以在“ 信号和插槽编辑器”选项卡中使用它。 Once in this tab, clicking in + button, the Receiver slot is present if the previous step is done right, as shown here: 在此选项卡中,单击+按钮,如果正确完成了上一步,则会显示Receiver插槽,如下所示:

Lastly, integrate the requested QFileDialog into the button press method, it is as easy as this : 最后,将请求的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_()

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

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