Results 1 to 8 of 8

Thread: How to get focus back to main window if non-modal dialog is closed?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to get focus back to main window if non-modal dialog is closed?

    I don't know what this "hide" button is. The dialog title bar has a close button, that does exactly what it is advertised to do. It calls the closeEvent(), which you were claiming it did not do. If that event is accepted (the default) then, to quote the QCloseEvent docs (emphasis mine):
    Close events contain a flag that indicates whether the receiver wants the widget to be closed or not. When a widget accepts the close event, it is hidden (and destroyed if it was created with the Qt::WA_DeleteOnClose flag).
    The corollary is that the dialog is not deleted on close if the Qt::WA_DeleteOnClose attribute is not set. The Qt::WA_DeleteOnClose is set only so the memory is not leaked if you close the parent-less dialog because you cannot rely a QObject parent to do it for you. If you want a mode-less top-level dialog that doesn't delete itself on close then leave out the attribute, but you have to manage the object lifetime elsewhere. Qt::WA_DeleteOnClose has no effect on whether the closeEvent() is called.

    To make the whole application quit if the main window is closed then create the mode-less dialog as a child of the main window. There's no magic here.

    This example demonstrates your requirement. Take careful note of when the dialog is destroyed.
    • Hides when its close button is clicked
    • Hides when its "Hide Me" button is clicked
    • Shows when the main window calls show()
    • Is not considered a top-level window that stops the main event loop exiting

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Dialog: public QDialog {
    5. Q_OBJECT
    6. public:
    7. Dialog(QWidget *p = 0): QDialog(p) {
    8. qDebug() << this << "created";
    9. QPushButton *pb = new QPushButton("Hide Me", this);
    10. connect(pb, SIGNAL(clicked()), this, SLOT(hide()));
    11. QVBoxLayout *layout = new QVBoxLayout;
    12. layout->addWidget(pb);
    13. setLayout(layout);
    14. }
    15. ~Dialog() {
    16. qDebug() << this << "destroyed";
    17. }
    18. protected:
    19. void closeEvent(QCloseEvent *e) { qDebug() << this << "closeEvent called"; }
    20. };
    21.  
    22. class MainWindow: public QMainWindow {
    23. Q_OBJECT
    24. public:
    25. MainWindow(QWidget *p = 0): QMainWindow(p), dialog(0) {
    26. QPushButton *pb = new QPushButton("Show dialog", this);
    27. connect(pb, SIGNAL(clicked()), this, SLOT(openDialog()));
    28. setCentralWidget(pb);
    29. }
    30. ~MainWindow() { qDebug() << "MainWindow destroyed:" << this; }
    31. public slots:
    32. void openDialog() {
    33. if (!dialog)
    34. dialog = new Dialog(this);
    35. dialog->show();
    36. }
    37. protected:
    38. void closeEvent(QCloseEvent *e) {
    39. qDebug() << this << "closeEvent called";
    40. }
    41. private:
    42. Dialog *dialog;
    43. };
    44.  
    45. int main(int argc, char *argv[])
    46. {
    47. QApplication app(argc, argv);
    48.  
    49. MainWindow m;
    50. m.show();
    51. return app.exec();
    52. }
    53. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

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

    Cupidvogel (1st January 2016), falconium (24th April 2011)

Similar Threads

  1. Replies: 2
    Last Post: 4th August 2010, 19:10
  2. Replies: 3
    Last Post: 1st February 2009, 15:49
  3. Replies: 5
    Last Post: 4th August 2006, 23:44
  4. Replies: 3
    Last Post: 23rd July 2006, 18:02
  5. cannot make a main window modal
    By Dark_Tower in forum Qt Programming
    Replies: 12
    Last Post: 23rd March 2006, 10:21

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