PySide2 QWizard非线性页面

时间:2017-12-13 19:26:51

标签: python pyqt5 pyside2 qwizard

我正在尝试将我的代码从PyQt5更改为PySide2。我的大多数脚本只需要将导入从PyQt5更改为PySide2,它的工作原理相同。但是我试图将Qt文档中的licensewizard示例从PyQt5重新编写为PySide2。在PyQt5中它完美地工作,但是,在PySide2中,我得到退出代码139的错误(被信号11:SIGSEGV中断)。像类向导示例的线性向导的PySide2 qwizard代码工作正常。有谁知道为什么它失败了非线性向导以及为什么我得到那个错误?

由于

以下代码适用于PyQt5。用PySide2替换PyQt5我收到错误

使用退出代码132完成处理(由信号4中断:SIGILL)

class IntroductionPage(QtWidgets.QWizardPage):
    def __init__(self):
        super(IntroductionPage, self).__init__()

        self.setTitle("Introduction")
        self.label = QtWidgets.QLabel("set this up")
        self.label.setWordWrap(True)
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.class1

class ClassesPage1(QtWidgets.QWizardPage):
    def __init__(self):
        super(ClassesPage1, self).__init__()
        self.setTitle("Choices")
        self.setSubTitle("Choose 1")

        self.radButton1 = QtWidgets.QRadioButton('1A')
        self.radButton2 = QtWidgets.QRadioButton('1B')
        self.radButton3 = QtWidgets.QRadioButton('1C')

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.class_selected1 = QtWidgets.QLineEdit()
        self.setLayout(self.layout)

    def nextId(self):
        if self.radButton2.isChecked():

            return Wizard.class2
        elif self.radButton3.isChecked():

            return Wizard.class2
        else:
            return Wizard.conclusion

class ClassesPage2(QtWidgets.QWizardPage):
    def __init__(self):
        super(ClassesPage2, self).__init__()
        #self.page = QWizardPage()
        self.setTitle("Classes for grade 2")
        self.setSubTitle("Please select a Class")

        self.radButton1 = QtWidgets.QRadioButton('2A')
        self.radButton2 = QtWidgets.QRadioButton('2B')
        self.radButton3 = QtWidgets.QRadioButton('2C')

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.class_selected2 = QtWidgets.QLineEdit()

        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.conclusion

class ClassesPage3(QtWidgets.QWizardPage):
    def __init__(self):
        super(ClassesPage3, self).__init__()
        self.setTitle("Classes for grade 3")
        self.setSubTitle("Please select a Class")

        self.radButton1 = QtWidgets.QRadioButton('3A')
        self.radButton2 = QtWidgets.QRadioButton('3B')
        self.radButton3 = QtWidgets.QRadioButton('3C')

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.class_selected3 = QtWidgets.QLineEdit()

        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.conclusion

class ConclusionPage(QtWidgets.QWizardPage):
    def __init__(self):
        super(ConclusionPage, self).__init__()

        self.layout = QtWidgets.QVBoxLayout()

        self.label = QtWidgets.QLabel(self)
        self.label2 = QtWidgets.QLabel(self)
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.label2)
        self.setLayout(self.layout)

class Wizard(QtWidgets.QWizard):
    num_of_pages = 5
    (intro, class1, class2, class3, conclusion) = range(num_of_pages)

    def __init__(self):
        super(Wizard, self).__init__()
        self.setPage(self.intro, IntroductionPage())

        self.setPage(self.class1, ClassesPage1())
        self.setPage(self.class2, ClassesPage2())
        self.setPage(self.class3, ClassesPage3())
        self.setPage(self.conclusion, ConclusionPage())
        self.setStartId(self.intro)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    wizard = Wizard()

    wizard.setWindowTitle("Set Up")
    wizard.setWizardStyle(QtWidgets.QWizard.MacStyle)
    wizard.show()

    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

似乎与PyQt5不同,PySide2要求您传递父级,在您的情况下,解决方案是更改类的构造函数:

class Some_Page(QtWidgets.QWizardPage):
    def __init__(self):
        super(Some_Page, self).__init__()

为:

class Some_Page(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(Some_Page, self).__init__(*args, **kwargs)

当我们添加页面时,我们将父级传递给他:

class Wizard(QtWidgets.QWizard):
    num_of_pages = 5
    (intro, class1, class2, class3, conclusion) = range(num_of_pages)

    def __init__(self, *args, **kwargs):
        super(Wizard, self).__init__(*args, **kwargs)
        self.setPage(self.intro, IntroductionPage(self))
        self.setPage(self.class1, ClassesPage1(self))
        self.setPage(self.class2, ClassesPage2(self))
        self.setPage(self.class3, ClassesPage3(self))
        self.setPage(self.conclusion, ConclusionPage(self))
        self.setStartId(self.intro)

完整代码:

from PySide2 import QtWidgets
import sys

class IntroductionPage(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(IntroductionPage, self).__init__(*args, **kwargs)

        self.setTitle("Introduction")
        self.label = QtWidgets.QLabel("Welcome to the One Inc™'s Teacher Interactive Accessment tool. Follow the prompts to set up your Mark Sheet")
        self.label.setWordWrap(True)
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.label)
        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.class1

class ClassesPage1(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(ClassesPage1, self).__init__(*args, **kwargs)
        self.setTitle("Choices")
        self.setSubTitle("Choose 1")

        self.radButton1 = QtWidgets.QRadioButton('1A')
        self.radButton2 = QtWidgets.QRadioButton('1B')
        self.radButton3 = QtWidgets.QRadioButton('1C')

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.setLayout(self.layout)

    def nextId(self):
        if self.radButton2.isChecked():

            return Wizard.class2
        elif self.radButton3.isChecked():

            return Wizard.class2
        else:
            return Wizard.conclusion

class ClassesPage2(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(ClassesPage2, self).__init__(*args, **kwargs)
        #self.page = QWizardPage()
        self.setTitle("Classes for grade 2")
        self.setSubTitle("Please select a Class")

        self.radButton1 = QtWidgets.QRadioButton('2A')
        self.radButton2 = QtWidgets.QRadioButton('2B')
        self.radButton3 = QtWidgets.QRadioButton('2C')

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.class_selected2 = QtWidgets.QLineEdit()

        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.conclusion

class ClassesPage3(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(ClassesPage3, self).__init__(*args, **kwargs)
        self.setTitle("Classes for grade 3")
        self.setSubTitle("Please select a Class")

        self.radButton1 = QtWidgets.QRadioButton('3A', self)
        self.radButton2 = QtWidgets.QRadioButton('3B', self)
        self.radButton3 = QtWidgets.QRadioButton('3C', self)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.radButton1)
        self.layout.addWidget(self.radButton2)
        self.layout.addWidget(self.radButton3)
        self.class_selected3 = QtWidgets.QLineEdit()

        self.setLayout(self.layout)

    def nextId(self):
        return Wizard.conclusion

class ConclusionPage(QtWidgets.QWizardPage):
    def __init__(self, *args, **kwargs):
        super(ConclusionPage, self).__init__(*args, **kwargs)

        self.layout = QtWidgets.QVBoxLayout()
        self.label = QtWidgets.QLabel(self)
        self.label2 = QtWidgets.QLabel(self)
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.label2)
        self.setLayout(self.layout)

class Wizard(QtWidgets.QWizard):
    num_of_pages = 5
    (intro, class1, class2, class3, conclusion) = range(num_of_pages)

    def __init__(self, *args, **kwargs):
        super(Wizard, self).__init__(*args, **kwargs)
        self.setPage(self.intro, IntroductionPage(self))
        self.setPage(self.class1, ClassesPage1(self))
        self.setPage(self.class2, ClassesPage2(self))
        self.setPage(self.class3, ClassesPage3(self))
        self.setPage(self.conclusion, ConclusionPage(self))
        self.setStartId(self.intro)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    wizard = Wizard()

    wizard.setWindowTitle("Set Up")
    wizard.setWizardStyle(QtWidgets.QWizard.MacStyle)
    wizard.show()

    sys.exit(app.exec_())