Results 1 to 2 of 2

Thread: Animated Child Widget

  1. #1

    Default Animated Child Widget

    I have the following simple little widget:

    AnimatedSquare.h
    AnimatedSquare.cpp

    Works fine as a top-level widget. As a child widget though, it doesn't work.

    What's wrong with it? Any help appreciated.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Animated Child Widget

    the problem is you're using (actually, abusing) the palette .

    Qt Code:
    1. #if !defined ANIMATED_SQUARE_H
    2. # define ANIMATED_SQUARE_H
    3.  
    4. # include <QtGui/QPalette>
    5. # include <QtGui/QWidget>
    6.  
    7. #include <QVariant>
    8.  
    9. class QPropertyAnimation;
    10.  
    11. /**
    12.  * A square that transitions from one color to another.
    13.  */
    14. class AnimatedSquare
    15. : public QWidget
    16. {
    17. Q_OBJECT
    18.  
    19. Q_PROPERTY (QColor animColor
    20. READ animColor WRITE setAnimColor)
    21. Q_PROPERTY (int animDuration
    22. READ animDuration WRITE setAnimDuration)
    23.  
    24. public:
    25.  
    26. AnimatedSquare (QWidget* = 0);
    27. ~AnimatedSquare();
    28.  
    29. QSize minimumSizeHint() const;
    30. QSize sizeHint() const;
    31.  
    32. void setAnimColors (const QColor&, const QColor&);
    33. QColor animColor() const;
    34. void setAnimColor (const QColor&);
    35. int animDuration() const;
    36. void setAnimDuration (int);
    37.  
    38. void start();
    39.  
    40. protected:
    41.  
    42. void paintEvent (QPaintEvent*);
    43.  
    44. private:
    45.  
    46. QPropertyAnimation* animation;
    47. int duration;
    48.  
    49. QVariant m_val;
    50. };
    51.  
    52. inline QSize
    53. AnimatedSquare::minimumSizeHint() const
    54. {
    55. return QSize (50, 50);
    56. }
    57.  
    58. inline QSize
    59. AnimatedSquare::sizeHint() const
    60. {
    61. return minimumSizeHint();
    62. }
    63.  
    64. inline QColor
    65. AnimatedSquare::animColor() const
    66. {
    67. return m_val.value<QColor>();
    68. }
    69.  
    70. inline int
    71. AnimatedSquare::animDuration() const
    72. {
    73. return duration;
    74. }
    75.  
    76. inline void
    77. AnimatedSquare::setAnimDuration (int milliseconds)
    78. {
    79. duration = milliseconds;
    80. }
    81.  
    82. #endif // !defined ANIMATED_SQUARE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtCore/QPropertyAnimation>
    2. #include <QtGui/QPainter>
    3.  
    4. #include <QPaintEvent>
    5. #include <QColor>
    6. #include <QDebug>
    7.  
    8. #include "AnimatedSquare.h"
    9.  
    10. AnimatedSquare::AnimatedSquare (QWidget* parent)
    11. : QWidget (parent)
    12. , animation (new QPropertyAnimation (this, "animColor"))
    13. , duration (1000)
    14. {
    15. animation->setDuration(duration);
    16. animation->setLoopCount (-1); // infinite
    17. }
    18.  
    19. void AnimatedSquare::paintEvent(QPaintEvent* e)
    20. {
    21. QPainter painter (this);
    22. QColor c;
    23. if(m_val.canConvert<QColor>())
    24. c = m_val.value<QColor>();
    25.  
    26. painter.fillRect(rect(), c);
    27. }
    28.  
    29. void AnimatedSquare::setAnimColors (const QColor& begin, const QColor& end)
    30. {
    31. animation->setStartValue(begin);
    32. animation->setEndValue(end);
    33. }
    34.  
    35. void AnimatedSquare::start()
    36. {
    37. animation->start();
    38. }
    39.  
    40. void AnimatedSquare::setAnimColor (const QColor& newColor)
    41. {
    42. m_val = newColor;
    43. update();
    44. }
    45.  
    46. AnimatedSquare::~AnimatedSquare()
    47. {
    48. delete animation;
    49. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 3
    Last Post: 20th July 2011, 00:49
  2. Replies: 1
    Last Post: 23rd June 2011, 23:09
  3. Replies: 1
    Last Post: 11th March 2011, 19:34
  4. [QtEmbedded] Translucent QGraphicsView with animated child widgets
    By zuck in forum Qt for Embedded and Mobile
    Replies: 13
    Last Post: 8th January 2010, 12:09
  5. Animated graphical widget in systray. howto?
    By Tetractys in forum Newbie
    Replies: 2
    Last Post: 6th October 2009, 23:51

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.