Search code examples
c++qtqtabbar

QTabWidget with rounded corners and a unified background for the active tab and content?


I have a QTabWidget with QScrollArea in it. The widgets are placed in Qt Designer.

I want to style it like this:

  • all external corners of the tab widget are rounded
  • tabBar has a background the same as the inactive tab
  • tab content has white background

enter image description here

Edit: I got rid of generic stylesheets, now there are two problems left:

  1. Bottom left corner looks strange. I can see that QScrollArea have correct borders, but something is placed above it.
  2. The background of tab contents is light grey, need to be white. enter image description here

Edit: I tried to change my code to apply stylesheet to the whole QTabWidget(using ui->tabWidget->setStyleSheet instead of ui->tabWidget->widget(0)->setStyleSheet, but it only became worse, stylesheet doesn't affect tab content at all: enter image description here

What stylesheet should I use to solve the problems listed above(strange corner and background color) and who should I aplly it to?

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->centralwidget->setStyleSheet(       "QWidget#centralwidget {"
                                                "background-color: yellow;}");
    ui->tabWidget->widget(0)->setStyleSheet("QTabWidget::pane {"
                                                "background-color: white;"
                                                "border: 1px solid green;"
                                                "border-bottom-left-radius: 8px;"
                                                "border-bottom-right-radius: 8px;""}"
                                            "QScrollArea {"
                                                "background-color: white;"
                                                "border: 1px solid blue;"
                                                "border-bottom-left-radius: 8px;"
                                                "border-bottom-right-radius: 8px;""}"
                                            "QScrollBar:vertical {"
                                                "border: none;"
                                                "background: transparent;"
                                                "padding: 0px 0px 4px 0px;"
                                                "width: 12px;""}"
                                            "QScrollBar::handle:vertical {"
                                                "border: 1px black;"
                                                "border-radius: 4px;"
                                                "margin: 2px;"
                                                "background: black;"
                                                "height: 240px;""}"
                                            "QScrollBar::add-line:vertical{"
                                                "border: none;"
                                                "background: transparent;"
                                                "subcontrol-position: bottom;"
                                                "subcontrol-origin: margin;""}"
                                            "QScrollBar::sub-line:vertical{"
                                                "border: none;"
                                                "background: transparent;"
                                                "width: 12px;"
                                                "subcontrol-position: top;"
                                                "subcontrol-origin: margin;""}");
    ui->tabWidget->setStyleSheet(
                                     "QTabBar {"
                                        "border: 1px solid grey;"
                                        "border-top-left-radius: 8px;"
                                        "border-top-right-radius: 8px;"
                                        "height: 42px;"
                                        "background-color: grey""}"
                                     "QTabBar::tab {"
                                         "background: grey;"
                                         "border: 1px solid grey;"
                                         "border-top-left-radius: 8px;"
                                         "border-top-right-radius: 8px;"
                                         "font: 16px Manrope Medium ;"
                                         "color: black;"
                                         "height: 42px;"
                                         "padding: 15px;""}"
                                     "QTabBar::tab:selected {"
                                         "background: white;"
                                         "color: blue;"
                                         "border: 1px solid white;"
                                         "border-top-left-radius: 8px;"
                                         "border-top-right-radius: 8px;"
                                         "border-bottom: none;""}");
        ui->tabWidget->tabBar()->setExpanding(true);
        ui->tabWidget->tabBar()->setDocumentMode(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>50</y>
      <width>621</width>
      <height>471</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="tab">
     <attribute name="title">
      <string>Tab 1</string>
     </attribute>
     <layout class="QGridLayout" name="gridLayout">
      <property name="leftMargin">
       <number>0</number>
      </property>
      <property name="topMargin">
       <number>0</number>
      </property>
      <property name="rightMargin">
       <number>0</number>
      </property>
      <property name="bottomMargin">
       <number>0</number>
      </property>
      <item row="0" column="0">
       <widget class="QScrollArea" name="scrollArea">
        <property name="widgetResizable">
         <bool>true</bool>
        </property>
        <widget class="QWidget" name="scrollAreaWidgetContents">
         <property name="geometry">
          <rect>
           <x>0</x>
           <y>-242</y>
           <width>601</width>
           <height>680</height>
          </rect>
         </property>
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QTableWidget" name="tableWidget">
            <property name="minimumSize">
             <size>
              <width>0</width>
              <height>600</height>
             </size>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton_2">
            <property name="text">
             <string>PushButton</string>
            </property>
           </widget>
          </item>
          <item>
           <widget class="QPushButton" name="pushButton">
            <property name="text">
             <string>PushButton</string>
            </property>
           </widget>
          </item>
         </layout>
        </widget>
       </widget>
      </item>
     </layout>
    </widget>
    <widget class="QWidget" name="tab_2">
     <attribute name="title">
      <string>Tab 2</string>
     </attribute>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Solution

  • I found that QScrollArea contains a child QWidget named scrollAreaWidgetContents, so that widget had straight angles and didn't have background. The solution is to hide its borders and make background transparent by adding "QWidget#scrollAreaWidgetContents" section. Next I added 2px border to selected tab to hide QTabBar background under it. The final stylesheet looks like this:

    ui->centralwidget->setStyleSheet(               "QWidget#centralwidget {"
                                                        "background-color: yellow;}");
        ui->tabWidget->widget(0)->setStyleSheet(    "QWidget#scrollAreaWidgetContents {"
                                                        "border: none;"
                                                        "background-color: transparent;}"
                                                    "QScrollArea {"
                                                        "background-color: white;"
                                                        "border: 1px solid white;"
                                                        "border-bottom-left-radius: 8px;"
                                                        "border-bottom-right-radius: 8px;""}"
                                                    "QScrollBar:vertical {"
                                                        "border: none;"
                                                        "background: transparent;"
                                                        "padding: 0px 0px 4px 0px;"
                                                        "width: 12px;""}"
                                                    "QScrollBar::handle:vertical {"
                                                        "border: 1px black;"
                                                        "border-radius: 4px;"
                                                        "margin: 2px;"
                                                        "background: black;"
                                                        "height: 240px;""}"
                                                    "QScrollBar::add-line:vertical{"
                                                        "border: none;"
                                                        "background: transparent;"
                                                        "subcontrol-position: bottom;"
                                                        "subcontrol-origin: margin;""}"
                                                    "QScrollBar::sub-line:vertical{"
                                                        "border: none;"
                                                        "background: transparent;"
                                                        "width: 12px;"
                                                        "subcontrol-position: top;"
                                                        "subcontrol-origin: margin;""}");
            ui->tabWidget->setStyleSheet(
                                            "QTabBar {"
                                                "border: 1px solid grey;"
                                                "border-top-left-radius: 8px;"
                                                "border-top-right-radius: 8px;"
                                                "height: 42px;"
                                                "background-color: grey""}"
                                             "QTabBar::tab {"
                                                 "background: grey;"
                                                 "border: 1px solid grey;"
                                                 "border-top-left-radius: 8px;"
                                                 "border-top-right-radius: 8px;"
                                                 "font: 16px Manrope Medium ;"
                                                 "color: black;"
                                                 "height: 42px;"
                                                 "padding: 15px;""}"
                                             "QTabBar::tab:selected {"
                                                 "background: white;"
                                                 "color: blue;"
                                                 "border: 2px solid white;"
                                                 "border-top-left-radius: 8px;"
                                                 "border-top-right-radius: 8px;"
                                                 "border-bottom: none;""}");
            ui->tabWidget->tabBar()->setExpanding(true);
            ui->tabWidget->tabBar()->setDocumentMode(true);
    

    enter image description here