QT:界面切换之飞入飞出

RT,

用QT实现的一个简单动画,比较简单

所以,直接上代码,呵呵。


//tqt.h
#ifndef TQT_H_
#define TQT_H_

#include <QtGui>
#include <QtCore>

class Widget : public QWidget
{
	Q_OBJECT
private:
	QFrame *frame[10];
	QPushButton *prevButton;
	QPushButton *nextButton;
	QPropertyAnimation *animation1;
	QPropertyAnimation *animation2;
	QSequentialAnimationGroup *animationGroup;

	QSize winSize;
	int index;
	bool isChanging;

protected:
	void resizeEvent(QResizeEvent *event);

public:
	Widget(QWidget *parent = 0);
	~Widget();

	public slots:
		void clickedPrevButton();
		void clickedNextButton();
		void animationFinished();
};


#endif



//tqt.cpp
#include "tqt.h"

Widget::Widget(QWidget *parent /* = 0 */)
: QWidget(parent)
{
	setWindowTitle("Widget");
	resize(400, 300);

	animation1 = new QPropertyAnimation(this);
	animation2 = new QPropertyAnimation(this);
	animationGroup = new QSequentialAnimationGroup;

	prevButton = new QPushButton("prev", this);
	nextButton = new QPushButton("next", this);
	QHBoxLayout *subLayout = new QHBoxLayout;
	QVBoxLayout *layout = new QVBoxLayout;
	subLayout->addStretch();
	subLayout->addWidget(prevButton);
	subLayout->addWidget(nextButton);
	subLayout->addStretch();
	layout->addStretch();
	layout->addLayout(subLayout);
	setLayout(layout);

	winSize = size();
	index = 0;
	for(int i=0; i<10; i++)
	{
		frame[i] = new QFrame(this);
		frame[i]->setObjectName("avatar");
       //0.jpg~9.jpg是当前目录下的10张图片
		QString str = QString("QFrame#avatar{border-image:url(%1.jpg)}")
			.arg( QString::number(i) );
		frame[i]->setStyleSheet(str);
	}
	prevButton->setEnabled(false);

	animation1->setStartValue( QPoint(winSize.width()/3, 10) );
	animation1->setEndValue( QPoint(winSize.width(), 10) );
	animation1->setDuration(2000);
	animation1->setPropertyName("pos");
	animation2->setStartValue( QPoint(-winSize.width()/3, 10) );
	animation2->setEndValue( QPoint(winSize.width()/3, 10) );
	animation2->setDuration(2000);
	animation2->setPropertyName("pos");
	animationGroup->addAnimation(animation1);
	animationGroup->addAnimation(animation2);
	
	index = 0;
	animation1->setTargetObject(frame[index]);
	isChanging = false;
	connect(prevButton, SIGNAL(clicked()), this, SLOT(clickedPrevButton()));
	connect(nextButton, SIGNAL(clicked()), this, SLOT(clickedNextButton()));
	connect(animationGroup, SIGNAL(finished()), this, SLOT(animationFinished()));
}

Widget::~Widget()
{

}

void Widget::resizeEvent(QResizeEvent *event)
{
	winSize = size();
	for(int i=0; i<10; i++)
		frame[i]->setGeometry(-winSize.width()/3, 10, winSize.width()/3, winSize.height()-50);
	frame[index]->setGeometry(winSize.width()/3, 10, winSize.width()/3, winSize.height()-50);

	animation1->setStartValue( QPoint(winSize.width()/3, 10) );
	animation1->setEndValue( QPoint(winSize.width(), 10) );
	animation2->setStartValue( QPoint(-winSize.width()/3, 10) );
	animation2->setEndValue( QPoint(winSize.width()/3, 10) );

}

void Widget::clickedPrevButton()
{
	if(isChanging)
		return;
	nextButton->setEnabled(true);
	isChanging = true;
	setFixedSize(winSize.width(), winSize.height());
	index--;
	animation2->setTargetObject(frame[index]);
	animationGroup->start();
	if(index <= 0)
		prevButton->setEnabled(false);
}


void Widget::clickedNextButton()
{
	if(isChanging)
		return;
	prevButton->setEnabled(true);
	isChanging = true;
	setFixedSize(winSize.width(), winSize.height());
	index++;
	animation2->setTargetObject(frame[index]);
	animationGroup->start();
	if(index >= 9)
		nextButton->setEnabled(false);
}

void Widget::animationFinished()
{
	isChanging = false;
	setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
	animation1->setTargetObject(frame[index]);
}



//main.cpp
#include "tqt.h"

int main(int argc, char **argv)
{
	QApplication app(argc, argv);
	Widget *widget = new Widget;
	widget->show();
	return app.exec();
}


本程序有10张图片,按prev, next可前后切换

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值