Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set Background color of body of tab of QTabWidget
Forum Updated to NodeBB v4.3 + New Features

Set Background color of body of tab of QTabWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dan1973
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dan1973
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • D dan1973

        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.

        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        @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)
        
        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved