how to abort a function taking to long to process
Back in Dos, when I had a long time consuming function (depending on the user
input data) and I wanted to give the user the change to abort that function so
he could alter the input data without having to wait for the end of calculation,
I would do something like this:
Code:
for (i=0;i<Lots_of_calculation && !kbhit() ;i++)
{
....
lots of calculation;
blablabla...;
more calculation;
.....
}
the kbhit() would terminate the for, if the user pressed a key.
This did not work in the actual Qt, so I tried something like:
Code:
for (i=0;i<Lots_of_calculation && flagAbortFunction ;i++)
{
....
lots of calculation;
blablabla...
more calculation;
.....
}
void someClass
::keyPressEvent(QKeyEvent *event
) {
flagAbortFunction = false;
QMessageBox(this,
"title",
"Show something to see if the function ended and ask user input data that process more quicly");
}
this code waits about one minute or so after the keypressed , and only then display
the message box, so it's also not working, because doenst end the 'for'. It seems that
the event keypressed is only processed after the function terminate, because the function
displays all the data, without signs of being interrupted.
Any ideas how how can I achieve my goal ???????
Re: how to abort a function taking to long to process
During your function no events are processed (unless you call QCoreApplication::processEvents()).
An alternative is to put your function inside a QThread.
Re: how to abort a function taking to long to process
Hi Caduel
I try this in the begginning of the function before the for()
Code:
app.processEvents(loop);
I have the errors:
Quote:
D:/qt4examples/Matematica/matriz.cpp:623: error: no matching function for call to `QCoreApplication::QCoreApplication()'
c:/Qt/2009.01/qt/include/QtCore/../../src/corelib/kernel/qcoreapplication.h:72: note: candidates are: QCoreApplication::QCoreApplication(const QCoreApplication&)
Can you help me on this?
Re: how to abort a function taking to long to process
QCoreApplication::processEvents is static. Just add a call to your for loop:
Quote:
Originally Posted by
john_god
Code:
for (i=0;i<Lots_of_calculation && !bAbort ;i++) // set bAbort in keyPressEvent or wherever you like
{
....
lots of calculation;
blablabla...;
more calculation;
.....
}
Ginsengelf
Re: how to abort a function taking to long to process
Read this article, it might give you some hints:
Keeping the GUI responsive.
Re: how to abort a function taking to long to process
Hi
I did has you told Ginsengelf and it works very good.
Wysota, I read your article, and I am sticking with the easy solution, because it suits my needs, this issue is more vast then I supposed. I am still a newbie, I never have used thread's, but I will get there...;)