使用状态机的并行状态

#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QStateMachine>
#include <QState>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QPushButton button1("clean or not");
    QPushButton button2("moving or not");
    QLabel label ;
    QLabel label1(&label);
    QLabel label2(&label);
    QStateMachine machine ;
    QState *s1 = new QState(QState::ParallelStates);//创建并行状态组,可在独立的子状态间进行切换
    QState *s11 = new QState(s1);
    QState *clean = new QState(s11);
    QState *dirty = new QState(s11);
    s11->setInitialState(clean);
    clean->assignProperty(&label1, "text", "clean" );//为属性分配值
    dirty->assignProperty(&label1, "text", "dirty");
    clean ->addTransition(&button1,&QPushButton::clicked, dirty);
    dirty->addTransition(&button1,&QPushButton::clicked, clean);
    QState *s12=new QState(s1);
    QState *moving =new QState(s12);
    QState *notMoving = new QState(s12);
    s12->setInitialState(notMoving);
    moving ->assignProperty(&label2,"text", "moving");
    notMoving ->assignProperty(&label2, "text", "not moving");
    moving ->addTransition(&button2,&QPushButton::clicked, notMoving);
    notMoving->addTransition(&button2,&QPushButton::clicked, moving);
    machine.addState(s1);
    machine.setInitialState(s1);
    machine.start();
    button1.move(100,300);
    button1.show();
    button2.move(300,300);
    button2.show();
    label1.resize(100,20);
    label2.resize(100,20);
    label2.move(0,20);
    label.move(180,120);
    label.resize(100,50);
    label.show( );

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值