Some questions about loop in QWidget
-
wrote on 29 Jul 2013, 06:43 last edited by
Hello``
i creat a class widget(come from QWidget)
put go() in widget::widget()
@go()
{
while(on)
{
...
repaint();
QApplication::processEvents();
}
}@also put something in closeEvent
@ void Widget::closeEvent(QCloseEvent *e)
{
running=on=false;
e->accept();
//this->deleteLater();
}@
if i run this,and put alt-F4
the window is hidden,but the pro is still runing unless i push the stop buttom in Qt creator.
why?if there is no go() in widget::widget() , the pro can close.
and i put this->deleteLater(); in closeEvent widget::~widget runs twice!
~widget be runned only widget be deleted. why it runs twice?
thank you for help -
wrote on 29 Jul 2013, 06:56 last edited by
Hi, you can use a QTimer to periodically do something, instead of using while loop.
-
wrote on 29 Jul 2013, 06:59 last edited by
[quote author="1+1=2" date="1375080995"]Hi, you can use a QTimer to periodically do something, instead of using while loop.[/quote]
thank you, but QTimer doesn't meet my requirments.
-
wrote on 29 Jul 2013, 07:09 last edited by
Hi, can you tell us your requirments ? I don't think that the things can be done in while loop in GUI thread can not be achieved with QTimer. And I also notice that you called repaint() manully, which seems not a good idea too.
-
wrote on 29 Jul 2013, 07:18 last edited by
[quote author="1+1=2" date="1375081773"]Hi, can you tell us your requirments ? I don't think that the things can be done in while loop in GUI thread can not be achieved with QTimer. And I also notice that you called repaint() manully, which seems not a good idea too.
[/quote]
i want refresh screen with precision.
-
wrote on 29 Jul 2013, 07:34 last edited by
You mean that you have tried following things and find they don't meet your requirments?
- QTimer instead of while-loop
- update() instead of repaint()
-
wrote on 29 Jul 2013, 08:12 last edited by
[quote author="1+1=2" date="1375083260"]You mean that you have tried following things and find they don't meet your requirments?
- QTimer instead of while-loop
- update() instead of repaint()[/quote]
thank you help
QTimer is not so precision
update i will try...
-
wrote on 29 Jul 2013, 08:23 last edited by
Yes, QTimer is not so precision.
But afaik, you can not make it accurate with your following code too.
@
while(on)
{
...
repaint();
QApplication::processEvents();
}
@ -
wrote on 30 Jul 2013, 02:29 last edited by
[quote author="1+1=2" date="1375086205"]Yes, QTimer is not so precision.
But afaik, you can not make it accurate with your following code too.
@
while(on)
{
...
repaint();
QApplication::processEvents();
}
@[/quote]you mean... processEvents may causes delay?
-
wrote on 30 Jul 2013, 02:40 last edited by
Yes, there are so many things needed to do by it, such as mousePressEvent/mouseMoveEvent/resizeEvent/closeEvent/...
-
wrote on 30 Jul 2013, 02:43 last edited by
[quote author="1+1=2" date="1375152030"]Yes, there are so many things needed to do by it, such as mousePressEvent/mouseMoveEvent/resizeEvent/closeEvent/...[/quote]
do you have any better ways?
-
wrote on 30 Jul 2013, 02:45 last edited by
[quote author="1+1=2" date="1375152030"]Yes, there are so many things needed to do by it, such as mousePressEvent/mouseMoveEvent/resizeEvent/closeEvent/...[/quote]
can i set events' priority for reflashing screen immediately?
-
What precision do you need?
[quote author="eiao" date="1375152344"]can i set events' priority for reflashing screen immediately?[/quote]You can set event priorities (see "Qt::EventPriority":http://qt-project.org/doc/qt-5.1/qtcore/qt.html#EventPriority-enum and "QCoreApplication::postEvent()":http://qt-project.org/doc/qt-5.1/qtcore/qcoreapplication.html#postEvent). But, high-priority events can only "jump queue" if the other events are still waiting to be processed. They cannot interrupt an event that has already started getting processed.
If your application has already started processing a low-priority event when a high-priority event arrives, your new high-priority event must still wait for the first one to finish.
1/13