您需要做的就是update()
从计时器事件中触发一个。该update()
方法在小部件上调度 a paintEvent
。
在之外的小部件上绘画是无效的paintEvent
- 这是我发布此答案时所有其他答案所做的错误。仅仅调用该paintEvent
方法不是一种解决方法。你应该打电话update()
。打电话repaint()
也可以,但只有在您了解两者的区别update()
并有充分理由这样做时才能这样做。
class Circle;
class MyWidget : public QWidget {
Q_OBJECT
QBasicTimer m_timer;
QList<Circle> m_circles;
void method(Circle &);
void paintEvent(QPaintEvent * p) {
QPainter painter(this);
// WARNING: this method can be called at any time
// If you're basing animations on passage of time,
// use a QElapsedTimer to find out how much time has
// passed since the last repaint, and advance the animation
// based on that.
...
for(int i=0; i<10;i++)
{
method(m_circles[i]); //co-ordinates change
m_circles[i].paint(&painter);
}
...
}
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_timer.timerId()) {
QWidget::timerEvent(ev);
return;
}
update();
}
public:
MyWidget(QWidget*parent = 0) : QWidget(parent) {
...
m_timer.start(1000, this);
}
};