Continuously drawing object using Qt and C++ -
i have 1 problem can't solve using internet. have label , set pixmap on it. put on main window (widget) button (qpushbutton) too. want that:
- if click on button on pixmap drawn circles continuously
- if click button second drawing must stopped function
pause()
the second 1 easy, it's empty slot:
void pause() {}
but @ first i've tried use loop
while(true) draw();
but crashed program (loop).
any idea how solve it?
you should never block main thread. cause os consider application has hanged. in fact practice move code, execution takes more 50 milliseconds thread keep main thread responsive, in case of qt, gui thread.
you should use event driven approach, not block 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()
invoked, draw()
called every 33 miliseconds. pause()
stop until start()
invoked again. can control rate @ draw()
invoked adjusting timer's interval, default 0, in case of drawing overkill, should adjust desired framers per second. in example above, 33 milliseconds, or 30 fps.
Comments
Post a Comment