简体   繁体   中英

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

I have a main dialog window as shown below在此处输入图像描述

Once the OK button is clicked, second window will open as shown below在此处输入图像描述

I need to trigger the click event of login button frpm the second window. Below is my code. but i doesnt trigger any method.

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_() will block until the dialog is closed by the user so you would need to set up any signal-slot connections before you call Dialog.exec_() . When a dialog is closed, it returns 1 when de dialog was accepted, and 0 if not. Closing the dialog does not detroy it (unless you set a flag to do so), so you can retrieve the data that was entered after Dialog.exec_() returns.

So, instead of connecting a slot to the dialog button buttonin the main window, you could instead subclass QDialog , setup the ui using your Qt Designer files, and connect the button.clicked signal to the QDialog.accept slot. Then in the main widget you can call Dialog.exec_() as before and retrieve the information afterwards, eg

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}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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