我遇到了Qt State Machine.的问题我永远无法执行状态:我遇到了各种类型的错误,我无法弄清楚问题是什么。有关错误及其导致的代码的详细信息,请参阅下文。
这是我的主要功能:
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();
}
我在MyState
中设置了断点,但断点永远不会被击中,start
/ stop
方法永远不会执行(实际上我只希望调用start
)。该应用程序直接进入Finisher::OnFinished
方法。
这是我的州级:
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();
};
Finisher非常简单:
class Finisher:public QObject
{
Q_OBJECT
public slots:
void OnFinished()
{
qDebug() << "Finished!";
QTimer::singleShot(0, QApplication::instance(), SLOT(quit()));
}
};
我尝试了几件事,包括直接设置每个州的父母:
// 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
但是,当调用machine.addState
时似乎无法添加状态:
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 ''
我也试过离开构造函数而不是调用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
但是,在这种情况下,我会收到以下错误:
QState::setInitialState: state 0xa05e5e0 is not a child of this state (0x43fe94)
QStateMachine::start: No initial state set for machine. Refusing to start.
最后一个是奇怪的,因为我明确地将初始状态说明为ms1
,然后我调用start
,所以我不知道那里有什么问题。有没有人对如何让这个例子起作用有任何想法?
答案 0 :(得分:2)
start()
中没有QState
插槽 - 但它存在于QStateMachine
中。
我想你想重新实现
virtual void onEntry ( QEvent * event )
virtual void onExit ( QEvent * event )
方法:
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();
};
工作正常。