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

Set Background color of body of tab of QTabWidget



  • DTSMain.png

    Hi,
    I am unable to set the body background color of Main Tab and other tabs of QTab widget.
    I am able to set the background color of each Tab header or tab bar.

    here is my code

        // GROUP1
        tw1= new QTabWidget;
        tw1->setContentsMargins(0, 0, 0, 0);
        tw1->setMinimumWidth(1000);
        tw1->setMinimumHeight(780);
        tw1->setStyleSheet(
                "QTabBar {background-color: #0B6C87; color:#ffffff;}"
                "QTabBar::tab:!selected {height: 60px; width: 125px;background-color: #0B6C87; color:#ffffff;font-size:11pt;}"
                "QTabBar::tab:selected {height: 60px; width: 125px;background-color: #0BAFF2; color:#ffffff;font-size:12pt; font-weight: bold;}"
        );
        tw1->addTab(new MainTab(), tr("MAIN"));
        tw1->addTab(new UtilitiesTab(), tr("UTILITIES"));
        tw1->addTab(new HCTab(), tr("HEALTH \nCHECK"));
        tw1->addTab(new LVTab(), tr("LOG \nVIEW"));
        tw1->addTab(new GRTab(), tr("GENERATE \nREPORT"));
        tw1->addTab(new HelpTab(), tr("HELP"));
        tw1->addTab(new AboutTab(), tr("ABOUT"));
        tw1->addTab(new ExitTab(), tr("EXIT"));
    
        connect(tw1, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
    
    

    I tried setting it in the constructor of the MainTab widget. its not working. but it changes my MainTab header label.

    MainTab::MainTab(QWidget *parent) : QWidget(parent) {
        QFont fntGgia12( "Georgia", 12, QFont::Bold);
        QFont fntGgia12N( "Georgia", 12, QFont::Normal);
        QFont fntGgia16( "Georgia", 16, QFont::Bold);
    
        // HEADING
        lblHdg = new QLabel("HFT MAIN PAGE");
        lblHdg->setFont(fntGgia16);
        lblHdg->setFixedSize(500, 50);
        lblHdg->setStyleSheet("QLabel {"
        "border-style: solid;"
        "border-width: 3px;"
        "border-color: black; "
        "}");
        lblHdg->setAlignment(Qt::AlignCenter);
        hbxHdg = new QHBoxLayout;
        hbxHdg->addWidget(lblHdg);
    
        // GROUP1
        bMM = new QPushButton("MANUAL MODE");
        bMM->setFont(fntGgia12);
        bMM->setFixedSize(160, 50);
        bMM->setStyleSheet("QPushButton {"
           "background-color: #FAD7A0;"
           "border-style: solid;"
           "border-color: black;"
           "border-width: 3px;"
           "border-radius: 15px;"
        "}"
    
        "QPushButton:hover {"
        "border-color: #DFFF00;"
        "}"
    
        "QPushButton:pressed {"
        "border-color: #1212F5;"
        "border-style: inset;"
        "background: qradialgradient("
        "cx: 0.4, cy: -0.1, fx: 0.4, fy: -0.1,"
        "radius: 1.35, stop: 0 #fff, stop: 1 #ddd"
        ");"
        "}"
        );
    
        connect(bMM, SIGNAL(clicked(bool)), SLOT(clkbMM()));
    
        bAM = new QPushButton("AUTOMATIC MODE");
        bAM->setFont(fntGgia12);
        bAM->setFixedSize(220, 50);
        bAM->setStyleSheet("QPushButton {"
           "background-color: #FAD7A0;"
           "border-style: solid;"
           "border-color: black;"
           "border-width: 3px;"
           "border-radius: 15px;"
        "}"
    
        "QPushButton:hover {"
        "border-color: #DFFF00;"
        "}"
    
        "QPushButton:pressed {"
        "border-color: #1212F5;"
        "border-style: inset;"
        "background: qradialgradient("
        "cx: 0.4, cy: -0.1, fx: 0.4, fy: -0.1,"
        "radius: 1.35, stop: 0 #fff, stop: 1 #ddd"
        ");"
        "}"
        );
    
        connect(bAM, SIGNAL(clicked(bool)), SLOT(clkbAM()));
    
        hbxBtns = new QHBoxLayout;
        hbxBtns->addWidget(bMM);
        hbxBtns->addWidget(bAM);
        hbxBtns->setContentsMargins(260, 0, 0, 0);
        hbxBtns->setSpacing(40);
        hbxBtns->addStretch(1);
    
        vbxMain = new QVBoxLayout;
        vbxMain->addLayout(hbxHdg);
        vbxMain->addSpacerItem(new QSpacerItem(0, 80, QSizePolicy::Expanding, QSizePolicy::Expanding));
        vbxMain->addLayout(hbxBtns);
        vbxMain->addStretch(1);
    
        setLayout(vbxMain);
        setStyleSheet("QWidget {background-color: #BBE3DF;}");
    }
    

    Please help....Thank you.



  • Hi all,

    I think i got it after searching for solution in net from StackOverflow:

    What's the point of this paintEvent override:
    ...
    void QWidgetDerivedWhatchamit::paintEvent(QPaintEvent *)
    {
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    }
    ...
    Surely that's what QWidget already does?

    from @edward strange

    I am overriding the paint event above. Thank you all.



  • @dan1973
    That's necessary for a custom QWidget-derived class to support background stylesheet.
    Another option is to set the attribulte

    setAttibute(Qt::WA_StyledBackground)
    

Log in to reply