0

When we call QApplication::exec() at the end of our main() function, the application enters Qt's event loop. Conceptually, the event loop looks like this:

   while (!exit_was_called) {
    while (!posted_event_queue_is_empty) {
        process_next_posted_event();
    }
    while (!spontaneous_event_queue_is_empty) {
        process_next_spontaneous_event();
    }
    while (!posted_event_queue_is_empty) {
        process_next_posted_event();
    }
}

First, the event loop processes any posted events until the queue is empty. Then, it processes the spontaneous events until there are none left. Finally, it processes any posted events that were generated during the processing of the spontaneous events.

So A single thread (main() thread in Single threaded GUI application) can not handle program as well as Event loop(With Event Queue functionality). Is there any threads other than main created for handling Events in QT?

1

an event queue is perfectly capable of handling the core program without starving the spontaneous events simply by creating a new event to be handled next and returning back to the loop

in code this is likely:

Q_INVOKABLE void do_computation(int step=0){
    switch(step)
    {
        case 0:
        //do some computation
            QMetaObject.invokeMethod(this,"do_computation",Qt::QueuedConnection, Q_ARG(int,1));
            break;
        case 1:
        //continue the computation
            QMetaObject.invokeMethod(this,"do_computation",Qt::QueuedConnection, Q_ARG(int,2));
            break;
        default: return;
    }

}

using QueuedConnection delays the dispatch until the next time the loop runs; possibly allowing other events to be handled first

for further reading I suggest reading the article on the Qt wiki about threads and events

  • please correct me if I am wrong. While executing a set a of codes, when a event generated from OS, the control pause the execution and go to other portion to make a Event Queue and come back again to the part where it had left its control. – sk patra Sep 4 '13 at 5:49
  • no the current event is finished first and then the OS event is handled – ratchet freak Sep 4 '13 at 6:17
  • That means OS is responsible for making Event Queue and dispatch to the respective thread,when it is available. – sk patra Sep 4 '13 at 7:42
  • no the OS is responsible for sending messages to the listener designated by the program, that listener will dispatch to the queue – ratchet freak Sep 4 '13 at 11:37
  • My point is thread is smallest unit of processing used for a task.According to your comment, the listener is a part of the program and add the event in queue.How it is done if the thread is performing some other task(executing some code) and in the mean time OS sends message?If there is nothing to listen(the thread is busy else where),Do the OS message wait for the listener to listen?What if at the mean time another message comes?Does OS has also mechanism to queue the messages if Listener is not listening? – sk patra Sep 4 '13 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.