效果不太明显
class StateMachine : public QWidget
{
Q_OBJECT
public:
StateMachine(QWidget *parent = 0, Qt::WFlags flags = 0);
~StateMachine();
private:
void createWidget();
private:
QStateMachine* machine;
QState* state1;
QState* state2;
QLayout* layout;
QLineEdit* edit1;
QPushButton* button1;
QLineEdit* edit2;
QPushButton* button2;
};
StateMachine::StateMachine(QWidget *parent, Qt::WFlags flags):
QWidget(parent,flags)
{
machine = new QStateMachine;
state1 = new QState(machine);
state2 = new QState(machine);
createWidget();
}
StateMachine::~StateMachine()
{
}
void StateMachine::createWidget()
{
layout = new QHBoxLayout(this);
edit1 = new QLineEdit("QLineEdit1");
button1 = new QPushButton("Click1");
edit2 = new QLineEdit("QLineEdit2");
button2 = new QPushButton("Click2");
edit2->setVisible(false);
button2->setVisible(false);
layout->addWidget(edit1);
layout->addWidget(button1);
layout->addWidget(edit2);
layout->addWidget(button2);
state1->assignProperty(edit2,"visible",false);
state1->assignProperty(button2,"visible",false);
state1->assignProperty(edit1,"visible",true);
state1->assignProperty(button1,"visible",true);
machine->setInitialState(state1);
state2->assignProperty(edit1,"visible",false);
state2->assignProperty(button1,"visible",false);
state2->assignProperty(edit2,"visible",true);
state2->assignProperty(button2,"visible",true);
QPropertyAnimation* ani = new QPropertyAnimation();
ani->setDuration(2000);
ani->setEasingCurve(QEasingCurve::InOutExpo);
QSignalTransition *transition1=state1->addTransition(button1,SIGNAL(clicked()),state2);
transition1->addAnimation(ani);
QSignalTransition *transition2=state2->addTransition(button2,SIGNAL(clicked()),state1);
transition2->addAnimation(ani);
machine->start();
}