How can I create a dialog that can be closed programatically, but not by the user ?

You can achieve this by reimplementing closeEvent() [qt.nokia.com] and then ignoring or accepting the event depending on a flag.

The following is an example of how to do this:

  1. #include <QtGui>
  2.  
  3. class Dialog : public QDialog
  4. {
  5.     Q_OBJECT
  6. public:
  7.     Dialog();
  8.     void closeEvent ( QCloseEvent * e );
  9. public slots:
  10.     void closeDialog();
  11. private:
  12.     bool ignoreCloseEvent;
  13. };
  14.  
  15. Dialog::Dialog() : QDialog()
  16. {
  17.     ignoreCloseEvent = true;
  18. }
  19.  
  20. void Dialog::closeEvent ( QCloseEvent * e )
  21. {
  22.     if (!ignoreCloseEvent) {
  23.         QDialog::closeEvent(e);
  24.     } else {
  25.         e->ignore();
  26.     }
  27. }
  28. void Dialog::closeDialog()
  29. {
  30.     ignoreCloseEvent = false;
  31.     accept();
  32. }
  33.  
  34. #include "main.moc"
  35.  
  36. int main(int argc, char **argv)
  37. {
  38.     QApplication app(argc, argv);
  39.     Dialog dialog;
  40.     dialog.show();
  41.     return app.exec();
  42. }  

No comments

Write a comment

Sorry, you must be logged in to post a comment.