c++ - QTimer 与 QPaintEvent

标签 c++ qt qpainter qtcore qtimer

我创建了一个小型 QT 应用程序,它可以在随机位置重新绘制一个圆圈。 我想做的是使用 QTimer 每秒重复该方法 预定 次绘制圆。

我不确定该怎么做。

这是我的ma​​in.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/

相关文章:

c++ - 是否可以从子类和父类中绘制 QWidget?

c++ - 如何使 std::regex 匹配 Utf8

c++ - "error: subscripted value is not an array, pointer, or vector"我正在使用一个字符串

c++ - 返回 std::vector 时缺少元素

c++ - Qt 样式表中的属性组合

qt - 在 Qt 中,应用程序未在设备上重新启动

c++ - 以编程方式连接到无线网络

c++ - QT Future Watcher 带邮件客户端

c++ - 如何在qt中绘制带彩色角的透明矩形?

c++ - Qt 5.5绘制填充饼图