替换 MainWindow 中的 CentralWidget [英] Replace CentralWidget in MainWindow

查看:140
本文介绍了替换 MainWindow 中的 CentralWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 PySide 有点陌生.我有一个主窗口对象,一次显示一个小部件.我一直在尝试更改 QMainWindow 类的中央小部件,以便在按下按钮时替换窗口中的可见小部件.问题是按下的按钮是在 Widget 类中,而不是在主窗口类中.

说...

class App(QtGui.QMainWindow):def __init__(self):super(App, self).__init__()self.initUI()定义 initUI(self):self.statusBar().showMessage('Listo.') #状态栏self.login_screen = LoginScreen()self.logged_in_screen = LoggedInScreen()self.setCentralWidget(self.login_screen)self.setGeometry(300, 300, 450, 600) #窗口大小self.setWindowTitle('PyTransactio - Client') #窗口标题self.setWindowIcon(QtGui.QIcon('icon.png')) #App Icon自我展示()

按下的按钮位于 login_screen 实例中.单击按钮时调用的方法位于 LoginScreen 类中:

def login(self):"""将登录数据发送到服务器以便登录"""#过程self.setParent(无)

将父小部件设置为 None 将从主窗口中删除小部件 (login_screen).当 loginButton(在 login_screen 内)时,我应该怎么做才能获得另一个小部件(例如 logged_in_screen)作为主窗口的中央小部件小部件)被按下?

也许登录方法应该在主窗口类中?如果是这样,我如何将 login_screen 中按下的按钮与主窗口的方法连接起来?

解决方案

您可以使用 QStackedWidget 作为中央小部件并向其添加登录屏幕和登录"屏幕.

示例用法:

from PyQt4 import QtCore, QtGui类 MainWindow(QtGui.QMainWindow):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.central_widget = QtGui.QStackedWidget()self.setCentralWidget(self.central_widget)login_widget = LoginWidget(self)login_widget.button.clicked.connect(self.login)self.central_widget.addWidget(login_widget)定义登录(自己):logging_in_widget = LoggedWidget(self)self.central_widget.addWidget(logged_in_widget)self.central_widget.setCurrentWidget(logged_in_widget)类 LoginWidget(QtGui.QWidget):def __init__(self, parent=None):super(LoginWidget, self).__init__(parent)布局 = QtGui.QHBoxLayout()self.button = QtGui.QPushButton('登录')layout.addWidget(self.button)self.setLayout(布局)# 你可能想要在这里做 self.button.click.connect(self.parent().login)类 LoggedWidget(QtGui.QWidget):def __init__(self, parent=None):super(LoggedWidget, self).__init__(parent)布局 = QtGui.QHBoxLayout()self.label = QtGui.QLabel('已登录!')layout.addWidget(self.label)self.setLayout(布局)如果 __name__ == '__main__':app = QtGui.QApplication([])窗口 = 主窗口()window.show()app.exec_()

如果您不想使用此小部件,那么我认为您每次更改中央小部件时都必须调用 QMainWindow.setCentralWidget.

至于 login 方法应该在哪里,这取决于.也许你可以为你的主窗口定义一个简单的界面来添加/删除/显示特定的中央小部件,并从 LoginScreenlogin 方法调用它.通过这种方式,LoginScreen 类不必知道实现细节,例如中央小部件是否实际上是一个 QStackedWidget 或者这件事是以其他方式完成的.>

I'm kinda new to PySide.I have a main window object which shows one widget at a time. I've been trying to change the central widget of the QMainWindow class in order to replace the visible Widget in the window when pressing a button. The problem is that the button pressed is in the Widget class, not in the main window class.

say...

class App(QtGui.QMainWindow):

    def __init__(self):
        super(App, self).__init__()

        self.initUI()

    def initUI(self):

        self.statusBar().showMessage('Listo.') #Status Bar
        self.login_screen = LoginScreen()
        self.logged_in_screen = LoggedInScreen()

        self.setCentralWidget(self.login_screen)

        self.setGeometry(300, 300, 450, 600) #Window Size
        self.setWindowTitle('PyTransactio - Client') #Window Title
        self.setWindowIcon(QtGui.QIcon('icon.png')) #App Icon
        self.show()

The pressed button is in the login_screen instance. The method called when the button is clicked is inside the LoginScreen class:

def login(self):
        """ Send login data to the server in order to log in """

        #Process

        self.setParent(None)

Setting the parent widget to None removes the widget (login_screen) from the main window. What should I do in order to get another widget (e.g. logged_in_screen) as the central widget of the main window when the loginButton (inside the login_screen widget) is pressed?

Maybe the login method should be inside the main window class? If so, how can I connect the buttons pressed in login_screen with the main window's method?

解决方案

You may use a QStackedWidget as central widget and add both the log-in screen and "logged-in" screen to it.

An example usage:

from PyQt4 import QtCore, QtGui


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        login_widget = LoginWidget(self)
        login_widget.button.clicked.connect(self.login)
        self.central_widget.addWidget(login_widget)
    def login(self):
        logged_in_widget = LoggedWidget(self)
        self.central_widget.addWidget(logged_in_widget)
        self.central_widget.setCurrentWidget(logged_in_widget)


class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        layout.addWidget(self.button)
        self.setLayout(layout)
        # you might want to do self.button.click.connect(self.parent().login) here


class LoggedWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoggedWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.label = QtGui.QLabel('logged in!')
        layout.addWidget(self.label)
        self.setLayout(layout)



if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

If you do not want to use this widget, then I think you'll have to call QMainWindow.setCentralWidget every time you change the central widget.

As to where the login method should be, it depends. Probably you could define a simple interface for your mainwindow to add/remove/show specific central widgets, and call it from the login method of LoginScreen. In this way the LoginScreen class does not have to know about implementation details such as if the central widget is actually a QStackedWidget or this thing is done in an other way.

这篇关于替换 MainWindow 中的 CentralWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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