Important: Please read the Qt Code of Conduct -

QStateMachine and GUI update in a QState::onExit()

  • Hello,

    I'm using QStateMachine with several sets of subclassed QState's.

    In one of these, I'm overriding onExit() to make some work when exiting the state. This work is long (about 2 secs) and I would like to update the GUI in order to tell the user that some work is on going. A QStackedLayout is used: one of the layer is shown and the text of a QLabel (inside the QWidget/layer of the stacked layout) is then set at the beginning of onExit(), before the hard work.

    Unfortunately, the GUI is updated later by when onExit() is over and after -I guess- some Qt internal loop/statemachine work.

    I tried to force to update() the widget or processEvents() (QApplication), but was unsuccessful.

    Do you have any hint about updating the GUI in onExit()?


  • qApp->processEvents() should work. Can you show a snippet of it?

  • Hi,

    Here it is:
    void ToolbarShownState::onExit(QEvent * /event/) {
    // sets the text of a QLabel
    statusWidget->setProperty("statusText", tr("Closing ..."));

    // shows the statusWidget

    // trying to refresh the GUI

    // Do some heavy work here
    // can last several seconds
    The setProperty is called since I can see in the console the qDebug() output it logs.

    I have other states where the same problem is encountered, GUI refreshes only when QStateMachine finishes its duty.

    The heavy work could be deferred in a call executed later with QTimer + 0 second delay or a connect() with a QueuedConnection, but it is not clear to me when the slot will be executed:

    • just after onExit()?
    • after QStateMachine has finished all its work (e.g., after entering into the next state, that would be a disaster)?
    • randomly?

  • What is statusWidget?
    If statusWidget is a QLabel then use "text" instead of "statusText"

  • I have subclassed QWidget, added it a QLabel, some methods and properties. Before this call the the QLabel has already a value (a different one).

    The second call (stackedLayout->setCurrentIndex(1);) doesn't trigger GUI update neither. The role of this call is to raise the statusWidget in stack.

  • I hope you have properly registered the property, and are handleing the QLabel text settings accordingly.

    Also whats with myQApp? are you sure that is not a problem?

Log in to reply