如何使用PyQt5在第二个窗口中触发按钮的点击事件

问题描述 投票:0回答:1

我有一个主对话框窗口,如下所示enter image description here

单击确定按钮后,将打开第二个窗口,如下所示enter image description here

我需要在第二个窗口中触发登录按钮frpm的click事件。下面是我的代码。但我不会触发任何方法。

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)
python user-interface pyqt5 qgis
1个回答
0
投票

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

因此,您可以将QDialog子类化,使用Qt Designer文件设置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}')

© www.soinside.com 2019 - 2024. All rights reserved.