Results 1 to 16 of 16

Thread: Any alternatives to QThread and QProcess for blocking operations in main GUI thread?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    Then what is the right design pattern for thing as simple as opening of new window? Consider this code:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent* keyEvent)
    2. {
    3. if (keyEvent->key() == Qt::Key_F1)
    4. {
    5. HelpDialog help(this);
    6. help.exec();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    If in MainWindow you press quickly F1 three times, the first QKeyEvent will cause to exec HelpDialog, but the other two events will be handled in HelpDialog, which is totally unacceptable, because it may cause some random actions that user is not aware of. Please don't say that it is pointless to press one button three times and that noone will do it, because you can do it just by accident. But there is a better reason for doing such a thing than accident. When process of displaying new window lasts long (let's say 1s) user doubts if the keyboard/application detected if key had been pressed and then he presses it again.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    statemachine. Like I already said, back in post 2!

    for this simple example you would have three states.

    1) waiting
    2) starting
    3) running

    You can only go from state to state in order: 1->2->3->1 etc.

    Default state is waiting. As soon as f1 is pressed, state is changed to 2). In state 2), keypress does nothing.

    State 2 does the making of new form and exec(). As soon as exec is called, state is changed to running.

    As soon as exec finishes, state changes back to 1.
    Last edited by amleto; 23rd December 2011 at 15:12.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    If I understand your idea, you want to run exec in QThread. As far as I know QThread is just a worker thread and you cannot use any widgets in it.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    No, it has nothing at all to do with threads. Where did I mention threads? I mentioned state machine, didn't I?

    and you can 'use' any widget you want in a thread, but you can only make widgets in the main thread.
    Last edited by amleto; 23rd December 2011 at 18:34.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    Then what is this state machine for? If you do this in main Gui thread you don't need to remember current state, everything happens consecutively like in my last example of code.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    your code has a problem when user keep pressing buttons. using a statemachine can resolve this problem. Is that the third time I have said it now?
    Last edited by amleto; 23rd December 2011 at 21:09.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    You are still not explaining how state machine could solve this problem.

    I think you are missing the fact that generation and delivery of QKeyEvent does not happen in background. There is no concurrency between construction of window and delivery of QKeyEvents. Here is the flow:

    1. QKeyEvent is dispatched to MainWindow.
    --- in the background user presses another two keys ---
    2. keyPressedEvent constructs HelpDialog.
    3. New event loop is started by calling exec() on modal dialog.
    4. In the new loop Linux keyboard driver's output buffer is checked and two new keys are found. (actually I'm not sur how it happens, that's one of the things I'm trying to figure out).
    5. Two QKeyEvent objects are generated for both of new keys.
    6. The first is dispatched to HelpDialog which has focus now.
    7. Later the second one will be dispatched to widget with focus (who knows which).

    This is how it works (more or less) in my system.

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Any alternatives to QThread and QProcess for blocking operations in main GUI thre

    Sorry, for some reason I have been thinking that you had a problem with too many events sent to the main window.

    Anyway, your problem is purely down the event handler (or something else on the main thread) taking up too much time. It is blocking the main thread from handling events quickly, and that is why a key press on mainwindow is getting delayed and sent to another window.

    Move heavy work out of the gui thread - it should not be there anyway.
    Last edited by amleto; 24th December 2011 at 12:17.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 5
    Last Post: 22nd February 2011, 21:21
  2. Replies: 7
    Last Post: 6th November 2010, 08:32
  3. QThread sends signal to main thread immediatly
    By BIllNo123 in forum Newbie
    Replies: 7
    Last Post: 27th August 2010, 10:32
  4. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06
  5. Determine if QThread::currentThread is main thread?
    By chaoticbob in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2008, 06:26

Tags for this Thread

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.