Results 1 to 10 of 10

Thread: Non-Blocking QMessageBox

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

    Default Non-Blocking QMessageBox

    I'm trying to display a message box but I do not want it to block, How can I accomplish this.

    Qt Code:
    1. QMessageBox msgBox(this);
    2. msgBox.setWindowTitle("Loading ....");
    3. msgBox.setText("Reading Files, please wait ...");
    4. msgBox.open();
    5. // Run Stuff
    6. msgBox.close();
    To copy to clipboard, switch view to plain text mode 

    If I use open() or show() the messagebox never shows up. If I use exec(), it does show up, but it blocks.

    The only way I got something like this to works is if I use ProgressDialog but then I need to use the progress bar. Unfortunately the task am doing is a shell call which provides no status.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Non-Blocking QMessageBox

    QMessageBox is explicitly modal. If you want a dialog that doesn't block, use QDialog.

    I question whether you really want this. Your tells the user that files are being loaded, then explicitly says "please wait". Apparently, you don't really want them doing anything until those files are read.

    If you use a QDialog and allow the user to fiddle with the rest of your interface, make sure you disable any controls that rely on the files being completely read until after IO completes.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Non-Blocking QMessageBox

    Just do the following modification, you should be all set. You should be calling show (instead of open), also required to modify the modality of the message box before showing it

    Qt Code:
    1. QMessageBox msgBox(this);
    2. msgBox.setWindowTitle("Loading ....");
    3. msgBox.setText("Reading Files, please wait ...");
    4. msgBox.setWindowModality(Qt::NonModal);
    5. //msgBox.open();
    6. msgBox.show();
    7. // Run Stuff
    8. msgBox.close();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Blocking QMessageBox

    I have tried using "show()" however no window shows up and the code continues.

    Only "exec()" causes a window to show up, but it blocks.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Non-Blocking QMessageBox

    Make sure QMessageBox has a widget (terminal widget) as parent. Just try using the above code in a simple project, it should work, then compare it with what you do in your project.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,539
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Blocking QMessageBox

    Quote Originally Posted by enricong View Post
    I have tried using "show()" however no window shows up and the code continues.

    Only "exec()" causes a window to show up, but it blocks.
    Problem is line 7 (Run Stuff). I think that this line blocks event que and all graphics processes needs working event que. Try to add after show() this line :
    Qt Code:
    1. QCoreApplication::processEvents();
    To copy to clipboard, switch view to plain text mode 
    Remember that this is workaround not the solution.
    Last edited by Lesiok; 25th July 2011 at 15:35.

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

    Default Re: Non-Blocking QMessageBox

    Yes, this is the problem.
    Adding that line allowed the window to show up, although the text in the window did not show up.

    "Run Stuff" is actually a shell call which provides no progress feedback.
    Perhaps I need to run it in its own thread.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Non-Blocking QMessageBox

    Adding that line allowed the window to show up, although the text in the window did not show up.
    That is the same issue as you had before - processEvents() allows some events to be handled (like the MessageBox to be shown) but your "Run Stuff" then stopped it again, before the text could be drawn.
    Perhaps I need to run it in its own thread.
    That would probably be better in this case.
    See QConcurrent, it sounds like its perfect for this case.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Blocking QMessageBox

    Thanks, I'll have to try to figure out how to modify my program to work with "QtConcurrent"
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,539
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Blocking QMessageBox

    Quote Originally Posted by enricong View Post
    Yes, this is the problem.
    Adding that line allowed the window to show up, although the text in the window did not show up.

    "Run Stuff" is actually a shell call which provides no progress feedback.
    Perhaps I need to run it in its own thread.
    On X-11 platform You need to call processEvents() 4-5 times. On Windows one call is enough.

Similar Threads

  1. non blocking QTcpSocket
    By SIFE in forum Qt Programming
    Replies: 2
    Last Post: 1st June 2011, 02:06
  2. Non-Gui thread blocking gui
    By Valheru in forum Qt Programming
    Replies: 17
    Last Post: 12th February 2010, 11:11
  3. Non blocking Gui Qthread in qt3
    By triperzonak in forum Newbie
    Replies: 2
    Last Post: 13th September 2008, 14:06
  4. ports blocking
    By babu198649 in forum General Programming
    Replies: 3
    Last Post: 4th August 2008, 15:51
  5. Blocking ftp
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 21st December 2007, 08:18

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.