Results 1 to 5 of 5

Thread: what to do during long calculation?

  1. #1
    Join Date
    Jan 2006
    Location
    Hannover, Germany
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default what to do during long calculation?

    Hi all,

    I have a QSpinBox which is connected to some slot to make some calculations:

    Qt Code:
    1. connect(mySpinBox, SIGNAL(valueChanged(int) ), this, SLOT(calculate(int)));
    To copy to clipboard, switch view to plain text mode 

    The calculation takes some decent amount of time (~5s) and therefore the GUI freezes. Even worse, if I click once on the SpinBox, the value is actually increased 3 or 4 times.
    Any hints on how to solve this ugly behaviour?

    Greets,
    Weilor

  2. #2
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: what to do during long calculation?

    The program freezes since the CPU is busy on an application process, and cannot process the GUI too in the meanwhile.
    To don't make stop the GUI, it is possible to execute calculate(int) on a parallel thread... but you better check if that operation worths the work...
    While for the incrementations, clicks are buffered while the process calculate(int) proceeds, to execute their events at the end of calculate(int). But since you said you click once....well, now I have no idea.

    Hope it helps
    Greetings
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  3. #3
    Join Date
    Jan 2006
    Location
    Hannover, Germany
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: what to do during long calculation?

    Thanks for your answer,
    Is it such an effort to execute some operation in another thread? Maybe you can give me some pseudo-code how to do it?

    Greets,
    weilor

  4. #4
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: what to do during long calculation?

    I am not sure about details of Qt4, but in Qt3 was quite easy.
    You have to inherit a your class from QThread and there override the virtual function run() that is called by the QThread start()method.
    An example could be:
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. private:
    4. . . .
    5. public:
    6. // constructor
    7. virtual void run(); //inside this you execute your calculate(int)
    8. . . .
    9. }
    To copy to clipboard, switch view to plain text mode 

    for more details that sure you need, refer to http://doc.trolltech.com/4.3/qthread.html

    Anyway, calculate from thread is a bit more complicated and could be a risk, since unxpected terminations could crash your program. Complicated because you have to manage the thread result to send it to your application, with some wrappers.
    So I suggest you to well study the thread guide.

    Greetings
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  5. #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: what to do during long calculation?

    You don't have to use threads. You can either ask the queue to process its events somewhere during your calculations using QCoreApplication:rocessEvents() or divide your calculations into smaller chunks and use QTimer::singleShot with 0 timeout to fire next stage at the end of previous stage of calculations.

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

    BatteryCell (25th September 2007)

Similar Threads

  1. winEvent---(MSG * message, long * result)
    By Naveen in forum Qt Programming
    Replies: 4
    Last Post: 29th June 2015, 11:05
  2. QTextEdit loading takes long time
    By sreedhar in forum Qt Programming
    Replies: 12
    Last Post: 21st March 2011, 10:29
  3. Font size calculation when painting in a QImage
    By Ishark in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2007, 22:22
  4. QSqlDatabase Time out no network to long....
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2007, 00:41
  5. how to srcoll a long text in one line from right to left?
    By deweyjew in forum Qt Programming
    Replies: 3
    Last Post: 21st November 2006, 10:10

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.