Results 1 to 20 of 30

Thread: Disable Close button (X) of a QDialog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Netherlands
    Posts
    56
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by marcel View Post
    Do you get any errors or it just doesn't work?

    See http://doc.trolltech.com/3.3/qt.html#WidgetFlags-enum.
    Play with different combinations until you get some result. But it really depends on the window manager to respect the flags.

    With the Winapi code I first get the following errors:

    Qt Code:
    1. #ifdef Q_WS_WIN
    2.  
    3. CMenu* menu = m_pParent->GetSystemMenu(FALSE);
    4. menu->ModifyMenu(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED );
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 
    : error C2065: 'CMenu' : undeclared identifier
    : error C2065: 'menu' : undeclared identifier
    : error C2039: 'GetSystemMenu' : is not a member of 'QWidget'
    : see declaration of 'QWidget'
    : error C2227: left of '->ModifyMenu' must point to class/struct/union
    : error C2065: 'SC_CLOSE' : undeclared identifier
    : error C2065: 'MF_BYCOMMAND' : undeclared identifier
    : error C2065: 'MF_GRAYED' : undeclared identifier


    After that i include the <afxwin.h> include file (for CMenu class)
    Then the following errors show up:
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE\afx.h(226) : warning C4005: 'ASSERT' : macro redefinition
    C:\Qt\3.3.4\include\qglobal.h(1010) : see previous definition of 'ASSERT'
    : error C2039: 'GetSystemMenu' : is not a member of 'QWidget'
    C:\Qt\3.3.4\include\qwidget.h(60) : see declaration of 'QWidget'
    ..:: Still Standing Strong ::..

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Disable Close button (X) of a QDialog

    That's MFC, not WinAPI...
    Qt Code:
    1. #include <QtGui>
    2. #ifdef Q_WS_WIN
    3. #include <qt_windows.h>
    4. #endif // Q_WS_WIN
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9. QWidget window;
    10.  
    11. #ifdef Q_WS_WIN
    12. HMENU menu = ::GetSystemMenu(window.winId(), FALSE);
    13. ::DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
    14. ::EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
    15. // or (removes the item in of system menu)
    16. // ::DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
    17. #endif // Q_WS_WIN
    18.  
    19. window.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    BrainB0ne (10th October 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Netherlands
    Posts
    56
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by jpn View Post
    That's MFC, not WinAPI...
    Qt Code:
    1. #include <QtGui>
    2. #ifdef Q_WS_WIN
    3. #include <qt_windows.h>
    4. #endif // Q_WS_WIN
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9. QWidget window;
    10.  
    11. #ifdef Q_WS_WIN
    12. HMENU menu = ::GetSystemMenu(window.winId(), FALSE);
    13. ::DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
    14. ::EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
    15. // or (removes the item in of system menu)
    16. // ::DeleteMenu(menu, SC_CLOSE, MF_BYCOMMAND);
    17. #endif // Q_WS_WIN
    18.  
    19. window.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Woooow! That did the job! I'm really happy with this solution
    ..:: Still Standing Strong ::..

  5. #4
    Join Date
    Jan 2006
    Posts
    14
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable Close button (X) of a QDialog

    Hi guys,
    I'm quite late on this thread but I had the same problem :-)

    Anybody knows how to disable that nasty little button on X11 and Mac OS X??

    In case you need it, here is a nice macro I am using on Win32 - it's pretty similar to the latest post of course and it works fine:

    Qt Code:
    1. # ifdef Q_OS_WIN
    2. # include <windows.h>
    3. # define ENABLE_CLOSE_BTN(Enable) \
    4. { QWidget* tlw = this; \
    5. while (tlw && !tlw->isWindow() && tlw->windowType() != Qt::SubWindow) \
    6. tlw = tlw->parentWidget(); \
    7. HMENU hMenu = GetSystemMenu((HWND) tlw->winId(), FALSE); \
    8. EnableMenuItem(hMenu, SC_CLOSE, Enable ? (MF_BYCOMMAND | MF_ENABLED) : (MF_BYCOMMAND | MF_GRAYED)); }
    9. # endif // Q_OS_WIN
    To copy to clipboard, switch view to plain text mode 

    Now just use ENABLE_CLOSE_BTN(true) or ENABLE_CLOSE_BTN(false).

    It will detect the top level widget of the widget containing the code, so you can use it inside of a child widget (eg. a wizard page in my case) without having to change the macro
    Feel free to replace that "while" loop if you are always going to use it inside of a top level window.

    And now on with your suggestions for X11/MAC

  6. The following user says thank you to blue.death for this useful post:

    GokuH12 (11th March 2009)

  7. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Disable Close button (X) of a QDialog

    How do I hide close, resize and minimize featuers in QGLWidget??
    Qt Code:
    1. setWindowFlags(Qt::FramelessWindowHint);
    To copy to clipboard, switch view to plain text mode 
    does not work!!
    Qt 5.3 Opensource & Creator 3.1.2

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by MarkoSan View Post
    How do I hide close, resize and minimize featuers in QGLWidget??
    Qt Code:
    1. setWindowFlags(Qt::FramelessWindowHint);
    To copy to clipboard, switch view to plain text mode 
    does not work!!
    What do you mean? Are you showing QGLWidget as a top level window? QGLWidget is a QWidget so when it comes to adjusting window flags, anything that works for a plain QWidget should work with QGLWidget as well.
    J-P Nurmi

  9. #7
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by blue.death View Post
    Hi guys,
    I'm quite late on this thread but I had the same problem :-)

    Anybody knows how to disable that nasty little button on X11 and Mac OS X??

    In case you need it, here is a nice macro I am using on Win32 - it's pretty similar to the latest post of course and it works fine:

    Qt Code:
    1. # ifdef Q_OS_WIN
    2. # include <windows.h>
    3. # define ENABLE_CLOSE_BTN(Enable) \
    4. { QWidget* tlw = this; \
    5. while (tlw && !tlw->isWindow() && tlw->windowType() != Qt::SubWindow) \
    6. tlw = tlw->parentWidget(); \
    7. HMENU hMenu = GetSystemMenu((HWND) tlw->winId(), FALSE); \
    8. EnableMenuItem(hMenu, SC_CLOSE, Enable ? (MF_BYCOMMAND | MF_ENABLED) : (MF_BYCOMMAND | MF_GRAYED)); }
    9. # endif // Q_OS_WIN
    To copy to clipboard, switch view to plain text mode 

    Now just use ENABLE_CLOSE_BTN(true) or ENABLE_CLOSE_BTN(false).

    It will detect the top level widget of the widget containing the code, so you can use it inside of a child widget (eg. a wizard page in my case) without having to change the macro
    Feel free to replace that "while" loop if you are always going to use it inside of a top level window.

    And now on with your suggestions for X11/MAC
    Woo so good! thx for the nice macro! its works just nice

  10. #8
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    16
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    Starting with Qt 4.5 there is a new window hint for controlling close button Qt::WindowCloseButtonHint:

    Qt Code:
    1. w.setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint| Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonHint);
    To copy to clipboard, switch view to plain text mode 

  11. The following 2 users say thank you to shad for this useful post:

    BrainB0ne (28th April 2009), Cupidvogel (7th March 2016)

  12. #9
    Join Date
    Jan 2006
    Location
    Netherlands
    Posts
    56
    Thanks
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by shad View Post
    Starting with Qt 4.5 there is a new window hint for controlling close button Qt::WindowCloseButtonHint:

    Qt Code:
    1. w.setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint| Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonHint);
    To copy to clipboard, switch view to plain text mode 
    That's very nice to hear/know when i'm going to use Qt 4.5 or higher in the future!
    ..:: Still Standing Strong ::..

  13. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by BrainB0ne View Post
    That's very nice to hear/know when i'm going to use Qt 4.5 or higher in the future!
    Very nice, thanks, but, is it possible to disable (X) in dialog that resided on QGraphicsView, I've read in the docs that is NOT possible ...
    Qt 5.3 Opensource & Creator 3.1.2

  14. #11
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Disable Close button (X) of a QDialog

    I use the same code in QT4.5, but it doesn't work!!
    why??

  15. #12
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by blue.death View Post
    Anybody knows how to disable that nasty little button on X11 and Mac OS X??
    I have the exact same problem. I'm using Qt 4.3 and don't have the luxury of Qt::WindowMinMaxButtonHint.

    I need to keep the title bar empty, so Qt::FramelessWindowHint makes the message box a bit bare.

    It was suggested that we can inherit a QDialog box and disable it there...would anyone else know of any other suggestions?

    Thanks,
    Kat

  16. #13
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    16
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by kyue View Post
    It was suggested that we can inherit a QDialog box and disable it there...would anyone else know of any other suggestions?
    try upgrading to qt 4.5

  17. #14
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Disable Close button (X) of a QDialog

    Try this

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qevent.h>
    3. #include <qpushbutton.h>
    4. #include <qdialog.h>
    5. #include <qmessagebox.h>
    6. #include <iostream>
    7.  
    8. class MyDialog : public QDialog
    9. {
    10. public:
    11.  
    12. MyDialog( QWidget* parent=0 ) : QDialog( parent ) {
    13. }
    14.  
    15. protected:
    16.  
    17. void closeEvent ( QCloseEvent * event ) {
    18. event->ignore();
    19. QMessageBox::information( this, "Close?", "Close? No way!" );
    20. }
    21. };
    22.  
    23. int main( int argc, char** argv )
    24. {
    25. QApplication app( argc, argv );
    26.  
    27. MyDialog dlg;
    28. QPushButton* button = new QPushButton( "Exit", &dlg );
    29. QObject::connect( button, SIGNAL( clicked() ),
    30. &app, SLOT( quit() ) );
    31. dlg.resize( 300, 300 );
    32. dlg.show();
    33.  
    34. return app.exec();
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 

  18. #15
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by lni View Post
    Try this

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qevent.h>
    3. #include <qpushbutton.h>
    4. #include <qdialog.h>
    5. #include <qmessagebox.h>
    6. #include <iostream>
    7.  
    8. class MyDialog : public QDialog
    9. {
    10. public:
    11.  
    12. MyDialog( QWidget* parent=0 ) : QDialog( parent ) {
    13. }
    14.  
    15. protected:
    16.  
    17. void closeEvent ( QCloseEvent * event ) {
    18. event->ignore();
    19. QMessageBox::information( this, "Close?", "Close? No way!" );
    20. }
    21. };
    22.  
    23. int main( int argc, char** argv )
    24. {
    25. QApplication app( argc, argv );
    26.  
    27. MyDialog dlg;
    28. QPushButton* button = new QPushButton( "Exit", &dlg );
    29. QObject::connect( button, SIGNAL( clicked() ),
    30. &app, SLOT( quit() ) );
    31. dlg.resize( 300, 300 );
    32. dlg.show();
    33.  
    34. return app.exec();
    35.  
    36. }
    To copy to clipboard, switch view to plain text mode 
    Thank you for your reply. Yes, this was a solution that was considered. I'm wondering if there is another solution where we don't have to inherit and ignore the closeEvent...

  19. #16
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable Close button (X) of a QDialog

    If this helps anyone, you can remove the three buttons on the top left (close, minimize, maximize) on a Mac and leaving the title there using:

    Qt Code:
    1. setWindowModality(Qt::WindowModal);
    To copy to clipboard, switch view to plain text mode 

  20. #17
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Disable Close button (X) of a QDialog

    try Window flags
    see the example: http://doc.trolltech.com/4.3/widgets-windowflags.html

  21. #18
    Join Date
    Dec 2009
    Posts
    128
    Thanks
    7
    Thanked 14 Times in 14 Posts
    Platforms
    Unix/X11 Windows

    Arrow Re: Disable Close button (X) of a QDialog

    hi

    this did the job for me (on a QDialog based class) :

    Qt Code:
    1. Qt::WindowFlags flags = windowFlags() ;
    2. flags |= Qt::CustomizeWindowHint ;
    3. flags &= ~Qt::WindowCloseButtonHint ;
    4. setWindowFlags( flags ) ;
    To copy to clipboard, switch view to plain text mode 

  22. The following user says thank you to totem for this useful post:

    ttimt (22nd February 2014)

  23. #19
    Join Date
    May 2009
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Disable Close button (X) of a QDialog

    Quote Originally Posted by shad View Post
    try upgrading to qt 4.5
    Sorry, I didn't see these replies until now. It's not an option for us right now.

Similar Threads

  1. Diasble close button on a QDialog
    By Krish_ng in forum Qt Programming
    Replies: 12
    Last Post: 17th July 2007, 04:23
  2. Replies: 1
    Last Post: 7th July 2007, 09:03
  3. Disable Checkable Button Question
    By jbpvr in forum Qt Programming
    Replies: 9
    Last Post: 20th March 2007, 17:57
  4. Replies: 2
    Last Post: 5th February 2007, 17:42
  5. Replies: 3
    Last Post: 16th November 2006, 12:24

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.