Results 1 to 10 of 10

Thread: disable close button in QMessageBox

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default disable close button in QMessageBox

    Is it possible to disable close button in QMessageBox? I want to make a question box where only Yes and No will be enabled.

    I tried to find out the needed flags with Qt "windowflags" example, the following combination seems to work for the window in that sample application, but it doesn't work with QMessageBox.

    Qt Code:
    1. Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint
    To copy to clipboard, switch view to plain text mode 

    also tried this with the same effect:
    Qt Code:
    1. setWindowFlags(Qt::FramelessWindowHint);
    2. setWindowFlags(Qt::WindowTitleHint);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: disable close button in QMessageBox

    Disable Qt::WindowCloseButtonHint flag

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: disable close button in QMessageBox

    Qt Code:
    1. QMessageBox mbox(QMessageBox::Question, "my app", tr("Do you want to update?"), QMessageBox::Yes | QMessageBox::No, &w,
    2. Qt::WindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint);
    To copy to clipboard, switch view to plain text mode 

    Doesn't work. Or am I doing something wrong?

  4. #4
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: disable close button in QMessageBox

    Try to remove Qt::WindowSystemMenuHint from the list. On some system it implies close button, too.

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: disable close button in QMessageBox

    no, didn't help either

  6. #6
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: disable close button in QMessageBox

    Bad news: it looks like you will need to implement a special message box from QDialog, because of QMessageBox doesn't let you to restrict close button by its nature.

    Qt Code:
    1. QDialog mbox;
    2. mbox.setWindowFlags(Qt::WindowTitleHint | Qt::Dialog | Qt::WindowMaximizeButtonHint | Qt::CustomizeWindowHint);
    3. mbox.exec();
    To copy to clipboard, switch view to plain text mode 

    It works, but you will also need to restrict key ESC, and put Yes/No buttons manually.

  7. #7
    Join Date
    Jun 2011
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: disable close button in QMessageBox

    I found this thread because I had the same problem, but managed to solve it in a different way.
    Qt Code:
    1. mBox.setWindowTitle("Dialog Title");
    2. mBox.setText("Question?");
    3. mBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    4.  
    5. Qt::WindowFlags wFlags = mBox.windowFlags();
    6. if(Qt::WindowCloseButtonHint == (wFlags & Qt::WindowCloseButtonHint))
    7. {
    8. wFlags = wFlags ^ Qt::WindowCloseButtonHint;
    9. mBox.setWindowFlags(wFlags);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nmahoney; 3rd June 2011 at 15:46.

  8. #8
    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: disable close button in QMessageBox

    The standard way is to allow the user to close the dialog using regular means and interpret this as if he pushed one of the buttons (usually the one related to "rejecting" the dialog -- most likely the "No" button in this example). The same goes with handling the Esc key.
    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.


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

    Default Re: disable close button in QMessageBox

    Quote Originally Posted by wysota View Post
    The standard way is to allow the user to close the dialog using regular means and interpret this as if he pushed one of the buttons (usually the one related to "rejecting" the dialog -- most likely the "No" button in this example). The same goes with handling the Esc key.
    Agree. Subverting normal UI conventions is very bad form. The user expects things to work as they always have, and will be frustrated if they don't.

  10. #10
    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: disable close button in QMessageBox

    To explain further what I mean...

    Usually you want to disable the close button because you don't want the user to be able to close the window. Here the situation is different -- you want him to close the window (which will happen if he chooses a button from the dialog box) but only using the means chosen by you and not any other. There is no reason to do that.
    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.


Similar Threads

  1. Replies: 2
    Last Post: 26th April 2011, 11:44
  2. Disable Close button (X) of a QDialog
    By BrainB0ne in forum Qt Programming
    Replies: 29
    Last Post: 8th December 2009, 17:01
  3. Disable close button on QTabWidget/QTabBar
    By minimoog in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2009, 05:25
  4. how to disable X button to close th window
    By raghvendramisra in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2009, 20:01
  5. About QMessageBox close my tray application
    By Doles in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2008, 21:27

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.