我创建了一个小型 QT 应用程序,它可以在随机位置重新绘制一个圆圈。 我想做的是使用 QTimer 每秒重复该方法 预定 次绘制圆。
我不确定该怎么做。
这是我的main.cpp
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
srand (time(NULL));
QApplication app(argc, argv);
widget f;
f.show();
return app.exec();
}
小部件.cpp
#include "widget.h"
widget::widget()
{
widget.setupUi(this);
}
void widget::paintEvent(QPaintEvent * p)
{
QPainter painter(this);
//**code
printcircle(& painter); //paints the circle
//**code
}
void paintcircle(QPainter* painter)
{
srand (time(NULL));
int x = rand() %200 + 1;
int y = rand() %200 + 1;
QRectF myQRect(x,y,30,30);
painter->drawEllipse(myQRect);
}
widget::~widget()
{}
widget.h
#ifndef _WIDGET_H
#define _WIDGET_H
class widget : public QWidget {
Q_OBJECT
public:
widget();
virtual ~widget();
public slots:
void paintEvent(QPaintEvent * p);
private:
Ui::widget widget;
};
#endif /* _WIDGET_H */
我将如何创建一个 Qtimer 来重复 printcricle() 方法。
谢谢
最佳答案
您可以在小部件类构造函数中创建一个计时器,如下所示:
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
即它将每秒调用小部件的绘制事件。
关于c++ - QTimer 与 QPaintEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23160374/