C++ – Using QMDIArea with Qt 4.4.

c++qmdiareaqtqt4user-interface

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->accept();
  else
   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
{
    Q_OBJECT
public:
    explicit MainWindowArea(QWidget *parent = 0);

signals:
    void closeAllSubWindows();
public slots:

};
// Implementation:
MainWindowArea::closeAllSubWindows()
{
    // set close condition (new project is creating, C = true)
    foreach(QMdiSubWindow* sub,this->subWindowList())
    {
        (qobject_cast<ChildWindow*>(sub))->close();
    }
} 

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