Reproduced to: http://blog.csdn.net/henreash/article/details/7618414
Qt provides the mdiarea control to easily implement standard MDI forms, but it is not convenient to use. it seems that it is better to switch the subform through the tab like the 360 browser. click the menu item or the button on the toolbar to create a new tab and generate a subform. click the cross sign on the tab to close the tab and release the child form object. to implement this style of MDI, you can use the tabwidget control of QT to set its tabsclosable attribute to true to display the cross sign on the tab. when you click the cross sign on the tab, The tabcloserequested (INT) signal of the tabwidget control is triggered. You only need to define a response slot to process the signal of the disabled tab.
# Include "qtmdi. H"
Qtmdi: qtmdi (qwidget * parent, QT: wflags flags)
: Qmainwindow (parent, flags)
{
Ui. setupui (this );
Delete UI. tab; // Delete two tabs that are contained by default on the tabwidget
Delete UI. tab_2;
Connect (UI. actionchild1, signal (triggered (bool), this, slot (action1triggered (bool); // the slot of Action triggering signals in the context menu to create a new subform
Connect (ui. actionchild2, signal (triggered (bool), this, slot (action2triggered (bool )));
Connect (ui. tabwidget, signal (tabcloserequested (INT), this, slot (tabcloserequested (INT); // trigger by clicking the cross sign on the tab
}
Qtmdi ::~ Qtmdi ()
{
}
Void qtmdi: tabcloserequested (INT index)
{
Qstring tabcaption = UI. tabwidget-> tabtext (INDEX );
If (qmessagebox: Question (this, TR ("message"), TR ("whether to close the tab:") + tabcaption, qmessagebox: Yes | qmessagebox: No) = qmessagebox: Yes)
Delete UI. tabwidget-> widget (INDEX); // close the tab and release the corresponding widget control object
}
Void qtmdi: action1triggered (bool checked)
{
For (INT I = 0; I <UI. tabwidget-> count (); I ++) // first, find and release the created String Based on the string to be displayed on the tab. If yes, activate the tab (displayed)
If (ui. tabwidget-> tabtext (I) = tr ("Child1 "))
{
Ui. tabwidget-> setcurrentindex (I );
Return;
}
Child1 * C1 = new Child1 (this); // if no one is found, create one and add it to the tabwidget.
Ui. tabwidget-> addtab (C1, TR ("Child1 "));
C1-> showmaximized ();
}
Void qtmdi: action2triggered (bool checked)
{
For (INT I = 0; I <UI. tabwidget-> count (); I ++)
If (ui. tabwidget-> tabtext (I) = tr ("child2 "))
{
Ui. tabwidget-> setcurrentindex (I );
Return;
}
Child2 * C2 = new child2 (this );
Ui. tabwidget-> addtab (C2, TR ("child2 "));
C2-> showmaximized ();
}