#include "ui_maindialog.h"
#include "maindialog.h"
#include <QState>
#include <QEventTransition>
#include <QStateMachine>
#include <qDebug>
MainDialog::MainDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowStaysOnTopHint);
    /*遍历界面上所有button*/
    QList<QPushButton *> button = this->findChildren<QPushButton *>();
    qDebug() << button.count();
    for(int i=0; i < button.count(); i++)//通过i的范围确定需要设置哪些button
    {
        setDynamicImage(button.at(i));
    }
}
void MainDialog::setDynamicImage(QPushButton *button)
{
    /*定义两种状态(鼠标进入button/退出button)*/
    QState *inside = new QState;
    inside->assignProperty(button,"flat", "false");//设置button为凸起
    QState *outside = new QState;
    outside->assignProperty(button, "flat", "true");//设置button为平面
    /*把进入、退出事件与button关联*/
    QEventTransition *enterTransition = new QEventTransition(button, QEvent::Enter);
    enterTransition->setTargetState(inside);//鼠标进入button
    outside->addTransition(enterTransition);//把outside状态,连接到enterTransition变化中去
    QEventTransition *leaveTransition = new QEventTransition(button, QEvent::Leave);
    leaveTransition->setTargetState(outside);//鼠标退出button
    inside->addTransition(leaveTransition);//把inside状态,连接到leaveTransition变化中去
    /*定义状态机*/
    QStateMachine *machine = new QStateMachine(this);
    /*在状态机中添加这两种状态*/
    machine->addState(inside);
    machine->addState(outside);
    /*设置初始状态*/
    machine->setInitialState(outside);
    /*启动状态机*/
    machine->start();
}
MainDialog::~MainDialog()
{
    delete ui;
}

http://blog.csdn.net/mingxia_sui/article/details/7760163
05-06 11:44