C++ – Using QMDIArea with Qt 4.4.


I'm using the QMdiArea in Qt 4.4.

If a new project is created, I add a number of sub windows to a QMdiArea. I'd like to disallow the user to close a sub window during runtime. The sub windows should only be closed if the whole application is closed or if a new project is created.

How can I do this?

Best Solution

You need to define your own subWindow. create a subclass of QMdiSubWindow and override the closeEvent(QCloseEvent *closeEvent). you can control it by argument. for example:

void ChildWindow::closeEvent(QCloseEvent *closeEvent)
  if(/*condition C*/)
   closeEvent->ignore(); // you can do something else, like 
                         // writing a string in status bar ...

then subclass the QMdiArea and override QMdiArea::closeAllSubWindows () like this:

class MainWindowArea : public QMdiArea
    explicit MainWindowArea(QWidget *parent = 0);

    void closeAllSubWindows();
public slots:

// Implementation:
    // set close condition (new project is creating, C = true)
    foreach(QMdiSubWindow* sub,this->subWindowList())

you may also need to override close slot of your mdi area.