QSignalTransition子类从未收到过自定义PyQt信号

时间:2019-03-13 14:57:33

标签: python python-3.x pyqt pyqt5 qstatemachine

尝试学习PyQt5 QStateMachine,并在为PySide编写的this example之后衍生自QSignalTransition。要使用PyQt,我只是更改了信号的定义以及在QSignalTransition初始化程序中引用该信号的方式。这是完整的代码:

from PyQt5.QtCore import QObject, pyqtSignal, QSignalTransition, QCoreApplication, QStateMachine, QState, QFinalState


class Factorial(QObject):
    xChanged = pyqtSignal(int)
    def __init__(self):
        super(Factorial, self).__init__()
        self.xval = -1
        self.facval = 1
    def getX(self):
        return self.xval
    def setX(self, x):
        if self.xval == x:
            return
        self.xval = x
        self.xChanged.emit(x)
    x = property(int, getX, setX)
    def getFact(self):
        return self.facval
    def setFact(self, fac):
        self.facval = fac
    fac = property(int, getFact, setFact)

class FactorialLoopTransition(QSignalTransition):
    def __init__(self, fact):
        super(FactorialLoopTransition, self).__init__(fact.xChanged[int])
        self.fact = fact
    def eventTest(self, e):
        if not super(FactorialLoopTransition, self).eventTest(e):
            return False
        return e.arguments()[0] > 1
    def onTransition(self, e):
        x = e.arguments()[0]
        fac = self.fact.fac
        self.fact.fac = x * fac
        self.fact.x = x - 1

class FactorialDoneTransition(QSignalTransition):
    def __init__(self, fact):
        super(FactorialDoneTransition, self).__init__(fact.xChanged[int])
        self.fact = fact
    def eventTest(self, e):
        if not super(FactorialDoneTransition, self).eventTest(e):
            return False
        return e.arguments()[0] <= 1
    def onTransition(self, e):
        print(self.fact.fac)

if __name__ == '__main__':
    import sys
    app = QCoreApplication(sys.argv)
    factorial = Factorial()
    machine = QStateMachine()

    compute = QState(machine)
    compute.assignProperty(factorial, 'fac', 1)
    compute.assignProperty(factorial, 'x', 6)
    compute.addTransition(FactorialLoopTransition(factorial))

    done = QFinalState(machine)
    doneTransition = FactorialDoneTransition(factorial)
    doneTransition.setTargetState(done)
    compute.addTransition(doneTransition)

    machine.setInitialState(compute)
    machine.finished.connect(app.quit)
    machine.start()
    sys.exit(app.exec_())

此状态机仅计算6的阶乘并打印。

在两个过渡中,我的问题是这段代码:

def eventTest(self, e):
    if not super(FactorialDoneTransition, self).eventTest(e):
        return False
    return e.arguments()[0] <= 1

在第一个也是唯一的呼叫上返回False。我认为这是因为传递的事件的类型不正确,并且超类返回False。有了这种行为,转换就永远不会执行,状态机将保持其初始状态。

有人可以解释出什么问题吗?是我从PySide转换为PyQt错了吗?

1 个答案:

答案 0 :(得分:1)

assignProperty()方法期望一个Q_PROPERTY,在PySide / PySide2等于Property(与python本身提供的属性不同)的情况下,在PyQt4 / PyQt5的情况下,您必须使用{{3} },这就是错误的原因,因此解决方案是将其替换为以下内容:

from PyQt5.QtCore import QObject, pyqtSignal, QSignalTransition, QCoreApplication, QStateMachine, QState, QFinalState, pyqtProperty


class Factorial(QObject):
    xChanged = pyqtSignal(int)
    def __init__(self):
        super(Factorial, self).__init__()
        self.xval = -1
        self.facval = 1

    def getX(self):
        return self.xval

    def setX(self, x):
        if self.xval == x:
            return
        self.xval = x
        self.xChanged.emit(x)

    x = pyqtProperty(int, getX, setX) # <---

    def getFact(self):
        return self.facval

    def setFact(self, fac):
        self.facval = fac

    fac = pyqtProperty(int, getFact, setFact) # <---

# ...