1

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?

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
nutario
  • 773
  • 1
  • 9
  • 15
  • Could you sub-class QMdiArea and override the default closeActiveSubWindow/closeAllSubWindows methods to not close the window when user requests it? – Tuminoid Dec 04 '08 at 08:52

2 Answers2

3

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.

sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • Yes, that is a posibility. It does not remove the "x" in the top right corner but it works right. Actually i was looking for a solution which although removes this "x", too. – nutario Dec 30 '10 at 09:59
  • Probably you can overcome by re-implementing the paint event of mdiArea... but it's hard stuff. – sorush-r Dec 30 '10 at 15:57
1

You'd do this the same as for a top-level window: process and ignore the QCloseEvent it sent. QMdiArea::closeActiveSubWindow/QMdiArea::closeAllSubWindows just call QWidget::close, which sends a closeEvent and confirms that it was accepted before proceeding.

You can process this event by subclassing QMdiSubWindow and reimplementing QWidget::closeEvent, or by using an event filter to intercept it..

puetzk
  • 10,534
  • 3
  • 28
  • 32