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

Aligning QTabBars in Grid Layout



  • For the project I'm working on I'd like to create a layout in which I have three different QTabBars on the sides of a QStackedWidget - The North, West, and East sides. I will use a combination of those selected tabs to determine what widget in the stack will be shown.

    My problem is how I can style these QTabBars. Right now the North bar stretches across the entire StackedWidget, and the East and West TabBars are centered. I want them all to have regular tab sizes and alight in the upper left and upper right corners.

    Any ideas on how I can do that?

    Helper function:

    QTabBar* ProjectTabView::TabGenerator(QTabBar::Shape shape){
        QTabBar* tabBar = new QTabBar();
        tabBar->setShape(shape);
        return tabBar;
    }
    

    Body of code:

        QGridLayout* gridLayout = new QGridLayout();
        this->setLayout(gridLayout);
    
        QTabBar* unitTabs = TabGenerator(QTabBar::RoundedEast);
        unitTabs->addTab("Unit");
        unitTabs->setStyleSheet("QTabBar {border: 1px solid black}");
        QTabBar* subsystemTabs = TabGenerator(QTabBar::RoundedNorth);
        subsystemTabs->addTab("Subsystem");
        QTabBar* logOrStateTabs = TabGenerator(QTabBar::RoundedWest);
        logOrStateTabs->addTab("Log");
    
        QStackedWidget* mainView = new QStackedWidget();
    
        gridLayout->addWidget(subsystemTabs, 0, 1);
        gridLayout->addWidget(logOrStateTabs, 1, 0);
        gridLayout->addWidget(mainView, 1, 1);
        gridLayout->addWidget(unitTabs, 1, 2);
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show a picture of what you would like to achieve ?



  • Ended up figuring this out using Spacers. New Grid code below:

    QGridLayout* gridLayout = new QGridLayout();
    this->setLayout(gridLayout);

    QTabBar* unitTabs = TabGenerator(QTabBar::RoundedEast);
    unitTabs->addTab("Unit");
    QTabBar* subsystemTabs = TabGenerator(QTabBar::RoundedNorth);
    subsystemTabs->addTab("Subsystem");
    QTabBar* logOrStateTabs = TabGenerator(QTabBar::RoundedWest);
    logOrStateTabs->addTab("Log");
    
    QStackedWidget* mainView = new QStackedWidget();
    
    QSpacerItem* leftVertSpacer = new QSpacerItem(1,1,QSizePolicy::Minimum, QSizePolicy::Expanding);
    QSpacerItem* rightVertSpacer = new QSpacerItem(1,1,QSizePolicy::Minimum, QSizePolicy::Expanding);
    QSpacerItem* topHorizSpacer = new QSpacerItem(1,1,QSizePolicy::Expanding, QSizePolicy::Minimum);
    
    gridLayout->addWidget(subsystemTabs, 0, 1);
    gridLayout->addWidget(logOrStateTabs, 1, 0);
    gridLayout->addWidget(mainView, 1, 1, 2, 2);
    gridLayout->addWidget(unitTabs, 1, 3);
    gridLayout->addItem(leftVertSpacer, 2,0);
    gridLayout->addItem(topHorizSpacer, 0,2);
    gridLayout->addItem(rightVertSpacer, 2, 3);

Log in to reply