Results 1 to 4 of 4

Thread: How to query the user for input from outside the Qt Message Loop

  1. #1
    Join Date
    May 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default How to query the user for input from outside the Qt Message Loop

    Hi,

    Qt suggests to run computationally expensive tasks in dedicated worker threads instead of SLOTs itself. How do you go about the problem of doing things like showing message boxes to the user and query for their input? The only solution I can think of is to emit a SIGNAL from within the worker thread to a class which shows the MessageBox and emits back a SIGNAL about the user's choice. This however seems overly complicated and messes up my code and control flow with additional sigslot connections. Is there a nicer solution to this or at least some way to abstract this away in a wrapper class somehow?

    I wonder how others go about this.

  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: How to query the user for input from outside the Qt Message Loop

    You can use either signals or events or you can resign from using threads in this code in favor of some other approach (e.g. dividing the task into subtasks and doing them separately to avoid blocking the event loop for too long).
    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. #3
    Join Date
    May 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to query the user for input from outside the Qt Message Loop

    Yes, dividing into subtasks or doing sth similar is probably the easiest way to go in most cases but it's not always possible, especially when talking about legacy code. And lets say we have sth like this then:

    Qt Code:
    1. void slot_myAlgorithm()
    2. {
    3. // spawns thread and joins at its end
    4. if(!runSubtask1())
    5. showError();
    6.  
    7. if(!runSubtask2())
    8. showError();
    9.  
    10. if(!runSubtask3())
    11. showError();
    12. }
    To copy to clipboard, switch view to plain text mode 

    What if another developer wants to reuse this entire algorithm pipeline from inside another thread? Then he again runs into this threading trouble and either has to restructure or copy existing code both of which should be avoided imo.

    And as another example: What if I'm using an std::thread which requires a static run function where I can't emit any signals from because it's not a QObject?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to query the user for input from outside the Qt Message Loop

    You can always use QMetaObject::invokeMethod() to call a slot in a QObject in another thread (the function can be given the necessary Qt::QueuedConnection flag).

    Alternatively to the signalling back you could just block the worker thread and wait for the UI thread to call a method that passes the response.
    E.g. emit the signal and calling acquire on a binary semaphore.
    When the UI has the user's answer, it calls a setter method which calls release() on the semaphore after setting the value.

    The worker thread can then continue reading the value.

    Cheers,
    _

Similar Threads

  1. ask user input in a while loop
    By Bennieboj in forum Newbie
    Replies: 5
    Last Post: 27th November 2011, 15:28
  2. How to get user-defined message?
    By ponponfish in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2011, 17:19
  3. How can I find Qt's message loop?
    By Cayan in forum Newbie
    Replies: 5
    Last Post: 4th August 2010, 22:04
  4. User Input using QString
    By kumarpraveen in forum Newbie
    Replies: 4
    Last Post: 9th July 2010, 08:49
  5. Replies: 10
    Last Post: 5th August 2009, 12:53

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.