Results 1 to 6 of 6

Thread: how to abort a function taking to long to process

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Question 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:


    Qt Code:
    1. for (i=0;i<Lots_of_calculation && !kbhit() ;i++)
    2. {
    3. ....
    4. lots of calculation;
    5. blablabla...;
    6. more calculation;
    7. .....
    8. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. for (i=0;i<Lots_of_calculation && flagAbortFunction ;i++)
    2. {
    3. ....
    4. lots of calculation;
    5. blablabla...
    6. more calculation;
    7. .....
    8. }
    9.  
    10. void someClass::keyPressEvent(QKeyEvent *event)
    11. {
    12. flagAbortFunction = false;
    13. QMessageBox(this,"title","Show something to see if the function ended and
    14. ask user input data that process more quicly");
    15. }
    To copy to clipboard, switch view to plain text mode 

    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 ???????

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default 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()

    Qt Code:
    1. loop.processEvents(QEventLoop::AllEvents);
    2. app.processEvents(loop);
    To copy to clipboard, switch view to plain text mode 

    I have the errors:

    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?
    Last edited by wysota; 15th April 2009 at 08:07. Reason: reformatted to look better

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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 View Post
    Qt Code:
    1. for (i=0;i<Lots_of_calculation && !bAbort ;i++) // set bAbort in keyPressEvent or wherever you like
    2. {
    3. ....
    4. lots of calculation;
    5. QCoreApplication::processEvents (); // <-- new
    6. blablabla...;
    7. more calculation;
    8. .....
    9. }
    To copy to clipboard, switch view to plain text mode 
    Ginsengelf
    Last edited by Ginsengelf; 15th April 2009 at 07:03. Reason: messed up tags

  5. The following user says thank you to Ginsengelf for this useful post:

    john_god (30th April 2009)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to abort a function taking to long to process

    Read this article, it might give you some hints:
    Keeping the GUI responsive.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    john_god (30th April 2009)

  8. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default 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...

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. Regading Driver to connect Postgresql Database
    By dummystories in forum Installation and Deployment
    Replies: 38
    Last Post: 12th March 2009, 07:19
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  4. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.