Results 1 to 16 of 16

Thread: QMessageBox in a second thread?

  1. #1
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QMessageBox in a second thread?

    Hi all,

    I have a second thread outside the main (GUI) thread, this (second) thread copys a directory with all subdirs and all files. If a file is exists, I have a (modal) QMessageBox for asking the user to override or not. But it comes:

    QObject::setParent: Cannot set parent, new parent is in a different thread
    QPixmap: It is not safe to use pixmaps outside the GUI thread
    How can I do this?

    Thx!
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMessageBox in a second thread?

    You can't use widgets in worker threads. Communicate with the main thread (e.g. using signals) and show the message box from there.
    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
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    Ok, but after emits the signal, the copy routines continues! It's must wait for users choise (override or not)!

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox in a second thread?

    Then you need to wait for the response from the gui thread before you continue. You probably have a list of files to work through, so just keep the index and resume from where you left off when you get the appropriate signal from the main thread.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMessageBox in a second thread?

    Or use the signal semantics that will halt the execution of the worker thread until all the slots are executed.
    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. #6
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    This works, thanks! ...but the return value is always the same!?

    Slot in GUI thread:
    Qt Code:
    1. int overrideQuestion(const QString fileName) {
    2. return QMessageBox::question(this, tr("Override?"), tr("File '%1' already exists! Override?").arg(fileName), QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll);
    3. }
    To copy to clipboard, switch view to plain text mode 
    In copy thread:

    Qt Code:
    1. signals:
    2. int overrideQuestion(const QString);
    3.  
    4. (...)
    5.  
    6. connect(this, SIGNAL(overrideQuestion(QString)), parent, SLOT(overrideQuestion(QString)), Qt::BlockingQueuedConnection);
    7.  
    8. (...)
    9.  
    10. int result = emit overrideQuestion(fileInfo.fileName());
    To copy to clipboard, switch view to plain text mode 

    result is always "267386880"!?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMessageBox in a second thread?

    Return values from slots are ignored.
    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.


  8. #8
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    Ok, but how can I do this with an int return value?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMessageBox in a second thread?

    You can use a shared variable, for example.
    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.


  10. #10
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    Sorry, I don't understand it! Variable as return value???

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox in a second thread?

    The slot will not return anything. You either need to store the variable elsewhere or you need to send a signal to the thread to indicate the return value. I would use a state machine in the thread and use a signal to indicate the return value (ie, the thread would change to paused state when the notification is raised and resume upon receiving the response from the user).

    You might have guessed that I hate blocking code, so everything runs via events and signals

    I've written a file manager before that goes one step further - if it needs a response from the user (eg. overwrite question), it sends a signal to the main thread to request about that file and processes the other files in the background, returning to the other files if and when a response is received from the main thread about those files.
    Last edited by squidge; 23rd May 2011 at 17:30.

  12. #12
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    store variable elsewhere .. can you give me an example with my above code please!?

  13. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox in a second thread?

    eg. you store the result from the message dialog in a variable that both threads have access it. I don't think you really need an example for that.

    But I would say that the method I described above is a better idea and keeps encapsulation.

  14. #14
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    Ok, but how can I paused the worker thread?

  15. #15
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMessageBox in a second thread?

    You don't "pause" it, you simply return to your threads event loop waiting for something to happen (such as a signal from the main ui to continue). When you receive this signal, you continue, using the context information you stored in your thread when you returned to the event loop.

    I assume you store your filenames in a vector or similar, so all you need to do is store an iterator to the element you were parsing when you emitted the message box request, then you restart from there once you receive the information back from the main thread.

  16. #16
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox in a second thread?

    Ok, thanks to all for your help!

Similar Threads

  1. Replies: 5
    Last Post: 22nd February 2011, 21:21
  2. Replies: 7
    Last Post: 27th January 2011, 08:48
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Replies: 16
    Last Post: 7th October 2009, 08:17
  5. QThread, QMessageBox, gui thread
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 25th October 2006, 12:23

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
  •  
Qt is a trademark of The Qt Company.