Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Custom TabBar

From Qt Wiki
Revision as of 09:26, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search


English | Deutsch | Български

Custom QTabBar

Something I quickly put up to demonstrate how to define a custom "QTabBar":http://doc.qt.io/qt-5.0/qtwidgets/qtabbar.html, 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

<br />// header file not shown here<br />#include &quot;MyTabBar.h&amp;quot;<br />#include &lt;QLabel&amp;gt;

MyTabBar::MyTabBar(QWidget *parent)<br /> : QTabBar(parent)<br />{<br /> this-&gt;addTab(QString());

QLabel *lbl;<br /> lbl = new QLabel();<br /> lbl-&gt;setPixmap(QPixmap(QString::fromUtf8(&quot;../../popup/5.png&amp;quot;)));<br /> lbl-&gt;setAlignment(Qt::AlignCenter);

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

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

<br />#include &quot;MyTabWidget.h&amp;quot;<br />#include &quot;MyTabBar.h&amp;quot;

#include &lt;QDebug&amp;gt;

MyTabWidget::MyTabWidget(QWidget *parent)<br /> : QTabWidget(parent)<br />{<br /> this-&gt;setGeometry(QRect(10, 10, 300, 250));

MyTabBar *bar;<br /> bar = new MyTabBar();<br /> this-&gt;setTabBar(bar);<br />}<br />

Calling it from mainwindow.cpp as follows:

<br /> MyTabWidget *mytab;<br /> mytab = new MyTabWidget(centralWidget());<br />