A simple animation implemented with QT, it's relatively simple
So, directly on the code, hehe.
[CPP]View Plaincopyprint?
- 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 ("Widgets");
- 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 is 10 images in the current directory
- 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, ten, Winsize.width ()/3, Winsize.height ()-50);
- Frame[index]->setgeometry (Winsize.width ()/3, ten, 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 ();
- }
This program has 10 pictures, press prev, next can switch back and forth
http://blog.csdn.net/small_qch/article/details/6858611
Interface switching fly-in fly-out