Using QT to implement MDI-style main forms

Source: Internet
Author: User

Article Source: http://hi.baidu.com/wuyunju/item/3d20164c99a276f6dc0f6c52

QT provides Mdiarea controls that make it easy to implement standard MDI forms, but it's not easy to use. It feels like 360 browsers to switch subforms by tab. Click the button on the menu item or toolbar to create a new tab, and generate a subform. Click the Cross on the tab to close the tab and release the subform object. To implement this style of MDI, you can use QT's Tabwidget control to set its Tabsclosable property to True, You can display the cross number on the tab. When you click on the cross on the tab, the tabcloserequested (int) signal of the Tabwidget control is set, and we only need to define a response slot to handle the signal to close the tab.

#include "qtmdi.h"

Qtmdi::qtmdi (Qwidget *parent, qt::wflags flags)
: Qmainwindow (parent, flags)
{
UI.SETUPUI (this);
Delete Ui.tab; Delete the default two tabs on Tabwidget
Delete ui.tab_2;
Connect (Ui.actionchild1, SIGNAL (triggered (bool)), this, slot (action1triggered (bool)));//The slot of the action trigger signal of the context menu, creating a new subform
Connect (Ui.actionchild2, SIGNAL (triggered (bool)), this, SLOT (action2triggered (bool)));
Connect (Ui.tabwidget, SIGNAL (tabcloserequested (int)), this, SLOT (tabcloserequested (int)));//Click on the Cross on the tab to trigger
}

Qtmdi::~qtmdi ()
{

}

Void qtmdi::tabcloserequested (Int index)
{
 QString tabCaption =  Ui.tabwidget->tabtext (index);
 if (Qmessagebox::question (This, tr ("Message"),  tr ("Do you want to turn off 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, based on the string to be displayed on the page to find the release has been created, if created, this tab is activated (display)
   if (Ui.tabwidget->tabtext (i)  == tr ("Child1"))
  {
    Ui.tabwidget->setcurrentindex (i);
   return;
  }
 child1 *c1 = new child1 (this);//Create one and add to Tabwidget
 ui.tabwidget-if not found >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 ();
}

http://blog.csdn.net/libaineu2004/article/details/19621569

Using QT to implement MDI-style main forms

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.