我目前正在尝试使用python和PyQt4(而不是Qt Designer)创建程序.我创建了一个登录类(QDialog)和一个Homepage类(QMainWindow).但是,因为我的程序将包含大量页面(通过程序导航将很大)我想知道如何在QMainWindow中切换布局,而不是不断创建新窗口并关闭旧窗口.例如,我将MainWindow('HomePage')布局设置为登录后的默认屏幕,然后在MainWindow中有一个子类,允许我导航到用户设置(或任何其他页面).而不是创建一个新窗口并关闭MainWindow,有没有办法让我将MainWindow布局交换到用户设置布局?(道歉,如果这没有意义,我对PyQt不熟悉).示例代码如下所示(V.Basic代码)
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow):
#Constructor
def __init__(self):
super(MainWindow, self).__init__() #call super class constructor
button1 = QPushButton("User Settings", self)
button1.clicked.connect(UserSelection)
button1.resize(50,50)
button1.move(350,50)
self.show()
class UserSelection(?):
...
def main():
app = QApplication(sys.argv) #Create new application
Main = MainWindow()
sys.exit(app.exec_()) #Monitor application for events
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
ivi*_*ica 10
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_()
Run Code Online (Sandbox Code Playgroud)