Use custom tab bar with QMdiArea
Asked Answered
A

1

8

I see that QMdiArea has a tabbed view mode. I want to be able to split the main window with two QMdiArea widgets and to be able to drag and drop tabs between each of them.

I have already done it with a simple QTabWidget where I can set custom tab bar. At the same time I want to switch QMdiArea view mode thus using QTabWidget is not an option for me. But I don't see any methods to set custom tab bar within QMdiArea.

How can this be done?

Ancel answered 13/5, 2013 at 20:53 Comment(0)
G
0

Answer provided by Dmitry K.


You have to make a new class inheriting QMdiArea. Set its view mode to TabbedView to make the standard QTabBar to be constructed within QMdiArea. Then get all children and find QTabBar widget with QString(QObject::metaObject()->className()) == "QTabBar". Hide it. You will get a blank area above the document in TabbedView mode. Construct you custom tab bar and reparent it to your custom mdi area. Connect signals and slots that are fired and used when the sub windows and tabs are activated. You can have your custom tab bar as a class member of your custom mdi area.

Code example:

  1. Looking for a standart QTabBar within a custom mdi area in its constructor:

     m_pMdiAreaTabBar = NULL;
     m_pMdiArea->setViewMode(QMdiArea::TabbedView);
     QObjectList listChildren = m_pMdiArea->children();
     for (QObjectList::Iterator i = listChildren.begin(); i != listChildren.end(); ++i)
     {
        if (QString((*i)->metaObject()->className()) == "QTabBar")
        {
           m_pMdiAreaTabBar = dynamic_cast<QTabBar*>(*i);
           break;
        }
     }
    
  2. Reparent:

     m_pTabBar->setParent(m_pMdiArea);
    
  3. Hiding:

     if (m_pMdiAreaTabBar != 0) m_pMdiAreaTabBar->hide();
    
  4. Signals & Slots used: QMdiArea::subWindowActivated(QMdiSubWindow*), QTabBar::currentChanged(int)

Gavrah answered 13/5, 2013 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.