繁体   English   中英

如何使用 PyQt5 在第二个 window 中触发按钮的单击事件

[英]How to trigger a click event for a push button in second window using PyQt5

我有一个主对话框 window 如下所示在此处输入图像描述

单击确定按钮后,将打开第二个 window,如下所示在此处输入图像描述

我需要触发第二个window登录按钮frpm的点击事件。 下面是我的代码。 但我没有触发任何方法。

from .gisedify_support_dialog_login import Ui_Dialog
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'gisedify_support_dialog_base.ui'))
class GisedifySupportDialog(QtWidgets.QDialog, FORM_CLASS):
 def __init__(self, parent=None):
    """Constructor."""
    super(GisedifySupportDialog, self).__init__(parent)
    # Set up the user interface from Designer through FORM_CLASS.
    # After self.setupUi() you can access any designer object by doing
    # self.<objectname>, and you can use autoconnect slots - see
    # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
    # #widgets-and-dialogs-with-auto-connect
    self.setupUi(self)

  def open_login_dialog(self):
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.exec_()
    ui.login_button.clicked.connect(self.login)

  def login(self):
    print('success')

class Login_Dialog(QtWidgets.QDialog,Ui_Dialog):
 def __init__(self, parent=None):
    super(Login_Dialog, self).__init__(parent)

QDialog.exec_()将阻塞,直到用户关闭对话框,因此您需要在调用Dialog.exec_()之前设置任何信号槽连接。 关闭对话框时,如果接受了对话框,则返回 1,否则返回 0。 关闭对话框不会破坏它(除非您设置了一个标志来这样做),因此您可以检索在Dialog.exec_()返回后输入的数据。

因此,与其将插槽连接到主 window 中的对话框按钮按钮,不如将QDialog子类化,使用 Qt 设计器文件设置 ui,然后将button.clicked信号连接到QDialog.accept插槽。 然后在主小部件中,您可以像以前一样调用Dialog.exec_()并在之后检索信息,例如

from PyQt5 import QtWidgets, QtCore, QtGui


class Login_Dialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.setupUi(self)
        self.login_button.clicked.connect(self.accept)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        # setup ui as before

    def get_login(self):
        dialog = Login_Dialog(self)
        if dialog.exec_():
            # get activation key from dialog
            # (I'm assuming here that the line edit in your dialog is assigned to dialog.line_edit)  
            self.activation_key = dialog.line_edit.text()
            self.login()

    def login(self)
        print(f'The activation_key you entered is {self.activation_key}')

暂无
暂无

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

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