/tmp/snapshot-pyside-6.2-rel/pyside-setup/examples/widgets/state-machine/eventtrans¶
(You can also check this code in the repository)
import sys
from PySide6.QtCore import QEvent, QRect, Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton
from PySide6.QtStateMachine import QEventTransition, QState, QStateMachine
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
button = QPushButton(self)
button.setGeometry(QRect(100, 100, 100, 100))
machine = QStateMachine(self)
s1 = QState()
s1.assignProperty(button, 'text', 'Outside')
s2 = QState()
s2.assignProperty(button, 'text', 'Inside')
enter_transition = QEventTransition(button, QEvent.Enter)
enter_transition.setTargetState(s2)
s1.addTransition(enter_transition)
leave_transition = QEventTransition(button, QEvent.Leave)
leave_transition.setTargetState(s1)
s2.addTransition(leave_transition)
s3 = QState()
s3.assignProperty(button, 'text', 'Pressing...')
press_transition = QEventTransition(button, QEvent.MouseButtonPress)
press_transition.setTargetState(s3)
s2.addTransition(press_transition)
release_transition = QEventTransition(button, QEvent.MouseButtonRelease)
release_transition.setTargetState(s2)
s3.addTransition(release_transition)
machine.addState(s1)
machine.addState(s2)
machine.addState(s3)
machine.setInitialState(s1)
machine.start()
self.setCentralWidget(button)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
main_win = MainWindow()
sys.exit(app.exec())
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.