【问题标题】:How to trigger a click event for a push button in second window using PyQt5如何使用 PyQt5 在第二个窗口中触发按钮的单击事件
【发布时间】:2020-05-19 09:38:04
【问题描述】:

我有一个如下所示的主对话窗口

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

我需要在第二个窗口触发登录按钮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)

【问题讨论】:

    标签: python user-interface pyqt5 qgis


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多