根据内容的变化从 QWizardPage 动态添加/删除 Finish [英] Dynamically add/remove Finish from QWizardPage based on change in its content

查看:93
本文介绍了根据内容的变化从 QWizardPage 动态添加/删除 Finish的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 许可证向导(使用 PyQt5)尝试学习如何创建非线性向导.但是我似乎被一个问题困住了.

I'm following the tutorial on a license wizard (using PyQt5) trying to learn how to create non-linear wizards. However I seem to be stuck on an issue.

我想要一个带有 QComboBox 的页面,其中所选项目确定包含组合框的当前 QWizardPage 是否是最终页面.

I want to have a page with a QComboBox where the selected item determines whether the current QWizardPage that contains the combobox is the final page or not.

这是该页面目前包含的内容:

Here is what the page contains so far:

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 的当前项目更改为校准文件 时,我都想跳过使页面最终化的向导的其余部分.在这种情况下,我想删除 Next 按钮.在所有其他情况下(目前只有两个:Image filesStream)我想让向导正常运行,而不是作为最后一页.

Whenever self.selection_sources's current item changes to Calibration file I would like to skip the rest of the wizard that is make the page final. In this case I want to remove the Next button. In all other cases (currently only two: Image files and Stream) I want to have the wizard act normally that is not as a final page.

我尝试实现自定义 isComplete(...) 但问题是它在 NextFinish>校准文件被选中.我可以忍受禁用 Next 按钮(而不是完全隐藏它),但禁用 Finish 对我来说基本上没有意义.我真的很惊讶 Next 按钮的存在.当到达最后一页时,它不应该完全消失吗?

I have tried implementing a custom isComplete(...) but the problem is that it disables both Next and Finish when Calibration file is selected. I can live with having a disabled Next button (instead of completely hiding it) but a disabled Finish basically doesn't make sense in my case. I'm actually surprised that the Next button is present. Isn't it supposed to go away completely when a final page has been reached?

任何想法如何解决这个问题?我想过遍历 QWizardPage 中的项目并手动禁用/隐藏 Next 按钮,但我希​​望有一种更简单、开箱即用的方法去做.在当前状态下,Finish 的动态插入正在工作,但是由于 Next 按钮,向导的转换没有正确设置.

Any ideas how to solve this problem? I thought about iterating through the items in the QWizardPage and manually disabling/hiding the Next button but I hope that there is an easier, out-of-the-box way to do that. In the current state the dynamic insertion of Finish is working however due to the Next button the wizard's transitions are not properly set.

推荐答案

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

In your code you use already QWizardPage.setFinalPage(True) to add a finish button to an intermediate page. Now the next button still exists. One way to remove it (not sure if this is the best way) is removing all following pages by calling QWizard.removePage() in conjunction with QWizard.nextId().

示例:

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().

See method end_wizard_after_page_two() in this example.

如果要反转效果,则必须反向执行所有操作(再次添加剩余页面并将FinalPage 设置为False).

If you want to reverse the effect you would have to do everything in reverse (adding the remaining pages again and setFinalPage to False).

这篇关于根据内容的变化从 QWizardPage 动态添加/删除 Finish的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆