如何使QStackedWidget中的“完成”按钮

时间:2019-04-03 05:31:26

标签: python-3.x pyqt5 qstackedwidget

我试图在QStackedWidget中创建“完成”按钮。

在“ checkButtons”功能中,我检查当前页面索引并设置单击事件和文本。我试图通过类名检查它,但是它也不起作用。

这是一个代码:

import sys

from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QDialog, QComboBox, QStackedWidget, QWidget,
            QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStyle)


class Main(QDialog):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        # Main window setup
        self.setWindowTitle("Stacked widget example")
        self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogNewFolder))
        self.setMinimumSize(400, 400)
        self.setMaximumSize(640, 480)

        self.rootVBox = QVBoxLayout()
        self.rootHBox = QHBoxLayout()
        self.rootHBox.addStretch()
        self.rootVBox.addStretch()

        self.pages = [FirstPage, SecondPage]
        self.stacked = QStackedWidget(self)
        for i in self.pages: self.stacked.addWidget(i(self))

        self.pageState = True
        self.buttonNext = QPushButton("Next")
        self.buttonNext.clicked.connect(self.buttonNextConnect)

        self.buttonBack = QPushButton("Back")
        self.buttonBack.clicked.connect(self.buttonBackConnect)

        self.rootHBox.addWidget(self.buttonBack)
        self.rootHBox.addWidget(self.buttonNext)

        self.rootVBox.addLayout(self.rootHBox)
        self.setLayout(self.rootVBox)

    def checkButtons(self):
        print(self.stacked.currentIndex())

        # I tried to check self.stacked.currentIndex() but it didn't work too
        # if self.stacked.currentWidget().__class__ == self.pages[-1]:
        if self.stacked.currentIndex() == len(self.pages) - 1:
            self.buttonNext.setText("Finish")
            self.buttonNext.clicked.connect(self.close)

        elif self.stacked.currentIndex() < len(self.pages) - 1:
            self.buttonNext.setText("Next")
            self.buttonNext.clicked.connect(self.buttonNextConnect)

    def buttonNextConnect(self):
        self.stacked.setCurrentIndex(self.stacked.currentIndex() + 1)
        self.checkButtons()

    def buttonBackConnect(self):
        self.stacked.setCurrentIndex(self.stacked.currentIndex() - 1)
        self.checkButtons()

    def finish(self):
        self.close()


class FirstPage(QWidget):
    def __init__(self, parent=None):
        super(FirstPage, self).__init__(parent)
        label = QLabel("First page")
        rootVBox = QVBoxLayout()
        rootHBox = QHBoxLayout()

        rootHBox.addWidget(label)
        rootVBox.addLayout(rootHBox)
        self.setLayout(rootVBox)


class SecondPage(QWidget):
    def __init__(self, parent=None):
        super(SecondPage, self).__init__(parent)
        label = QLabel("Second page")
        rootVBox = QVBoxLayout()
        rootHBox = QHBoxLayout()

        rootHBox.addWidget(label)
        rootVBox.addLayout(rootHBox)
        self.setLayout(rootVBox)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Main()
    main.show()

    sys.exit(app.exec_())

如果尝试按“ next”,“ back”然后再按“ next”,则程序将关闭。那么,我该如何解决?我应该为每个小部件制作控制按钮吗?

1 个答案:

答案 0 :(得分:2)

您必须使用QStackedWidget的currentChanged信号来知道您所在的页面,从而更改文本,但是在buttonNextConnect插槽中,如果要切换到新页面,则应检查是否已在最后一页上然后调用完成,如果您不切换到另一页

{{1}}

另一种选择是使用QWizard和QWizardPage:

{{1}}