Some questions about loop in QWidget
-
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 -
Hi, you can use a QTimer to periodically do something, instead of using while loop.
-
[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.
-
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 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.
-
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 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...
-
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 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?
-
Yes, there are so many things needed to do by it, such as mousePressEvent/mouseMoveEvent/resizeEvent/closeEvent/...
-
[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?
-
[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.