Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStateMachine and GUI update in a QState::onExit()
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DerekDomino
    wrote on last edited by
    #1

    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()?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Santosh Reddy
      wrote on last edited by
      #2

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

      SS

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerekDomino
        wrote on last edited by
        #3

        Hi,

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

        // shows the statusWidget
        stackedLayout->setCurrentIndex(1);
        

        // trying to refresh the GUI
        myQApp->processEvents();
        //statusWidget->update();
        //stackedLayout->update();

        // 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?
        1 Reply Last reply
        0
        • S Offline
          S Offline
          Santosh Reddy
          wrote on last edited by
          #4

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

          SS

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerekDomino
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Santosh Reddy
              wrote on last edited by
              #6

              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?

              SS

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved