正确使用PyQt信号

2024-09-28 03:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

不久前,我在Qt中做了一些C++的工作,现在我正在使用PyQT。在

我有一个QStackedWidget的子类,里面有一个QWidget的子类。在QWidget中,我想单击一个按钮,该按钮将转到QStackedWidget的下一页。我的(简化)方法如下:

class Stacked(QtGui.QStackedWidget):
    def __init__(self, parent=None):
        QtGui.QStackedWidget.__init__(self, parent)
        self.widget1 = EventsPage()
        self.widget1.nextPage.connect(self.nextPage)
        self.widget2 = MyWidget()
        self.addWidget(self.widget1)
        self.addWidget(self.widget2)

    def nextPage(self):
        self.setCurrentIndex(self.currentIndex() + 1)


class EventsPage(QtGui.QWidget):
    nextPage = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.continueButton = QtGui.QPushButton('Continue')
        self.continueButton.clicked.connect(self.nextPage)

所以,基本上,我将continueButton clicked信号连接到EventsPage nextPage信号,然后将其堆栈连接到nextPage方法。我可以在Stacked and connectself.widget1.continueButton.clicked中深入研究EventsPage的内部,但这似乎完全违背了信号和插槽的用途。在

那么,这种方法有意义吗,还是有更好的方法呢?在


Tags: 方法self信号initdef子类parentqtgui
1条回答
网友
1楼 · 发布于 2024-09-28 03:21:33

不,这很有道理。把信号想象成人们从楼顶招手。他们不想穿过街道(所有的楼梯…),所以他们会观察其他建筑物上的其他人在做什么。这样,就没人关心大楼里发生了什么。在

相关问题 更多 >

    热门问题