Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:36:38+00:00 2026-06-01T16:36:38+00:00

So I am trying to understand a problem with Qt’s QStateMachine and I’m hoping

  • 0

So I am trying to understand a problem with Qt’s QStateMachine and I’m hoping someone can help explain why this is happening. I’m very interested in the fundamental understanding of QStateMachine rather than just the fix.

First consider the state machine with states A, and B and event 1. Event 1 brings you from A to B. A is the initial state.

Specifically this is for maintaining neighbors. In device X I get a message where a neighbor Y says hello. This causes neighbor X to malloc a neighbor state machine for this new neighbor Y. This makes the neighbor state machine and then calls QStateMachine::start( ) ;

Now after this state machine is started I need to continue processing this hello message. So at first I was doing:

QStateMachine::start( ) ;
emit event 1 ;

My understanding is this won’t work because start is an async call and so the state machine is not in its initial start until after start is done. That leads me to my first question.

1) So the state machine start gets placed in the qapp event queue but isn’t emit an async call as well? Won’t the event 1 be placed in the event queue after the start and so wouldn’t that mean we will be in the initial state? Or is the emit not an async call?

Thinking this was the problem I changed my code a little by connecting a function to the state machine started signal. I then changed my code to queue up the events if the state machine is not started and I process this queue of pending events (and emit them to the state machine) after the started signal is called.

Well it turns out the initial state is STILL not set when I get started signal. e.g. QStateMachine::configuration( ).contains( initialstate ) == false. This leads me to my second and bigger question.

2) Why when the started signal is emitted am I not in the initial state.

The sequence of events here is:

  1. Create state machine
  2. Set initial state
  3. Start state machine
  4. Receive event 1
  5. Since not started queue event 1
  6. Started signal rxed
  7. Process event 1
  8. Since is unknown state do nothing
  9. Receive event 1
  10. Process event 1
  11. Now in state A. transition to state B.

The sequence should be:

  1. Create state machine
  2. Set initial state
  3. Start state machine
  4. Receive event 1
  5. Since not started queue event 1
  6. Started signal rxed
  7. Process event 1
  8. Now is state A. transition to state B.
  9. Receive event 1
  10. Process event 1
  11. Now is state B. Do nothing.

Or event better I wish I didn’t have to queue the event. I wish I could do this:

  1. Create state machine
  2. Set initial state
  3. Start state machine
  4. Receive event 1
  5. Process event 1
  6. Now is state A. transition to state B.
  7. Receive event 1
  8. Process event 1
  9. Now is state B. Do nothing.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:36:39+00:00Added an answer on June 1, 2026 at 4:36 pm

    Transition signals are only connected after the state changes (in QStateMachinePrivate::registerSignalTransition) and the connection is not a queued connection:

    bool ok = QMetaObject::connect(sender, signalIndex, signalEventGenerator,
                                       signalEventGenerator->metaObject()->methodOffset());
    

    For the “event 1” to be received, the machine must already be in a state that react to that signal. Even it was a queued connection, the slot would be queued but only after the signal was received, which it isn’t since there is no connection yet at that point.

    To solve your problem, you can wait for the machine is in “state A” before emitting the signal:

    machine->start();
    qApp->processEvents();
    emit event1();
    

    Or you can delay the signal emission and queue it after the other already queued operations:

    machine->start();
    QTimer::singleShot(0, emitter, SIGNAL(event1()));
    // or
    QMetaObject::invokeMethod(emitter, "event1", Qt::QueuedConnection);
    

    The started signal is emitted before the initial state is set (according to source code), which can be helpful if you have initialization to do before any state is set. If you need to wait for the initial state, you can use the signal QState::entered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand if a DHT can be used to solve a problem
I'm trying to understand how to solve this trivial problem in C, in the
I'm trying to understand lists in prolog when I stumbpled over this problem: there's
I trying to understand jquery, and can't figure a problem out. I have a
I have been trying to understand and solve this problem for the last three
I'm having trouble trying to understand how to use recursion with this problem. I'm
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
I'm trying to understand cocoa bindings, but I found a problem that I'm really
Trying to understand this binding process of the WPF. See the code at the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.