pyQt QWizard在完成按钮旁边更改以提前退出

时间:2015-07-12 22:15:06

标签: pyqt pyside wizard

如果用户输入了足够的数据,是否有办法在向导中途将下一个按钮更改为完成按钮? 下面的示例使用HaveFinishButtonOnEarlyPages设置 - 但是,如果用户选择单选按钮,我想要做的是用完成按钮替换下一个按钮。这可能吗?

from PyQt4 import QtGui
from PyQt4.QtGui import QWizard
import sys

def createPage1():
    page = QtGui.QWizardPage()
    page.setTitle("Page 1")
    page.setSubTitle("Enter some data, if you don't want to enter any more data select finish early.")
    essentialLabel = QtGui.QLabel("Essential data:")
    essentialLineEdit = QtGui.QLineEdit()
    finishEarlyRB = QtGui.QRadioButton("Select to finish wizard early")
    layout = QtGui.QGridLayout()
    layout.addWidget(essentialLabel, 0, 0)
    layout.addWidget(essentialLineEdit, 0, 1)
    layout.addWidget(finishEarlyRB, 1, 0)
    page.setLayout(layout)
    return page

def createPage2():
    page = QtGui.QWizardPage()
    page.setTitle("Page 2")
    page.setSubTitle("Enter some more data.")
    nonEssentialLabel = QtGui.QLabel("Non essential data:")
    nonEssentialLineEdit = QtGui.QLineEdit()
    layout = QtGui.QGridLayout()
    layout.addWidget(nonEssentialLabel, 0, 0)
    layout.addWidget(nonEssentialLineEdit, 0, 1)
    page.setLayout(layout)
    return page

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    wizard = QtGui.QWizard()
    wizard.setOption(QWizard.HaveFinishButtonOnEarlyPages, on=True)
    wizard.addPage(createPage1())
    wizard.addPage(createPage2())
    wizard.show()
    sys.exit(wizard.exec_())

1 个答案:

答案 0 :(得分:1)

感谢figs,我重新编写了这个,以便用户有一个下一个和完成按钮。如果满足以下页面上的某些条件,则启用完成按钮。

from PyQt4 import QtGui
from PyQt4.QtGui import QWizard, QWizardPage
import sys

class TrialWizard(QWizard):
    def __init__(self, parent = None):
        super(QWizard, self).__init__(parent)
        self.setOption(QWizard.HaveFinishButtonOnEarlyPages, on=True)
        self.addPage(TrialWizardPage1(self))
        self.addPage(TrialWizardPage2(self))


class TrialWizardPage1(QWizardPage):
    def __init__(self, parent):
        super(TrialWizardPage1, self).__init__(parent)
        self.setupUi()
        self.connectSlots()

    def setupUi(self):
        self.setTitle("Page 1")
        self.setSubTitle("Enter some data, if you don't want to enter any more data select finish early.")
        essentialLabel = QtGui.QLabel("Essential data:")
        self.essentialLineEdit = QtGui.QLineEdit()
        self.finishEarlyRB = QtGui.QRadioButton("Select to finish wizard early")
        layout = QtGui.QGridLayout()
        layout.addWidget(essentialLabel, 0, 0)
        layout.addWidget(self.essentialLineEdit, 0, 1)
        layout.addWidget(self.finishEarlyRB, 1, 0)
        self.setLayout(layout)

    def isComplete(self):
        if len(self.essentialLineEdit.text()) > 0:
            return True
        else:
            return False

    def connectSlots(self):
        self.finishEarlyRB.clicked.connect(self.finishEarlyRBClicked)

    def finishEarlyRBClicked(self):
        if self.finishEarlyRB.isChecked():
            self.setFinalPage(True)
            self.completeChanged.emit()

class TrialWizardPage2(QWizardPage):
    def __init__(self, parent):
        super(TrialWizardPage2, self).__init__(parent)
        self.setupUi()

    def setupUi(self):
        self.setTitle("Page 2")
        self.setSubTitle("Enter some more data.")
        nonEssentialLabel = QtGui.QLabel("Non essential data:")
        nonEssentialLineEdit = QtGui.QLineEdit()
        layout = QtGui.QGridLayout()
        layout.addWidget(nonEssentialLabel, 0, 0)
        layout.addWidget(nonEssentialLineEdit, 0, 1)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    wizard = TrialWizard()
    wizard.show()
    sys.exit(app.exec_())