Results 1 to 9 of 9

Thread: exec QMessageBox

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default exec QMessageBox

    is there anyway not to lose focus of QMessageBox to Mainwindow unless it is closed first?

    Qt Code:
    1. QMessageBox msgBox;
    2. msgBox.setText(trUtf8(""));
    3. msgBox.setInformativeText"");
    4. QAbstractButton *myYesButton = msgBox.addButton(trUtf8(" "), QMessageBox::YesRole);
    5. QAbstractButton *myNoButton = msgBox.addButton(trUtf8(" "),QMessageBox::NoRole);
    6. msgBox.setIcon(QMessageBox::Question);
    7.  
    8. msgBox.exec(); //???
    9.  
    10. if(msgBox.clickedButton() == myNoButton)
    11. {
    12. qDebug() << "no";
    13. return;
    14. }
    15. else
    16. {
    17. system("sudo shutdown -hf now");
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: exec QMessageBox

    You are opening the QMessageBox as a modal dialog. It will take the focus for the entire application until it is closed. That seems to be exactly what you are asking for.

    If you want a modeless dialog call show() instead of exec() and be to respond to button clicks asynchronously in connected slots.

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: exec QMessageBox

    Quote Originally Posted by ChrisW67 View Post
    You are opening the QMessageBox as a modal dialog. It will take the focus for the entire application until it is closed. That seems to be exactly what you are asking for.
    no, when I click the mainwindow, it loses focus and goes to background. Besides, it is run as a separate program.

    but in the case
    Qt Code:
    1. reply = QMessageBox::question(this, tr(" "), " ", QMessageBox::Yes | QMessageBox::No);
    To copy to clipboard, switch view to plain text mode 

    it is run in the same MainWindow program and does not lose focus when mainwindow is clicked, unless we choose yes or no.

    I cannot use the second second because I need to translate Yes and No.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: exec QMessageBox

    Besides, it is run as a separate program.
    What did you expect to happen... The second program to magically know which other program to block and block it?

  5. #5
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: exec QMessageBox

    Quote Originally Posted by ChrisW67 View Post
    What did you expect to happen... The second program to magically know which other program to block and block it?
    yes, I understand.

    I am looking for a way to make it look like

    Qt Code:
    1. reply = QMessageBox::question(this, tr(" "), " ", QMessageBox::Yes | QMessageBox::No);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: exec QMessageBox

    I have no idea why you would have program 1 launch program 2 just to display a dialog when there is a clear an obvious way to use the dialog inline... but it's your party.

    To achieve what you are asking you would need the launching program to call setDisabled(true) on its top-level window, launch the second program, and wait from the termination of the called program before calling setEnabled(true). The second program should be written to be always on-top. The Qt::WindowStaysOnTopHint window flag is one way you might be able to do this.

  7. #7
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: exec QMessageBox

    Quote Originally Posted by ChrisW67 View Post
    I have no idea why you would have program 1 launch program 2 just to display a dialog when there is a clear an obvious way to use the dialog inline... but it's your party.

    To achieve what you are asking you would need the launching program to call setDisabled(true) on its top-level window, launch the second program, and wait from the termination of the called program before calling setEnabled(true). The second program should be written to be always on-top. The Qt::WindowStaysOnTopHint window flag is one way you might be able to do this.
    thanks for the way you mentioned. about the first 2 lines, I do not want the first program to launch the second. But, unfortunately, the way the code behaves is like that.

  8. #8
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: exec QMessageBox

    i created a custom dialog with class. initialized new dialog instance in mainwindow constructor. when button clicked, I show the dialog. before that I disable the mainwindow. inside new dialog class before closing it, I signal mainwindow to get enabled.
    the problem is when mainwindow sets to disabled, the buttons inside the custom dialog get disabled as well! and the close button top-left side of custom dialog onlyworks.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: exec QMessageBox

    Now your dialog is part of the same application! Make up your mind!

    My first response applies and you don't need to do anything special: a modal dialog will block all input to the remainder of the application it belongs to. If the dialog has a parent then it will stay on top of that parent.

Similar Threads

  1. Qt exec bug
    By waiter in forum Newbie
    Replies: 1
    Last Post: 7th April 2013, 08:17
  2. Replies: 4
    Last Post: 30th August 2012, 23:28
  3. WA_DeleteOnClose and exec()
    By Wasabi in forum Newbie
    Replies: 6
    Last Post: 7th August 2011, 05:54
  4. app.exec() never returns?
    By stealth86 in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2007, 18:41
  5. How to use QThread::exec()
    By intensifi in forum Qt Programming
    Replies: 4
    Last Post: 10th February 2006, 16:00

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.