Using QMDIArea with Qt 4.4.
I am using QMdiArea
in Qt 4.4.
If a new project is created, I add some subframes to the QMdiArea
. I would like to prevent the user from closing the sub window at runtime. Sub-windows should only be closed if the entire application is closed or a new project is created.
How can i do this?
source to share
You need to define your own subWindow
. subclass QMdiSubWindow
and override closeEvent(QCloseEvent *closeEvent)
. you can manipulate it with an argument. eg:
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 QMdiArea
and override QMdiArea::closeAllSubWindows ()
as follows:
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 the slot of close
your mdi area.
source to share
You would do it the same way as for a top-level window: process and ignore the dispatched QCloseEvent. QMdiArea :: closeActiveSubWindow / QMdiArea :: closeAllSubWindows is simply called by QWidget :: close, which dispatches a closeEvent and confirms that it was received before continuing.
You can handle this event by subclassing QMdiSubWindow and overriding QWidget :: closeEvent, or by using an event filter to catch it.
source to share