I have the following simple little widget:
Attachment 7736
Attachment 7735
Works fine as a top-level widget. As a child widget though, it doesn't work.
What's wrong with it? Any help appreciated.
Printable View
I have the following simple little widget:
Attachment 7736
Attachment 7735
Works fine as a top-level widget. As a child widget though, it doesn't work.
What's wrong with it? Any help appreciated.
the problem is you're using (actually, abusing) the palette .
Code:
#if !defined ANIMATED_SQUARE_H # define ANIMATED_SQUARE_H # include <QtGui/QPalette> # include <QtGui/QWidget> #include <QVariant> class QPropertyAnimation; /** * A square that transitions from one color to another. */ class AnimatedSquare { Q_OBJECT READ animColor WRITE setAnimColor) Q_PROPERTY (int animDuration READ animDuration WRITE setAnimDuration) public: ~AnimatedSquare(); void setAnimColors (const QColor&, const QColor&); void setAnimColor (const QColor&); int animDuration() const; void setAnimDuration (int); void start(); protected: private: QPropertyAnimation* animation; int duration; QVariant m_val; }; inline QSize AnimatedSquare::minimumSizeHint() const { } inline QSize AnimatedSquare::sizeHint() const { return minimumSizeHint(); } inline QColor AnimatedSquare::animColor() const { return m_val.value<QColor>(); } inline int AnimatedSquare::animDuration() const { return duration; } inline void AnimatedSquare::setAnimDuration (int milliseconds) { duration = milliseconds; } #endif // !defined ANIMATED_SQUARE_H
Code:
#include <QtCore/QPropertyAnimation> #include <QtGui/QPainter> #include <QPaintEvent> #include <QColor> #include <QDebug> #include "AnimatedSquare.h" , animation (new QPropertyAnimation (this, "animColor")) , duration (1000) { animation->setDuration(duration); animation->setLoopCount (-1); // infinite } { QColor c; if(m_val.canConvert<QColor>()) c = m_val.value<QColor>(); painter.fillRect(rect(), c); } void AnimatedSquare::setAnimColors (const QColor& begin, const QColor& end) { animation->setStartValue(begin); animation->setEndValue(end); } void AnimatedSquare::start() { animation->start(); } void AnimatedSquare::setAnimColor (const QColor& newColor) { m_val = newColor; update(); } AnimatedSquare::~AnimatedSquare() { delete animation; }