Important: Please read the Qt Code of Conduct -

PySide QEvent post crash

  • Hello everyone,

    I'm working on Linux Ubuntu 12.10, with PySide 1.1.1 and python 2.7
    I have a problem when posting QEvent through a QStateMachine.

    If I want it to work I have to keep a reference on the event, or it crashes.
    I have set up a little sample code to illustrate my problem.

    I would like to know if I am doing it wrong or if it is a known problem and if I should use the workaround (keeping a reference on the event) ?


    from future import print_function
    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *

    app = QApplication(sys.argv)
    sm = QStateMachine()
    init = QState(sm)

    e = None
    def no_crash():
    global e
    print("send an event that doesn't crash...")
    e = QEvent(QEvent.Type(QEvent.registerEventType()))

    def crash():
    print("and one that does...")
    e = QEvent(QEvent.Type(QEvent.registerEventType()))

    QTimer.singleShot(2000, no_crash)
    QTimer.singleShot(4000, crash)


    Thanks by advance for your help


  • I initially ran into the same ugly crash when I was converting the 'Events, Transitions, and Guards' example from Qt Project's excellent State Machine Framework doc to PySide. Your global reference to the registered QEvent Type fixes the crash problem because it prevents python's automatic garbage collection from kicking in before you want it to.

    In my final code, rather than using a global reference which is inelegant, I wrap all the functionality into a QWidget which is used as the main window of the app. That allows me to save the registered QEvent as an instance variable in the QWidget's class constructor. The idea being that python knows not to garbage collect instance variables as long as the class instance itself is being used.

Log in to reply