Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to manipulate layout of QTabBar?



  • Hi! I am trying to create a widget in QTabBar of a QTabWidget. To achieve this I created a few widgets inside QWidget and added it to my QTabBar like this:

    tabBar = ui.tabWidget->tabBar();
    newTabBar = new myTabBar; // QWidget with multiple widgets inside it
    tabBar->setTabButton(0, QTabBar::LeftSide, newTabBar);
    

    I was able to add the widgets, but I am not able to control the margins of QTabBar. It looks like this:

    TabProblem.PNG
    No matter how many different methods I try, I am unable to get rid of the extra margins around the widgets in the tab bar. I want the tab bar to be the exact size of the widget that I am putting in there. I tried the following methods (comments above describes what the end result was):

    /* Doesn't do anything */
    tabBar = ui.tabWidget->tabBar();
    tabBar->setContentsMargins(0, 0, 0, 0);
    newTabBar = new myTabBar;
    tabBar->setTabButton(0, QTabBar::LeftSide, newTabBar);
    
    /* Throws an exception: 0x00007FF9F06F4B1A (Qt5Widgetsd.dll) in QtWidgetsApplication1.exe: 0xC0000005: Access violation reading location 0x0000000000000008. */
    tabBar = ui.tabWidget->tabBar();
    tabBar->layout()->setContentsMargins(0, 0, 0, 0);
    newTabBar = new myTabBar;
    tabBar->setTabButton(0, QTabBar::LeftSide, newTabBar);
    
    /* This produces a weird result shown in picture below.*/
    tabBar = ui.tabWidget->tabBar();
    newTabBar = new myTabBar;
    myLayout = new QVBoxLayout;
    myLayout->addWidget(newTabBar);
    myLayout->setContentsMargins(0, 0, 0, 0);
    tabBar->setLayout(myLayout);
    

    TabWeird.PNG

    Is there any way I can get rid of this extra margin?


  • Lifetime Qt Champion

    Hi
    I dont think you can.
    https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qtabbar.cpp.html
    seems to be no layouts we can adjust.

    Also if you could, it would no longer be possible to switch tab as you cant click on the actual tab anymore :)



  • @mrjj Thanks! It's a pity that we can't change the layout of the Tab Bar.


  • Lifetime Qt Champion

    @CJha
    Hi
    You could use a QStacked instead and make your own header bar for the full widgets.


Log in to reply