Interface switching fly-in fly-out

Source: Internet
Author: User

A simple animation implemented with QT, it's relatively simple

So, directly on the code, hehe.

[CPP]View Plaincopyprint?
  1. Tqt.h
  2. #ifndef Tqt_h_
  3. #define Tqt_h_
  4. #include <QtGui>
  5. #include <QtCore>
  6. Class Widget: Public qwidget
  7. {
  8. Q_object
  9. Private
  10. Qframe *FRAME[10];
  11. Qpushbutton *prevbutton;
  12. Qpushbutton *nextbutton;
  13. Qpropertyanimation *animation1;
  14. Qpropertyanimation *animation2;
  15. Qsequentialanimationgroup *animationgroup;
  16. Qsize winsize;
  17. int index;
  18. bool ischanging;
  19. Protected
  20. void Resizeevent (Qresizeevent *event);
  21. Public
  22. Widget (Qwidget *parent = 0);
  23. ~widget ();
  24. Public Slots:
  25. void Clickedprevbutton ();
  26. void Clickednextbutton ();
  27. void animationfinished ();
  28. };
  29. #endif
  30. Tqt.cpp
  31. #include "Tqt.h"
  32. Widget::widget (qwidget *parent/ * = 0 */)
  33. : Qwidget (parent)
  34. {
  35. Setwindowtitle ("Widgets");
  36. Resize (400, 300);
  37. Animation1 = New Qpropertyanimation (this);
  38. Animation2 = New Qpropertyanimation (this);
  39. Animationgroup = new Qsequentialanimationgroup;
  40. Prevbutton = New Qpushbutton ("prev", this );
  41. Nextbutton = New Qpushbutton ("Next", this );
  42. Qhboxlayout *sublayout = new Qhboxlayout;
  43. Qvboxlayout *layout = new Qvboxlayout;
  44. Sublayout->addstretch ();
  45. Sublayout->addwidget (Prevbutton);
  46. Sublayout->addwidget (Nextbutton);
  47. Sublayout->addstretch ();
  48. Layout->addstretch ();
  49. Layout->addlayout (sublayout);
  50. SetLayout (layout);
  51. Winsize = size ();
  52. index = 0;
  53. For (int i=0; i<10; i++)
  54. {
  55. Frame[i] = new Qframe (this);
  56. Frame[i]->setobjectname ("Avatar");
  57. //0.jpg~9.jpg is 10 images in the current directory
  58. QString str = QString ("Qframe#avatar{border-image:url (%1.jpg)}")
  59. . Arg (Qstring::number (i));
  60. Frame[i]->setstylesheet (str);
  61. }
  62. Prevbutton->setenabled (false);
  63. Animation1->setstartvalue (Qpoint (Winsize.width ()/3, 10));
  64. Animation1->setendvalue (Qpoint (Winsize.width (), 10));
  65. Animation1->setduration (2000);
  66. Animation1->setpropertyname ("pos");
  67. Animation2->setstartvalue (Qpoint (-winsize.width ()/3, 10));
  68. Animation2->setendvalue (Qpoint (Winsize.width ()/3, 10));
  69. Animation2->setduration (2000);
  70. Animation2->setpropertyname ("pos");
  71. Animationgroup->addanimation (Animation1);
  72. Animationgroup->addanimation (Animation2);
  73. index = 0;
  74. Animation1->settargetobject (Frame[index]);
  75. Ischanging = false;
  76. Connect (Prevbutton, SIGNAL (clicked ()), this , SLOT (Clickedprevbutton ()));
  77. Connect (Nextbutton, SIGNAL (clicked ()), this , SLOT (Clickednextbutton ()));
  78. Connect (Animationgroup, SIGNAL (finished ()), this , SLOT (animationfinished ()));
  79. }
  80. Widget::~widget ()
  81. {
  82. }
  83. void Widget::resizeevent (Qresizeevent *event)
  84. {
  85. Winsize = size ();
  86. For (int i=0; i<10; i++)
  87. Frame[i]->setgeometry (-winsize.width ()/3, ten, Winsize.width ()/3, Winsize.height ()-50);
  88. Frame[index]->setgeometry (Winsize.width ()/3, ten, Winsize.width ()/3, Winsize.height ()-50);
  89. Animation1->setstartvalue (Qpoint (Winsize.width ()/3, 10));
  90. Animation1->setendvalue (Qpoint (Winsize.width (), 10));
  91. Animation2->setstartvalue (Qpoint (-winsize.width ()/3, 10));
  92. Animation2->setendvalue (Qpoint (Winsize.width ()/3, 10));
  93. }
  94. void Widget::clickedprevbutton ()
  95. {
  96. if (ischanging)
  97. return;
  98. Nextbutton->setenabled (true);
  99. Ischanging = true;
  100. Setfixedsize (Winsize.width (), Winsize.height ());
  101. index--;
  102. Animation2->settargetobject (Frame[index]);
  103. Animationgroup->start ();
  104. if (index <= 0)
  105. Prevbutton->setenabled (false);
  106. }
  107. void Widget::clickednextbutton ()
  108. {
  109. if (ischanging)
  110. return;
  111. Prevbutton->setenabled (true);
  112. Ischanging = true;
  113. Setfixedsize (Winsize.width (), Winsize.height ());
  114. index++;
  115. Animation2->settargetobject (Frame[index]);
  116. Animationgroup->start ();
  117. if (index >= 9)
  118. Nextbutton->setenabled (false);
  119. }
  120. void Widget::animationfinished ()
  121. {
  122. Ischanging = false;
  123. Setmaximumsize (Qwidgetsize_max, Qwidgetsize_max);
  124. Animation1->settargetobject (Frame[index]);
  125. }
  126. Main.cpp
  127. #include "Tqt.h"
  128. int main (int argc, char **argv)
  129. {
  130. Qapplication app (argc, argv);
  131. Widget *widget = new Widget;
  132. Widget->show ();
  133. return app.exec ();
  134. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.