Custom TabBar

From Qt Wiki
(Redirected from CustomTabBar)
Jump to navigation Jump to search

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Something I quickly put up to demonstrate how to define a custom QTabBar, which shows an image always center aligned (The default QTabBar aligns the icon to the left always).

The custom tab bar class, here only showing one tab being added, the same can be extended to more tabs

// header file not shown here
#include "MyTabBar.h"
#include <QLabel>

MyTabBar::MyTabBar(QWidget* parent)
    : QTabBar(parent)
    QLabel* lbl;
    lbl = new QLabel();

    // can set a larger size below too, but the icon is always center aligned
    this->setTabButton(0, QTabBar::LeftSide, lbl);

The custom tab widget class, which uses the custom tab bar

#include "MyTabWidget.h"
#include "MyTabBar.h"

#include <QDebug>

MyTabWidget::MyTabWidget(QWidget *parent)
    : QTabWidget(parent)
    this->setGeometry(QRect(10, 10, 300, 250));
    MyTabBar* bar;
    bar = new MyTabBar();

Calling it from mainwindow.cpp as follows:

    MyTabWidget *mytab;
    mytab = new MyTabWidget(centralWidget());