官术网_书友最值得收藏!

How to do it…

State machine is not that hard to achieve in Qt at all. Let's get started by following these steps:

  1. We will set up a new user interface for our example program, which looks like this:
  1. We will include some headers in our source code:
#include <QStateMachine>
#include <QPropertyAnimation>
#include <QEventTransition>
  1. In our main window's constructor, add the following code to create a new state machine and two states, which we will be using later:
QStateMachine *machine = new QStateMachine(this);
QState *s1 = new QState();
QState *s2 = new QState();
  1. We will define what we should do within each state, which, in this case, will be to change the label's text, as well as the button's position and size:
QState *s1 = new QState();
s1->assignProperty(ui->stateLabel, "text", "Current state: 1");
s1->assignProperty(ui->pushButton, "geometry", QRect(50, 200, 100, 50));

QState *s2 = new QState();
s2->assignProperty(ui->stateLabel, "text", "Current state: 2");
s2->assignProperty(ui->pushButton, "geometry", QRect(200, 50, 140, 100));
  1. Once you are done with that, let's proceed by adding event transition classes to our source code:
QEventTransition *t1 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t1->setTargetState(s2);
s1->addTransition(t1);

QEventTransition *t2 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t2->setTargetState(s1);
s2->addTransition(t2);
  1. Add all of the states we have just created to the state machine and define state 1 as the initial state. Then, call machine->start() to run the state machine:
machine->addState(s1);
machine->addState(s2);
machine->setInitialState(s1);
machine->start();
  1. If you run the example program now, you will notice that everything works fine, except the button is not going through a smooth transition and it simply jumps instantly to the position and size we set previously. This is because we have not used a property animation to create a smooth transition.
  2. Go back to the event transition step and add the following lines of code:
QEventTransition *t1 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t1->setTargetState(s2);
t1->addAnimation(new QPropertyAnimation(ui->pushButton, "geometry"));
s1->addTransition(t1);

QEventTransition *t2 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t2->setTargetState(s1);
t2->addAnimation(new QPropertyAnimation(ui->pushButton, "geometry"));
s2->addTransition(t2);

  1. You can also add an easing curve to the animation to make it look more interesting:
QPropertyAnimation *animation = new QPropertyAnimation(ui->pushButton, "geometry");
animation->setEasingCurve(QEasingCurve::OutBounce);
QEventTransition *t1 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t1->setTargetState(s2);
t1->addAnimation(animation);
s1->addTransition(t1);

QEventTransition *t2 = new QEventTransition(ui->changeState, QEvent::MouseButtonPress);
t2->setTargetState(s1);
t2->addAnimation(animation);
s2->addTransition(t2);
主站蜘蛛池模板: 孙吴县| 新竹市| 商水县| 长子县| 泰兴市| 恩施市| 濮阳县| 通山县| 安仁县| 四会市| 镇远县| 筠连县| 宝兴县| 龙山县| 郸城县| 弋阳县| 唐山市| 贵德县| 大港区| 罗甸县| 嘉鱼县| 平原县| 凤山市| 邻水| 柘荣县| 镶黄旗| 陵水| 宁化县| 鸡泽县| 明溪县| 屏东市| 汕头市| 玉田县| 新巴尔虎左旗| 沙湾县| 广丰县| 遵义县| 晋江市| 简阳市| 吉木乃县| 滁州市|