我有一个从QMainWindow派生的主Window,它可能显示不同的Widget,具体取决于手头的任务。

我在下面创建了一个简化的示例:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        '''
        Constructor
        '''
        QMainWindow.__init__(self, parent)
        self.central_widget = QStackedWidget()
        self.setCentralWidget(self.central_widget)
        self.start_screen = Start(self)
        self.second_screen = Second(self)
        self.central_widget.addWidget(self.start_screen)
        self.central_widget.addWidget(self.second_screen)
        self.central_widget.setCurrentWidget(self.start_screen)



class Start(QWidget):

    def __init__(self, parent=None):
        super(Start, self).__init__(parent)
        layout = QHBoxLayout()
        button = QPushButton(text=QString('Push me!'))
        layout.addWidget(button)
        self.setLayout(layout)
        self.connect(button, SIGNAL("clicked()"), self.change_widget)

    def change_widget(self):
        self.parent().setCurrentWidget(
            self.parent().parent().second_screen)


class Second(QWidget):

    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        layout = QHBoxLayout()
        button = QPushButton(text=QString('Back to Start!'))
        layout.addWidget(button)
        self.setLayout(layout)
        self.connect(button, SIGNAL("clicked()"), self.change_widget)

    def change_widget(self):
        self.parent().setCurrentWidget(
            self.parent().parent().start_screen)

app = QApplication(sys.argv)
myWindow = MainWindow(None)
myWindow.show()
app.exec_()


这会在两个不同的中央窗口小部件之间切换,但是我发现用parent()函数进行处理很乏味,我想知道是否有更好的方法来组织窗口小部件。

setCurrentWidget方法可能始终可以通过一个parent()语句访问,但是如果由于某种原因层次深度发生了变化,是否有比parent().parent()更好的方法来访问QMainWindow?还是我会在每次单击按钮时重新创建窗口小部件(我假设,如果这是一个具有数据输入功能的窗口小部件,那么这些数据将会丢失,这很烦人)。

感谢您提出的任何改进建议。

最佳答案

您应该使用Signals。这允许任何父级层次结构,子级窗口小部件不必知道如何使用它们或以什么顺序显示。主窗口小部件可以处理所有这些。

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        '''
        Constructor
        '''
        QMainWindow.__init__(self, parent)
        self.central_widget = QStackedWidget()
        self.setCentralWidget(self.central_widget)
        self.start_screen = Start(self)
        self.second_screen = Second(self)
        self.central_widget.addWidget(self.start_screen)
        self.central_widget.addWidget(self.second_screen)
        self.central_widget.setCurrentWidget(self.start_screen)

        self.start_screen.clicked.connect(lambda: self.central_widget.setCurrentWidget(self.second_screen))
        self.second_screen.clicked.connect(lambda: self.central_widget.setCurrentWidget(self.start_screen))



class Start(QWidget):

    clicked = pyqtSignal()

    def __init__(self, parent=None):
        super(Start, self).__init__(parent)
        layout = QHBoxLayout()
        button = QPushButton(text=QString('Push me!'))
        layout.addWidget(button)
        self.setLayout(layout)
        button.clicked.connect(self.clicked.emit)


class Second(QWidget):

    clicked = pyqtSignal()

    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        layout = QHBoxLayout()
        button = QPushButton(text=QString('Back to Start!'))
        layout.addWidget(button)
        self.setLayout(layout)
        button.clicked.connect(self.clicked.emit)


app = QApplication(sys.argv)
myWindow = MainWindow(None)
myWindow.show()
app.exec_()

10-08 00:50