这个也比较有用
#ifndef UI_TEST_H
#define UI_TEST_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Form
{
public:
QGroupBox *group2;
QWidget *layoutWidget;
QGridLayout *gridLayout_2;
QPushButton *pushButton;
QLineEdit *lineEdit_11;
QTextEdit *textEdit;
QPushButton *button;
QGroupBox *group1;
QWidget *layoutWidget1;
QGridLayout *gridLayout;
QLineEdit *lineEdit;
QLineEdit *lineEdit_2;
QLineEdit *lineEdit_3;
QLineEdit *lineEdit_5;
QLineEdit *lineEdit_6;
QLineEdit *lineEdit_7;
QLineEdit *lineEdit_4;
QLineEdit *lineEdit_8;
QLineEdit *lineEdit_9;
QLineEdit *lineEdit_10;
void setupUi(QWidget *Form)
{
if (Form->objectName().isEmpty())
Form->setObjectName(QString::fromUtf8("Form"));
Form->resize(315, 452);
group2 = new QGroupBox(Form);
group2->setObjectName(QString::fromUtf8("group2"));
group2->setGeometry(QRect(10, 210, 291, 231));
layoutWidget = new QWidget(group2);
layoutWidget->setObjectName(QString::fromUtf8("layoutWidget"));
layoutWidget->setGeometry(QRect(10, 20, 271, 201));
gridLayout_2 = new QGridLayout(layoutWidget);
gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
gridLayout_2->setContentsMargins(0, 0, 0, 0);
pushButton = new QPushButton(layoutWidget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
gridLayout_2->addWidget(pushButton, 0, 0, 1, 1);
lineEdit_11 = new QLineEdit(layoutWidget);
lineEdit_11->setObjectName(QString::fromUtf8("lineEdit_11"));
gridLayout_2->addWidget(lineEdit_11, 0, 1, 1, 1);
textEdit = new QTextEdit(layoutWidget);
textEdit->setObjectName(QString::fromUtf8("textEdit"));
textEdit->setMinimumSize(QSize(0, 120));
textEdit->setMaximumSize(QSize(16777215, 120));
gridLayout_2->addWidget(textEdit, 1, 0, 1, 2);
button = new QPushButton(Form);
button->setObjectName(QString::fromUtf8("button"));
button->setGeometry(QRect(11, 12, 75, 23));
group1 = new QGroupBox(Form);
group1->setObjectName(QString::fromUtf8("group1"));
group1->setGeometry(QRect(11, 41, 291, 170));
layoutWidget1 = new QWidget(group1);
layoutWidget1->setObjectName(QString::fromUtf8("layoutWidget1"));
layoutWidget1->setGeometry(QRect(10, 30, 271, 126));
gridLayout = new QGridLayout(layoutWidget1);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setContentsMargins(0, 0, 0, 0);
lineEdit = new QLineEdit(layoutWidget1);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
gridLayout->addWidget(lineEdit, 0, 0, 1, 1);
lineEdit_2 = new QLineEdit(layoutWidget1);
lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
gridLayout->addWidget(lineEdit_2, 0, 1, 1, 1);
lineEdit_3 = new QLineEdit(layoutWidget1);
lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
gridLayout->addWidget(lineEdit_3, 1, 0, 1, 1);
lineEdit_5 = new QLineEdit(layoutWidget1);
lineEdit_5->setObjectName(QString::fromUtf8("lineEdit_5"));
gridLayout->addWidget(lineEdit_5, 1, 1, 1, 1);
lineEdit_6 = new QLineEdit(layoutWidget1);
lineEdit_6->setObjectName(QString::fromUtf8("lineEdit_6"));
gridLayout->addWidget(lineEdit_6, 2, 0, 1, 1);
lineEdit_7 = new QLineEdit(layoutWidget1);
lineEdit_7->setObjectName(QString::fromUtf8("lineEdit_7"));
gridLayout->addWidget(lineEdit_7, 2, 1, 1, 1);
lineEdit_4 = new QLineEdit(layoutWidget1);
lineEdit_4->setObjectName(QString::fromUtf8("lineEdit_4"));
gridLayout->addWidget(lineEdit_4, 3, 0, 1, 1);
lineEdit_8 = new QLineEdit(layoutWidget1);
lineEdit_8->setObjectName(QString::fromUtf8("lineEdit_8"));
gridLayout->addWidget(lineEdit_8, 4, 1, 1, 1);
lineEdit_9 = new QLineEdit(layoutWidget1);
lineEdit_9->setObjectName(QString::fromUtf8("lineEdit_9"));
gridLayout->addWidget(lineEdit_9, 3, 1, 1, 1);
lineEdit_10 = new QLineEdit(layoutWidget1);
lineEdit_10->setObjectName(QString::fromUtf8("lineEdit_10"));
gridLayout->addWidget(lineEdit_10, 4, 0, 1, 1);
retranslateUi(Form);
QMetaObject::connectSlotsByName(Form);
} // setupUi
void retranslateUi(QWidget *Form)
{
Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
group2->setTitle(QApplication::translate("Form", "Edit Box", 0, QApplication::UnicodeUTF8));
pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
button->setText(QApplication::translate("Form", "Click", 0, QApplication::UnicodeUTF8));
group1->setTitle(QApplication::translate("Form", "What is this?", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class Form: public Ui_Form {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_TEST_H
#ifndef QTEST_H
#define QTEST_H
#include <QWidget>
#include <QState>
#include <QStateMachine>
#include "ui_test.h"
class QTest : public QWidget
{
Q_OBJECT
public:
QTest(QWidget *parent = 0, Qt::WFlags flags = 0);
~QTest();
private:
Ui::Form ui;
QStateMachine* machine;
QState* state1;
QState* state2;
};
#endif
#include <QPropertyAnimation>
#include <QSignalTransition>
#include "qtest.h"
#include <iostream>
QTest::QTest(QWidget *parent, Qt::WFlags flags):QWidget(parent, flags)
{
machine = new QStateMachine(this);
state1 = new QState(machine);
state2 = new QState(machine);
ui.setupUi(this);
ui.textEdit->resize(280,180);
ui.group1->resize(300,210);
ui.group1->move(10,40);
ui.group2->resize(300,0);
ui.group2->move(10,40);
resize(320,280);
state1->assignProperty(ui.group1,"geometry",QRect(10,40,300,210));
state1->assignProperty(ui.group2,"geometry",QRect(10,260,300,0));
state2->assignProperty(ui.group1,"geometry",QRect(10,40,300,0));
state2->assignProperty(ui.group2,"geometry",QRect(10,40,300,210));
QPropertyAnimation* ani1 = new QPropertyAnimation(ui.group2,"geometry");
ani1->setDuration(1200);
ani1->setEasingCurve(QEasingCurve::Linear);
QPropertyAnimation* ani2 = new QPropertyAnimation(ui.group1,"geometry");
ani2->setDuration(1200);
ani2->setEasingCurve(QEasingCurve::Linear);
QSignalTransition *transition1=state1->addTransition(ui.button,SIGNAL(clicked()),state2);
transition1->addAnimation(ani2);
transition1->addAnimation(ani1);
QSignalTransition *transition2=state2->addTransition(ui.button,SIGNAL(clicked()),state1);
transition2->addAnimation(ani2);
transition2->addAnimation(ani1);
machine->setInitialState(state1);
machine->start();
}
QTest::~QTest()
{
}
要修改为水平动画,可以做以下修改:
state1->assignProperty(ui.group1,"geometry",QRect(10,40,300,210));
state1->assignProperty(ui.group2,"geometry",QRect(310,40,0,210));
state2->assignProperty(ui.group1,"geometry",QRect(10,40,0,210));
state2->assignProperty(ui.group2,"geometry",QRect(10,40,300,210));