在qt中创建qstate [英] Create a qstate in qt

查看:233
本文介绍了在qt中创建qstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个简单的状态机,像

  switch(state)
{
case state_1:
doSomething();
state = state_2;
break;
case state_2:
doSomething_2();
state = state_finished;
break;
}

如何将这个简单的状态机转换为qstate转换?

解决方案

这将是这样的例子:

 code> TestClass test_class; 
QStateMachine machine;

QState * state_1 = new QState(& machine);
QObject :: connect(state_1,SIGNAL(entered()),& test_class,SLOT(doSomething1()));

QState * state_2 = new QState(& machine);
QObject :: connect(state_2,SIGNAL(entered()),& test_class,SLOT(doSomething2()));

QFinalState * state_finished = new QFinalState(& machine);
TestClassFinishedTransition * finished_transition =
new TestClassFinishedTransition(& test_class);
finished_transition-> setTargetState(state_finished);
state_2-> addTransition(finished_transition);

machine.setInitialState(state_1);
machine.start();

TestClass是基于 QObject 的类。 / p>

我希望这段代码能帮助你开始理解这个概念。但是,您必须仔细阅读文档,并通过提供的示例Qt。


If I have a simple state machine like that

  switch(state)
    {
    case state_1:
      doSomething();
    state = state_2;
    break;
    case state_2:
    doSomething_2();
state = state_finished;
    break;
    }

how would I convert this simple state machine into qstate with transitions ?

解决方案

It would be for example something like this:

TestClass test_class;
QStateMachine machine;

QState *state_1 = new QState(&machine);
QObject::connect(state_1, SIGNAL(entered()), &test_class, SLOT(doSomething1()));

QState *state_2 = new QState(&machine);
QObject::connect(state_2, SIGNAL(entered()), &test_class, SLOT(doSomething2()));

QFinalState *state_finished = new QFinalState(&machine);
TestClassFinishedTransition *finished_transition = 
                                new TestClassFinishedTransition(&test_class);
finished_transition->setTargetState(state_finished);
state_2->addTransition(finished_transition);

machine.setInitialState(state_1);
machine.start();

TestClass would be QObject based class.

I hope this piece of code helps you to get started in understanding the concept. However, you have to read the documentation carefully and go through examples provided by Qt.

这篇关于在qt中创建qstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆