1

我正在尝试将我的代码从 PyQt5 更改为 PySide2。我的大部分脚本只需将导入从 PyQt5 更改为 PySide2,它的工作原理是一样的。但是,我尝试将 Qt 文档中的许可向导示例从 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_())
4

1 回答 1

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_())
于 2017-12-14T07:36:55.177 回答