Switching between frames without erasing contents of each frame

一世执手 提交于 2019-12-02 12:04:15

Instead of switching the centralwidget, because:

Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.

you also can't use instances, because they would get deleted after first use as well, you should use a QStackedWidget and set the centralwidget just once.

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(50, 50, 400, 450)

        ToolTab = UIToolTab( self )
        ToolTab.CPSBTN.clicked.connect( self.gotoP1 )
        Window = UIWindow( self )
        Window.ToolsBTN.clicked.connect( self.gotoP2 )
        self.stack = QStackedWidget(self)
        self.stack.addWidget(ToolTab)
        self.stack.addWidget( Window )
        self.setCentralWidget( self.stack )
        self.show()

    def gotoP2(self):
        self.setWindowTitle("Page1")
        self.stack.setCurrentIndex(0)

    def gotoP1(self):
        self.setWindowTitle("Page2")
        self.stack.setCurrentIndex( 1 )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!