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
    Russia
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows
    Thanked 5 Times in 4 Posts

    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

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

    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 

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

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

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

    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 

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

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

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

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

    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 

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

    ttimt (22nd February 2014)

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

    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
  •  
Qt is a trademark of The Qt Company.