QSignalTransition 子类从未收到自定义 PyQt 信号
Custom PyQt signal never received by QSignalTransition subclass
尝试学习 PyQt5 QStateMachine
,使用从 QSignalTransition
派生的转换 this example 为 PySide 编写。为了使用 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
returns False
在第一次也是唯一一次通话中。我假设这是因为传递的事件不是正确的类型,并且超类 returns False
。对于这种行为,永远不会执行转换,并且状态机保持在其初始状态。
谁能解释一下这是什么问题?是我从 PySide 到 PyQt 的转换错了吗?
assignProperty()
method expects a Q_PROPERTY that in the case of PySide/PySide2 equals Property (which is different from the property provided by python natively) and in the case of PyQt4/PyQt5 you must use pyqtProperty,这是错误的原因,因此解决方案是替换为以下内容:
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) # <---
# ...
尝试学习 PyQt5 QStateMachine
,使用从 QSignalTransition
派生的转换 this example 为 PySide 编写。为了使用 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
returns False
在第一次也是唯一一次通话中。我假设这是因为传递的事件不是正确的类型,并且超类 returns False
。对于这种行为,永远不会执行转换,并且状态机保持在其初始状态。
谁能解释一下这是什么问题?是我从 PySide 到 PyQt 的转换错了吗?
assignProperty()
method expects a Q_PROPERTY that in the case of PySide/PySide2 equals Property (which is different from the property provided by python natively) and in the case of PyQt4/PyQt5 you must use pyqtProperty,这是错误的原因,因此解决方案是替换为以下内容:
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) # <---
# ...