I need to be able to change state of a QStateMachine from a Signal (or something equivalent) that was not generated by user interaction (i.e. by a process completing). This example redirectsa user generated signal to demonstrate the idea, but doesn't yet work. Here, the push button signals fire other signals that I want to use for the transition. The states here are B1 <-> B2
where Bi -> Bj
on the redirected signal when button bi
is pressed.
The QStateMachine documentation only seems to cover transitions based on direct signals from buttons and I haven't been able to extrapolate to the more general case of using any Signal (i.e. one that I create).
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_())
Your code has the following problems: