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 9251845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:48:08+00:00 2026-06-18T10:48:08+00:00

I’m having some trouble with the Qt State Machine. I’m never able to execute

  • 0

I’m having some trouble with the Qt State Machine. I’m never able to execute the states: I get various types of errors and I can’t figure out what is the problem. Please see below for more information on the errors and the code that leads to them.

This is my main function:

int main(int argc, char *argv[])
{
    // Initialize the app
    // ... app initialization here
    QApplication app(argc, argv);

    // Create the state machine
    QStateMachine machine;

    MyState* ms1 = new MyState("ms1");
    MyState* ms2 = new MyState("ms2");
    QFinalState* fs = new QFinalState();

    ms1->addTransition(ms2);
    ms2->addTransition(fs);

    machine.addState(ms1);
    machine.addState(ms2);
    machine.addState(fs);
    machine.setInitialState(ms1);

    QObject::connect(&machine, SIGNAL(finished()), new Finisher(), SLOT(OnFinished()));
    machine.start();
    return app.exec();
}

I set break points in MyState, but the break points never get hit and the start/stop methods never execute (I actually only expect start to be called). The application goes directly into the Finisher::OnFinished method.

Here is my state class:

class MyState: public QState
{
    Q_OBJECT
private:
    QString _stateName;
public:
    MyState(QString stateName, QState * parent = 0):QState(parent), _stateName(stateName){}
    ~MyState(){}

public slots:
    void start()
    {
        qDebug() << _stateName << " start.";
        emit finished();
    }

    void stop()
    {
        qDebug() << _stateName << " stop.";
    }

signals:
    void finished();
    //void exited();
};

The Finisher is pretty simple:

class Finisher:public QObject
{
    Q_OBJECT
    public slots:
    void OnFinished()
    {
        qDebug() << "Finished!";
        QTimer::singleShot(0, QApplication::instance(), SLOT(quit()));
    }
};

I’ve tried several things, including setting the parent of each state directly:

// If I use the following code, I get an error 
// stating that "the state has already been added to this machine"
MyState* ms1 = new MyState("ms1");
MyState* ms2 = new MyState("ms2", ms1);
QFinalState* fs = new QFinalState(ms2);

ms1->addTransition(ms2);
ms2->addTransition(fs);

machine.addState(ms1);
machine.addState(ms2);
machine.addState(fs);
machine.setInitialState(ms1);
// .. and so on

However, when it seems that I can’t add the states when the machine.addState is called:

QStateMachine::addState: state has already been added to this machine
QStateMachine::addState: state has already been added to this machine
Unrecoverable error detected in running state machine: Missing initial state in
compound state ''

I’ve also tried just leaving the constructors and not calling machine.addState:

QStateMachine machine;
MyState* ms1 = new MyState("ms1");
MyState* ms2 = new MyState("ms2", ms1);
QFinalState* fs = new QFinalState(ms2);

ms1->addTransition(ms2);
ms2->addTransition(fs);
machine.setInitialState(ms1);
// .. and so on

However, in this case I get the following errors:

QState::setInitialState: state 0xa05e5e0 is not a child of this state (0x43fe94)

QStateMachine::start: No initial state set for machine. Refusing to start.

The last one is strange because I explicitly state the initial state to ms1 and then I call start, so I don’t know what’s the problem there. Does anybody have any ideas on how to get this example to work?

  • 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-18T10:48:09+00:00Added an answer on June 18, 2026 at 10:48 am

    There is nothing like start() slot in QState – but it exists in QStateMachine.

    I suppose you want to reimplement

    virtual void    onEntry ( QEvent * event )
    virtual void    onExit ( QEvent * event )
    

    methods:

    class MyState: public QState
    {
        Q_OBJECT
    private:
        QString _stateName;
    public:
        MyState(QString stateName, QState * parent = 0):QState(parent), _stateName(stateName){}
        ~MyState(){}
    
    protected:
       virtual  void onEntry(QEvent * event)
        {
            qDebug() << _stateName << " start.";
        }
    
       virtual  void onExit(QEvent * event)
        {
            qDebug() << _stateName << " stop.";
        }
    signals:
        void finished();
        //void exited();
    };
    

    which is working just fine.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I need to clean up various Word 'smart' characters in user input, including but
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.