QStateMachine 程序转换(即信号)
QStateMachine procedural transition (i.e. Signal)
我需要能够从不是由用户交互(即由进程完成)生成的信号(或类似的东西)更改 QStateMachine 的状态。这个例子重定向了一个用户生成的信号来演示这个想法,但还没有起作用。在这里,按钮信号触发了我想用于转换的其他信号。此处的状态是 B1 <-> B2
,其中 Bi -> Bj
在按下按钮 bi
时在重定向信号上。
QStateMachine 文档似乎只涵盖基于按钮直接信号的转换,我无法推断出使用任何信号(即我创建的信号)的更一般情况。
import sys
from PySide2.QtCore import Qt, QStateMachine, QState, Signal as QSignal
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.w = QWidget()
self.setCentralWidget(self.w)
l1 = QVBoxLayout()
self.b1 = QPushButton("B1", self.w)
self.b1.pressed.connect(self.b1Pressed)
l1.addWidget(self.b1)
self.b2 = QPushButton("B2", self.w)
self.b2.pressed.connect(self.b2Pressed)
l1.addWidget(self.b2)
self.w .setLayout(l1)
self.setupStateMachine()
self.show()
def b1Pressed(self):
print("b1 pressed")
self._b1Pressed.emit()
def b2Pressed(self):
print("b2 pressed")
self._b2Pressed.emit()
def setupStateMachine(self):
self._b1Pressed = QSignal()
self._b2Pressed = QSignal()
self.sm = QStateMachine()
self.B1 = QState()
self.B1.assignProperty(self.b1, "enabled", True)
self.B1.assignProperty(self.b2, "enabled", False)
self.sm.addState(self.B1)
self.B2 = QState()
self.B2.assignProperty(self.b1, "enabled", False)
self.B2.assignProperty(self.b2, "enabled", True)
self.sm.addState(self.B2)
# The point of it all:
# change states from a signal (or program generated event)
self.B1.addTransition(self._b1Pressed, self.B2)
self.B2.addTransition(self._b2Pressed, self.B1)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
您的代码存在以下问题:
- 信号未声明为 class 的属性,但在方法的同一级别。
- 您还没有建立初始状态。
- 您还没有启动 QStateMachine。
class MainWindow(QMainWindow):
<b>_b1Pressed = QSignal()
_b2Pressed = QSignal()</b>
def __init__(self):
QMainWindow.__init__(self)
self.w = QWidget()
self.setCentralWidget(self.w)
l1 = QVBoxLayout()
self.b1 = QPushButton("B1", self.w)
self.b1.pressed.connect(self.b1Pressed)
l1.addWidget(self.b1)
self.b2 = QPushButton("B2", self.w)
self.b2.pressed.connect(self.b2Pressed)
l1.addWidget(self.b2)
self.w.setLayout(l1)
self.setupStateMachine()
self.show()
def b1Pressed(self):
print("b1 pressed")
self._b1Pressed.emit()
def b2Pressed(self):
print("b2 pressed")
self._b2Pressed.emit()
def setupStateMachine(self):
self.sm = QStateMachine()
self.B1 = QState()
self.B1.assignProperty(self.b1, "enabled", True)
self.B1.assignProperty(self.b2, "enabled", False)
self.sm.addState(self.B1)
self.B2 = QState()
self.B2.assignProperty(self.b1, "enabled", False)
self.B2.assignProperty(self.b2, "enabled", True)
self.sm.addState(self.B2)
# The point of it all:
# change states from a signal (or program generated event)
self.B1.addTransition(self._b1Pressed, self.B2)
self.B2.addTransition(self._b2Pressed, self.B1)
<b>self.sm.setInitialState(self.B1)
self.sm.start()</b>
我需要能够从不是由用户交互(即由进程完成)生成的信号(或类似的东西)更改 QStateMachine 的状态。这个例子重定向了一个用户生成的信号来演示这个想法,但还没有起作用。在这里,按钮信号触发了我想用于转换的其他信号。此处的状态是 B1 <-> B2
,其中 Bi -> Bj
在按下按钮 bi
时在重定向信号上。
QStateMachine 文档似乎只涵盖基于按钮直接信号的转换,我无法推断出使用任何信号(即我创建的信号)的更一般情况。
import sys
from PySide2.QtCore import Qt, QStateMachine, QState, Signal as QSignal
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.w = QWidget()
self.setCentralWidget(self.w)
l1 = QVBoxLayout()
self.b1 = QPushButton("B1", self.w)
self.b1.pressed.connect(self.b1Pressed)
l1.addWidget(self.b1)
self.b2 = QPushButton("B2", self.w)
self.b2.pressed.connect(self.b2Pressed)
l1.addWidget(self.b2)
self.w .setLayout(l1)
self.setupStateMachine()
self.show()
def b1Pressed(self):
print("b1 pressed")
self._b1Pressed.emit()
def b2Pressed(self):
print("b2 pressed")
self._b2Pressed.emit()
def setupStateMachine(self):
self._b1Pressed = QSignal()
self._b2Pressed = QSignal()
self.sm = QStateMachine()
self.B1 = QState()
self.B1.assignProperty(self.b1, "enabled", True)
self.B1.assignProperty(self.b2, "enabled", False)
self.sm.addState(self.B1)
self.B2 = QState()
self.B2.assignProperty(self.b1, "enabled", False)
self.B2.assignProperty(self.b2, "enabled", True)
self.sm.addState(self.B2)
# The point of it all:
# change states from a signal (or program generated event)
self.B1.addTransition(self._b1Pressed, self.B2)
self.B2.addTransition(self._b2Pressed, self.B1)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
您的代码存在以下问题:
- 信号未声明为 class 的属性,但在方法的同一级别。
- 您还没有建立初始状态。
- 您还没有启动 QStateMachine。
class MainWindow(QMainWindow):
<b>_b1Pressed = QSignal()
_b2Pressed = QSignal()</b>
def __init__(self):
QMainWindow.__init__(self)
self.w = QWidget()
self.setCentralWidget(self.w)
l1 = QVBoxLayout()
self.b1 = QPushButton("B1", self.w)
self.b1.pressed.connect(self.b1Pressed)
l1.addWidget(self.b1)
self.b2 = QPushButton("B2", self.w)
self.b2.pressed.connect(self.b2Pressed)
l1.addWidget(self.b2)
self.w.setLayout(l1)
self.setupStateMachine()
self.show()
def b1Pressed(self):
print("b1 pressed")
self._b1Pressed.emit()
def b2Pressed(self):
print("b2 pressed")
self._b2Pressed.emit()
def setupStateMachine(self):
self.sm = QStateMachine()
self.B1 = QState()
self.B1.assignProperty(self.b1, "enabled", True)
self.B1.assignProperty(self.b2, "enabled", False)
self.sm.addState(self.B1)
self.B2 = QState()
self.B2.assignProperty(self.b1, "enabled", False)
self.B2.assignProperty(self.b2, "enabled", True)
self.sm.addState(self.B2)
# The point of it all:
# change states from a signal (or program generated event)
self.B1.addTransition(self._b1Pressed, self.B2)
self.B2.addTransition(self._b2Pressed, self.B1)
<b>self.sm.setInitialState(self.B1)
self.sm.start()</b>