简体   繁体   English

使用Qt和C ++连续绘制对象

[英]Continuously drawing object using Qt and C++

I have one problem which I can't solve using the Internet. 我有一个使用互联网无法解决的问题。 I have label and I set pixmap on it. 我有标签,并在上面设置了像素图。 I put it on main window (widget) where is button (QPushButton) too. 我也把它放在按钮(QPushButton)所在的主窗口(小部件)上。 I want to do that: 我想这样做:

  1. If I click on the button then on this pixmap will be drawn circles continuously 如果我单击按钮,则在此像素图上将连续绘制圆圈
  2. If I click this button for second then drawing must be stopped by function pause() 如果我第二次单击此按钮,则必须通过功能pause()停止绘图

The second one is easy, it's empty slot: 第二个很简单,它是空插槽:

void pause() {}

But at first I've tried to use loop 但是起初我尝试使用循环

while(true)
    draw();

but it crashed a program (loop). 但它使程序崩溃(循环)。

Any idea how to solve it? 知道如何解决吗?

You should never block the main thread. 您永远不应阻塞主线程。 This will cause the OS to consider your application has hanged. 这将导致操作系统认为您的应用程序已挂起。 In fact it is a good practice to move any code, whose execution takes more than 50 milliseconds to another thread to keep the main thread responsive, especially in the case of Qt, where it is also the GUI thread. 实际上,将任何执行时间超过50毫秒的代码移动到另一个线程以保持主线程的响应是一个好习惯,尤其是在Qt也是GUI线程的情况下。

You should use an event driven approach, which will not block the thread. 您应该使用事件驱动的方法,该方法不会阻塞线程。

class YourClass : public QObject { // QObject or derived
    Q_OBJECT
public:
    YourClass() { connect(&timer, &Timer::timeout, this, &YourClass::draw); }
public slots:
    void start() { timer.start(33); }
    void pause() { timer.stop(); }
private:
    QTimer timer;
    void draw() { ... }
};

When start() is invoked, draw() will be called every 33 miliseconds. 调用start() ,将每33毫秒调用一次draw() pause() will effectively stop that until start() is invoked again. pause()将有效地停止该操作,直到再次调用start()为止。 You can control the rate at which draw() is invoked by adjusting the timer's interval, by default it is 0, which in the case of drawing is overkill, you should adjust for a desired framers per second. 您可以通过调整计时器的时间间隔来控制调用draw()的速率,默认情况下为0,如果绘图过大,则应调整为每秒所需的成帧器。 In the example above, the it is 33 milliseconds, or roughly 30 FPS. 在上面的示例中,它是33毫秒,大约是30 FPS。

You should then call draw() with some time interval, instead of halting the whole GUI thread with it. 然后,您应该以一定的时间间隔调用draw() ,而不是使用它停止整个GUI线程。

For that, there's QTimer : 为此,有QTimer

QTimer timer; // should be a member, a pointer optionally - you then do new Qtimer(this);
connect(&timer, &QTimer::timeout, draw);
timer.start(500); // in milliseconds
// assuming you are calling this from member function of QObject-deriving class
// and draw is a non-member function

If you know to do the connections, you can connect it to anything... 如果您知道要进行连接,则可以将其连接到任何东西...

The same can be done with QThread and putting it to sleep in that loop. 使用QThread可以完成同样的操作,并将其置于该循环中。

Anyhow , I don't get how an empty pause() stops the drawing. 无论如何 ,我不知道如何使用空的pause()停止绘图。 Aren't you halting your application again? 您不再次暂停应用程序吗? Just do timer.stop(); 只是做timer.stop(); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM