Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Disable Close button (X) of a QDialog

  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 Disable Close button (X) of a QDialog

    Hello,

    I want to know if it is possible to disable the close button (X) of a QDialog during a Process.
    After my Process is finished i want to enable the close button (X) again.

    Posted some screenshots of an example to show what i mean.





    Any help is welcome

    Greetz,
    ..:: Still Standing Strong ::..

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    I didn't manage to do it by using the Qt window flags, but it is possible using winapi:
    Qt Code:
    1. #ifdef Q_WS_WIN
    2. CMenu* menu = this->GetSystemMenu(FALSE);
    3. menu->ModifyMenu(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED );
    4. #endif
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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

    Marcel,
    this is not a Qt code. it is VC++ code.

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    And what did I say?
    It's not VC code, but it is winapi code. Please notice the ifdefs.

  5. #5
    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

    BrainB0ne,

    try this:
    setWindowFlags(Qt::FramelessWindowHint);
    setWindowFlags(Qt::WindowTitleHint);

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

    ntp (3rd December 2007)

  7. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    The second setWindowFlags overrides the first.
    And he said disable the button, not hide it. But I guess that's a solution too.

  8. #7
    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

    the first flag reset all flag to 0, so,it is also required.

  9. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    No.
    Using the second flag only yields the same result.
    setWindowFlags(Qt::WindowTitleHint) will result in the window flags to be JUST Qt::WindowTitleHint.

    If you want to set more than one flag you have to OR them:
    setWindowFlags(flag1 | flag2 | ... | flagn );

  10. #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

    Damn i am really late with reacting.... sorry for that u guys.

    I am gonna try some of the solutions mentioned... i will tell you about what i have done
    ..:: Still Standing Strong ::..

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

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

    hmmm... I dont get Marcel's solution to work with the Winapi code.

    And the next solution with the windowflags also doesnt work at Qt 3, the mentioned defines do not exist.

    Maybe i have to search trough the Qt 3 Assistent for appropriate window flags.

    Thx for your help and till next time
    ..:: Still Standing Strong ::..

  12. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    hmmm... I dont get Marcel's solution to work with the Winapi code.
    Do you get any errors or it just doesn't work?

    Maybe i have to search trough the Qt 3 Assistent for appropriate window flags.
    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.

  13. #12
    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 ::..

  14. #13
    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

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

    BrainB0ne (10th October 2007)

  16. #14
    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 ::..

  17. #15
    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

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

    GokuH12 (11th March 2009)

  19. #16
    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

  20. #17
    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

  21. #18
    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

  22. #19
    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 

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

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

  24. #20
    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 ::..

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.