Results 1 to 5 of 5

Thread: Questions about slot execution.

  1. #1
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Questions about slot execution.

    Assumme I have a qapplication with a qmainwindow.
    In the qmainwindow is a timer emitting the signal timeout(). This signal is connected to a slot computeAlot() which does a lot of computing.

    Something like this:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainWindow.h"
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication application(argc, argv);
    6. MainWindow mainWindow(0);
    7. mainWindow.show();
    8. return application.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 
    mainWindow.h:
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QTest>
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit MainWindow(QWidget * parent = 0);
    8. private slots:
    9. void computeAlot();
    10. }
    To copy to clipboard, switch view to plain text mode 
    mainWindow.cpp
    Qt Code:
    1. #include "mainWindow.h"
    2. MainWindow::MainWindow(QWidget *parent)
    3. {
    4. const int updateRate = 10; //ms
    5. const Qt::ConnectionType connectionType = Qt::QueuedConnection;
    6. QTimer timer;
    7. connect(timer, SIGNAL(timeout()), this, SLOT(computeAlot()), connectionType);
    8. timer.start(updateRate);
    9. }
    10. MainWindow::ComputeAlot()
    11. {
    12. const int computationTime = 500; //ms
    13. // Should do some computions here
    14. QTest::qSleep(computationTime);
    15. }
    To copy to clipboard, switch view to plain text mode 

    If the computationTime is higher than the updateRate how often will ComputeAlot() be executed if the timer stops after 100ms? Is it executed 10 times or one time or some threshold times?
    Is this behavior dependent of the connectionType?
    Would there be a different behavior if the ComputeAlot() would be executed in a different thread?

    And if I want the computeAlot() slot be executed as often as possible while the GUI stays responsive, how do i do that? I discovered if I set the updateRate to 0 the GUI is not nicely responsive.

    Thanx

  2. #2
    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: Questions about slot execution.

    Timers are checked when the event loop processes all its events. Thus if you have a timer set to 100ms but there is something that prevents the event loop from processing its events (like your heavy computation) then it won't check the timer, thus the best interval you can get in a single thread is the time of the heavy computation + some additional delay for processing other events. Thus with 100ms timer and 500ms computation, you will get an average of 120 triggers per minute.

    If you use multiple threads then signals will be emitted using the same rule as mentioned above, but slot executions in a different thread will be queued and you'll get lags. Thus your slot will still be executed about 120 per minute but you can run out of memory (as signals will be getting stacked in the event loop) or after you stop the timer in the first thread, the other thread will still take a lot of time to catch up.

    If you want "as often as can be" semantics, use 0 timers. Your gui will still be lagging (because you'll be blocking the event loop for 500ms all the time) though. Alternatively read this article: [wiki]Keeping the GUI Responsive[/wiki] and apply the step-by-step approach.
    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.


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

    skimmer (13th September 2012)

  4. #3
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about slot execution.

    Thank you.

    Thus if you have a timer set to 100ms but there is something that prevents the event loop from processing its events (like your heavy computation) then it won't check the timer
    So it will also block other events, as soon as the timer executes the slot. And the timer will not emit the signal as soon as it would be time for it. The signal will be emitted only every 500ms (also not every 600ms)

    Thus with 100ms timer and 500ms computation, you will get an average of 120 triggers per minute.
    Okay clear, 120/min => 500ms period.

    If you want "as often as can be" semantics, use 0 timers. Your gui will still be lagging (because you'll be blocking the event loop for 500ms all the time) though. Alternatively read this article: Keeping the GUI Responsive and apply the step-by-step approach.
    I will read that article first.

  5. #4
    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: Questions about slot execution.

    Quote Originally Posted by skimmer View Post
    The signal will be emitted only every 500ms (also not every 600ms)
    It will be emitted not more often than every 500ms. It could be 500, 505, 600 or even 1E+10ms.
    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.


  6. #5
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Questions about slot execution.

    Good.
    With the proposed workerThread solution its simply solved in the easy case above. Here is my test code:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MainWindow(QWidget * parent = 0);
    6. private slots:
    7. void computeAlot();
    8. private:
    9. int m_computationTime;
    10. };
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. m_computationTime(500)
    14. {
    15. const int totalRunTime = 5000;//ms
    16. const int updateRate = 100; //ms
    17. const Qt::ConnectionType connectionType = Qt::QueuedConnection; //Qt::DirectConnection
    18. QTimer * timer = new QTimer;
    19. WorkerThread * workerThread = new WorkerThread;
    20. workerThread->m_computationTime = m_computationTime;
    21.  
    22. connect(timer, SIGNAL(timeout()), workerThread, SLOT(start()), connectionType);
    23. //connect(timer, SIGNAL(timeout()), this, SLOT(computeAlot()), connectionType);
    24.  
    25. timer->start(updateRate);
    26. QTimer::singleShot(totalRunTime, timer, SLOT(stop()));
    27. }
    28. void MainWindow::computeAlot()
    29. {
    30. qDebug("Computig a lot ...");
    31. QTest::qSleep(m_computationTime);
    32. qDebug(" ...Computed a lot");
    33. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class WorkerThread : public QThread
    2. {
    3. public:
    4. void run();
    5. int m_computationTime; //ms
    6. };
    7. void WorkerThread::run()
    8. {
    9. qDebug("Computig a lot in a thread ...");
    10. QTest::qSleep(m_computationTime);
    11. qDebug(" ...Computed a lot");
    12. }
    To copy to clipboard, switch view to plain text mode 

    Where the "computation" is done 9 times and the GUI is responsive all the time.

Similar Threads

  1. One question about the execution of the slot function
    By charlse in forum Qt Programming
    Replies: 3
    Last Post: 26th April 2012, 10:21
  2. Improper execution!
    By dreamh4k in forum Newbie
    Replies: 1
    Last Post: 19th September 2009, 02:08
  3. Signal/Slot execution sequences/preemption
    By xenome in forum Qt Programming
    Replies: 8
    Last Post: 17th September 2009, 22:40
  4. execution in QT
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2009, 08:12

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.