Results 1 to 11 of 11

Thread: Qwidget Animation with Qtimer

  1. #1
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Qwidget Animation with Qtimer

    I want to make an animation with two circlewidget in a scene. My purpose is to make the first widget with concentric circle animated for a periode of time and then stop it, after make the second widget animated also for a periode of time .

    but I get blocked how to make such thing here is my code:

    Main.cpp:
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QTimer>
    4. #include<unistd.h>
    5. #include<QGraphicsScene>
    6. #include<QGraphicsProxyWidget>
    7. #include<QGraphicsView>
    8. #include "circlewidget.h"
    9. #include<QTimeLine>
    10.  
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14.  
    15. QApplication app(argc, argv);
    16. CircleWidget * circle= new CircleWidget();
    17. CircleWidget * circl= new CircleWidget();
    18.  
    19.  
    20. QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
    21. proxy->setWidget(circle);
    22. proxy->setPos(0,1);
    23.  
    24. scene.addItem(proxy);
    25. QTimer * time=new QTimer();
    26. QTimer * timer=new QTimer();
    27.  
    28. QGraphicsProxyWidget *prox = new QGraphicsProxyWidget();
    29. prox->setWidget(circl);
    30. prox->setPos(200,100);
    31. scene.addItem(prox);
    32.  
    33. /*QTimeLine *timer = new QTimeLine(5000);
    34. timer->setFrameRange(0, 100);*/
    35.  
    36. QGraphicsView view(&scene);
    37.  
    38.  
    39.  
    40. QObject::connect(timer, SIGNAL(timeout()), circl, SLOT(nextAnimationFrame()));
    41. timer->start();
    42. sleep(5);
    43. timer->stop();
    44. QObject::connect(time, SIGNAL(timeout()),circle, SLOT(nextAnimationFrame()));
    45. time->start();
    46. view.show();
    47.  
    48.  
    49.  
    50. return app.exec();
    51.  
    52. }
    To copy to clipboard, switch view to plain text mode 
    circlewidget.cpp:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "circlewidget.h"
    4. #include<unistd.h>
    5. #include <stdlib.h>
    6.  
    7. CircleWidget::CircleWidget(QWidget *parent)
    8. : QWidget(parent)
    9. {
    10.  
    11.  
    12. setBackgroundRole(QPalette::Base);
    13. setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    14.  
    15. }
    16.  
    17.  
    18. QSize CircleWidget::minimumSizeHint() const
    19. {
    20. return QSize(50, 50);
    21. }
    22.  
    23. QSize CircleWidget::sizeHint() const
    24. {
    25. return QSize(180, 180);
    26. }
    27.  
    28. void CircleWidget::nextAnimationFrame()
    29. {
    30.  
    31. update();
    32. ++ frameNo;
    33.  
    34. }
    35.  
    36.  
    37. void CircleWidget::paintEvent(QPaintEvent *)
    38. {
    39. QPainter painter(this);
    40. painter.setRenderHint(QPainter::Antialiasing);
    41. painter.translate(width() / 2, height() / 2);
    42.  
    43. for (int diameter = 0; diameter < 170; diameter += 6) {
    44.  
    45. int delta = abs((frameNo %120) - diameter / 2);
    46. int alpha = 250 - (delta * delta) / 3 - diameter;
    47. if (alpha > 0) {
    48. painter.setPen(QPen(QColor(166, diameter / 2, 32, alpha), 5));
    49.  
    50. painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0,
    51. diameter, diameter));
    52.  
    53. }
    54.  
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 
    ciclewidget.h:
    Qt Code:
    1. #ifndef CIRCLEWIDGET_H
    2. #define CIRCLEWIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. class CircleWidget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. CircleWidget(QWidget *parent = 0);
    13. QSize minimumSizeHint() const;
    14. QSize sizeHint() const;
    15.  
    16. public slots:
    17. void nextAnimationFrame();
    18.  
    19. protected:
    20. void paintEvent(QPaintEvent *event);
    21.  
    22. private:
    23.  
    24. int frameNo;
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 17th June 2014 at 09:13. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qwidget Animation with Qtimer

    You start the timer but the pause the main thread for 5 seconds.
    You then stop the timer.

    So the timer never ever has a chance to actually run.

    QtWidgets is an event based UI framework, so you need to run an event loop to let it process events, such as paint requests, user input or timers.

    the line
    Qt Code:
    1. return app.exec();
    To copy to clipboard, switch view to plain text mode 
    does that.

    What you could do is to use QTimer::singleShot() to delay the start of the second timer.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Qwidget Animation with Qtimer

    thx for you're response,
    I used the QTimer::singleShot() but the animation didn't want to start !

    Qt Code:

    QObject::connect(timer, SIGNAL(timeout()), circl, SLOT(nextAnimationFrame()));
    timer->start();
    QTimer::singleShot(20,circle,SLOT(nextAnimationFra me()));

    view.show();

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qwidget Animation with Qtimer

    Please show your current main().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Qwidget Animation with Qtimer

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QTimer>
    4. #include<unistd.h>
    5. #include<QGraphicsScene>
    6. #include<QGraphicsProxyWidget>
    7. #include<QGraphicsView>
    8. #include "circlewidget.h"
    9. #include<QTimeLine>
    10.  
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14.  
    15. QApplication app(argc, argv);
    16. CircleWidget * circle= new CircleWidget();
    17. CircleWidget * circl= new CircleWidget();
    18.  
    19.  
    20. QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
    21. proxy->setWidget(circle);
    22. proxy->setPos(0,1);
    23.  
    24. scene.addItem(proxy);
    25. QTimer * time=new QTimer();
    26. QTimer * timer=new QTimer();
    27.  
    28. QGraphicsProxyWidget *prox = new QGraphicsProxyWidget();
    29. prox->setWidget(circl);
    30. prox->setPos(200,100);
    31. scene.addItem(prox);
    32.  
    33. /*QTimeLine *timer = new QTimeLine(5000);
    34. timer->setFrameRange(0, 100);*/
    35.  
    36. QGraphicsView view(&scene);
    37.  
    38.  
    39.  
    40. QObject::connect(timer, SIGNAL(timeout()), circl, SLOT(nextAnimationFrame()));
    41. timer->start();
    42. QTimer::singleShot(200,circle,SLOT(nextAnimationFrame()));
    43.  
    44. view.show();
    45.  
    46.  
    47.  
    48. return app.exec();
    49.  
    50. }
    To copy to clipboard, switch view to plain text mode 

    Added after 59 minutes:


    My goal is to make animation with animated widget ( which are the concentric circle widget ) .....May I use the thread in this case ?!
    Last edited by anda_skoa; 17th June 2014 at 14:51. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qwidget Animation with Qtimer

    My suggestion was to use the single shot timer to start the second animation timer

    Qt Code:
    1. QTimer::singleShot(5000, time, SLOT(start()));
    To copy to clipboard, switch view to plain text mode 

    You should probably also set some timer intervals.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qwidget Animation with Qtimer

    What exactly is supposed to be animated? Your nextAnimationFrame() only increments some integer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Qwidget Animation with Qtimer

    But it's animated u can test it if you want :3
    thx anda_skoa you helped me

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qwidget Animation with Qtimer

    You shouldn't animate it like that. Regardless if it works or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Qwidget Animation with Qtimer

    But how should I animate it?! ....they are circles....I want to do resizeable and smoothly animated circle. So I worked on the diametre to create a concentric circle .
    It's not like that I should work ? you make me confused

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qwidget Animation with Qtimer

    Quote Originally Posted by tissa View Post
    But how should I animate it?! ....they are circles....I want to do resizeable and smoothly animated circle
    So create property for the size of the circle and animate that property instead of some "frameNo" integer using some "nextAnimationFrame" function. If you set the diameter to "10" then it is supposed to be "10", if you set it to "16" then it should be "16". Right now you can only increase it by 1 in a rather unreliable manner.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    tissa (18th June 2014)

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 19:47
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. Replies: 1
    Last Post: 27th July 2012, 16:33
  4. Replies: 3
    Last Post: 1st April 2010, 23:56
  5. Replies: 1
    Last Post: 2nd May 2006, 21:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.