Use QT to implement an MDI-style main form

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.