首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据QWizardPage内容的更改动态添加/删除完成

根据QWizardPage内容的更改动态添加/删除完成
EN

Stack Overflow用户
提问于 2016-06-12 22:08:43
回答 1查看 1.9K关注 0票数 3

我正在学习关于许可证向导的教程(使用PyQt5),试图学习如何创建非线性向导。然而,我似乎被困在一个问题上。

我希望有一个带有QComboBox的页面,其中所选的项确定包含组合框的当前QWizardPage是否是最后一个页面。

以下是到目前为止该页所包含的内容:

代码语言:javascript
复制
class CalibrationPageSource(QWizardPage):
    def __init__(self, parent):
        super(CalibrationPageSource, self).__init__(parent)
        self.setTitle('Calibration Wizard')
        self.setSubTitle('Select the source for the calibration')

        layout = QVBoxLayout()
        label = QLabel('''
            <ul>
                <li><b>From calibration file</b>: browse and select an existing YAML calibration file that contains the camera matrix and distortion coefficients (for example from a previous calibration)</li>
                <li><b>From image files</b>: browse and select one or more image files with the calibration pattern visible inside each</li>
                <li><b>From stream</b> - if the calibration node is connected to an active <b><i>Device node</i></b> you can use its image stream to interactively calibrate your device</li>
            </ul>
        ''')
        label.setWordWrap(True)
        layout.addWidget(label)

        layout_sources = QHBoxLayout()
        label_sources = QLabel('Source:')
        self.selection_sources = QComboBox()
        self.selection_sources.addItem('Calibration file')
        self.selection_sources.addItem('Image files')
        self.selection_sources.addItem('Stream')
        self.selection_sources.currentIndexChanged['QString'].connect(self.source_changed)
        self.selection_sources.setCurrentIndex(1)
        layout_sources.addWidget(label_sources)
        layout_sources.addWidget(self.selection_sources)
        layout.addLayout(layout_sources)

        self.setLayout(layout)

    @pyqtSlot(str)
    def source_changed(self, source):
        if source == 'Calibration file':
            self.setFinalPage(True)
            # TODO Add file dialog
        else:
            self.setFinalPage(False)
            # TODO Remove file dialog (if present)

每当self.selection_sources的当前项更改为Calibration file时,我希望跳过向导的其余部分,使页面成为最终页面。在本例中,我希望删除Next按钮。在所有其他情况下(目前只有两种:Image filesStream),我希望向导正常运行,而不是作为最后一页。

我尝试过实现一个自定义isComplete(...),但问题是当选择Calibration file时,它会同时禁用Calibration fileFinish。我可以忍受有一个禁用的Next按钮(而不是完全隐藏它),但是在我的情况下,禁用Finish基本上是没有意义的。我对Next按钮的出现感到惊讶。到了最后一页的时候,它不应该完全消失吗?

有什么办法解决这个问题吗?我考虑过迭代QWizardPage中的项,并手动禁用/隐藏Next按钮,但我希望有一种更简单的、开箱即用的方法。在当前状态下,Finish的动态插入工作正常,但是由于Next按钮,向导的转换没有正确设置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-14 09:47:20

在您的代码中,您已经使用QWizardPage.setFinalPage(True)向中间页添加了一个finish按钮。现在,next按钮仍然存在。删除它的一种方法(不确定这是否是最好的方法)是通过调用QWizard.removePage()QWizard.nextId()来删除所有下面的页面。

示例:

代码语言:javascript
复制
from PyQt5.QtWidgets import *

def end_wizard_after_page_two():
    # add finish button to wizard
    p2.setFinalPage(True)
    # remove all over buttons
    while True:
        id = w.nextId()
        if id == -1:
            break
        w.removePage(id)

app = QApplication([])

# page 1
p1 = QWizardPage()
p1.setTitle('Page 1')

# page 2
p2 = QWizardPage()
p2.setTitle('Page 2')

b = QPushButton('No further pages')
b.clicked.connect(end_wizard_after_page_two)
l = QVBoxLayout(p2)
l.addWidget(b)

# page 3
p3 = QWizardPage()
p3.setTitle('Page 3')

# wizard
w = QWizard()
w.addPage(p1)
w.addPage(p2)
w.addPage(p3)
w.show()

app.exec_()

请参阅本例中的方法end_wizard_after_page_two()

如果您想要逆转效果,您必须做一切反向(再次添加剩余的页面和setFinalPage到False)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37779526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档