如何在 PyQt 中的两个窗口之间进行通信或切换? [英] How to communicate or switch between two windows in PyQt?

查看:85
本文介绍了如何在 PyQt 中的两个窗口之间进行通信或切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 和 Qt 开发应用程序.

我使用 Qt 设计了 ​​2 个主窗口,即..QMainWindow(不是 QWidget 或 QDialog).

就这样吧.

1.LoginWindow -- LoginUI(Qt)

2.StuffWindow --- StuffUI

<块引用>

  1. 首先我应该显示登录窗口.

  2. 然后我应该将用户名传递给 StaffWindow(管理东西所需的用户名)

  3. 应显示员工窗口并关闭登录窗口..

我怎样才能做到这一点..?帮帮我..

解决方案

我同意 jdi 提出的大部分观点,但我更喜欢稍微不同的方法.

  • LoginWindow 应该是一个以 MODAL 启动的 QDialog.
  • 检查exec_()的返回值(即accept/reject)以进行登录或取消/退出.
  • 检查LoginWindow
  • 内的登录信息
  • 如果登录成功,使用提供的参数启动MainWindow

在看到 jdi 的答案之前,我开始编写一个简单的示例.我不妨把它放在这里.

导入系统从 PyQt4 导入 QtGui、QtCore类登录对话框(QtGui.QDialog):def __init__(self, parent=None):super(LoginDialog, self).__init__(parent)self.username = QtGui.QLineEdit()self.password = QtGui.QLineEdit()loginLayout = QtGui.QFormLayout()loginLayout.addRow("用户名", self.username)loginLayout.addRow("密码", self.password)self.buttons = QtGui.QDialogBu​​ttonBox(QtGui.QDialogBu​​ttonBox.Ok | QtGui.QDialogBu​​ttonBox.Cancel)self.buttons.accepted.connect(self.check)self.buttons.rejected.connect(self.reject)布局 = QtGui.QVBoxLayout()layout.addLayout(登录布局)layout.addWidget(self.buttons)self.setLayout(布局)定义检查(自我):if str(self.password.text()) == "12345": # 进行实际登录检查自我接受()别的:pass # 或通知用户用户名/密码错误类 MainWindow(QtGui.QMainWindow):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.label = QtGui.QLabel()self.setCentralWidget(self.label)def setUsername(self, username):# 用用户名做任何你想做的事self.username = 用户名self.label.setText("输入的用户名:%s" % self.username)如果 __name__ == "__main__":app = QtGui.QApplication(sys.argv)登录 = 登录对话框()如果不是 login.exec_(): # 'reject': 用户按下了 'Cancel',所以退出sys.exit(-1)# '接受':继续主 = 主窗口()main.setUsername(login.username.text()) # 获取用户名,并将其提供给主窗口main.show()sys.exit(app.exec_())

I am developing an application using python and Qt.

I have designed 2 Main windows ie..QMainWindow (Not QWidget or QDialog) using Qt.

Let it be.

1.LoginWindow -- LoginUI(Qt)

2.StuffWindow --- StuffUI

  1. First i should display Login Window.

  2. Then i should pass the username to StaffWindow (username needed for managing stuffs)

  3. StaffWindow should be shown and LoginWindow Should be Closed..

How can i achieve this..? Help me..

解决方案

I do agree most of the points jdi raised, but I prefer a slightly different approach.

  • LoginWindow should be a QDialog started as MODAL.
  • Check the return of exec_() (i.e. accept/reject) for login or cancel/quit.
  • Check the login inside the LoginWindow
  • If successful login, launch MainWindow with parameters supplied

I started coding a simple example before seeing jdi's answer. I might as well put it here.

import sys
from PyQt4 import QtGui, QtCore

class LoginDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(LoginDialog, self).__init__(parent)

        self.username = QtGui.QLineEdit()
        self.password = QtGui.QLineEdit()
        loginLayout = QtGui.QFormLayout()
        loginLayout.addRow("Username", self.username)
        loginLayout.addRow("Password", self.password)

        self.buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel)
        self.buttons.accepted.connect(self.check)
        self.buttons.rejected.connect(self.reject)

        layout = QtGui.QVBoxLayout()
        layout.addLayout(loginLayout)
        layout.addWidget(self.buttons)
        self.setLayout(layout)

    def check(self):
        if str(self.password.text()) == "12345": # do actual login check
            self.accept()
        else:
            pass # or inform the user about bad username/password


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.label = QtGui.QLabel()
        self.setCentralWidget(self.label)

    def setUsername(self, username):
        # do whatever you want with the username
        self.username = username
        self.label.setText("Username entered: %s" % self.username)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    login = LoginDialog()
    if not login.exec_(): # 'reject': user pressed 'Cancel', so quit
        sys.exit(-1)      

    # 'accept': continue
    main = MainWindow()
    main.setUsername(login.username.text()) # get the username, and supply it to main window
    main.show()

    sys.exit(app.exec_())

这篇关于如何在 PyQt 中的两个窗口之间进行通信或切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆