在不擦除每一帧内容的情况下在帧之间切换 [英] Switching between frames without erasing contents of each frame
问题描述
我一直在使用下面的 setCentralWidget
方法(此处简化)编写 GUI 以在帧之间进行转换 - 但直到很远才意识到它在移动到下一个窗口时擦除了每个窗口(我需要避免).我的实际代码有输入框,当用户返回到该框架进行修改等时,这些输入框需要保持在完成状态.
I've been writing a GUI using the below setCentralWidget
method (simplified here) for transitioning between frames - but didn't realize until pretty far in that it erased each window upon moving to the next (which I need to avoid). My actual code has entry boxes that need to be kept in their completed state for when the user goes back to that frame to revise, etc.
我想在尽可能少地改变我的层次结构的情况下重写 - 最有可能使用 QStackedWidget
或类似的东西.我在网上找到的唯一例子不是写在我的程序需要的类层次结构中(每个页面在它自己的类中).有没有人在下面的示例文本中看到一个简单/优雅的解决方案?
I'd like to rewrite with as little change to my hierarchy as possible - most likely using QStackedWidget
or something similar. The only examples I've found online are not written within the class hierarchy (each page within its own class) which my program needs. Does anyone see a simple / elegant solution within this sample text below?
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
self.ToolsBTN = QPushButton('go P1', self)
self.ToolsBTN.move(50, 350)
class UIToolTab(QWidget):
def __init__(self, parent=None):
super(UIToolTab, self).__init__(parent)
self.CPSBTN = QPushButton("go P2", self)
self.CPSBTN.move(100, 350)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(50, 50, 400, 450)
self.setFixedSize(400, 450)
self.gotoP2()
def gotoP2(self):
self.ToolTab = UIToolTab(self)
self.setWindowTitle("Page1")
self.setCentralWidget(self.ToolTab)
self.ToolTab.CPSBTN.clicked.connect(self.gotoP1)
self.show()
def gotoP1(self):
self.Window = UIWindow(self)
self.setWindowTitle("Page2")
self.setCentralWidget(self.Window)
self.Window.ToolsBTN.clicked.connect(self.gotoP2)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
推荐答案
而不是切换 centralwidget,因为:
Instead of switching the centralwidget, because:
注意: QMainWindow 拥有小部件指针的所有权并在适当的时候将其删除.
Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
你也不能使用实例,因为它们在第一次使用后也会被删除,你应该使用 QStackedWidget
并且只设置一次 centralwidget.
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 )
这篇关于在不擦除每一帧内容的情况下在帧之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!