Custom TabBar: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]] | |||
'''English''' | [[CustomTabBar_German|Deutsch]] | [[CustomTabBar_Bulgarian|Български]] | |||
= 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 | The custom tab bar class, here only showing one tab being added, the same can be extended to more tabs<br /><code><br />// header file not shown here<br />#include "MyTabBar.h&quot;<br />#include <QLabel&gt; | ||
MyTabBar::MyTabBar(QWidget *parent)<br /> : QTabBar(parent)<br />{<br /> this->addTab(QString()); | |||
= | QLabel *lbl;<br /> lbl = new QLabel();<br /> lbl->setPixmap(QPixmap(QString::fromUtf8("../../popup/5.png&quot;)));<br /> lbl->setAlignment(Qt::AlignCenter); | ||
* | // can set a larger size below too, but the icon is always center aligned<br /> lbl->setFixedSize(16,16);<br /> this->setTabButton(0, QTabBar::LeftSide, lbl);<br />}<br /></code> | ||
The custom tab widget class, which uses the custom tab bar<br /><code><br />#include "MyTabWidget.h&quot;<br />#include "MyTabBar.h&quot; | |||
#include <QDebug&gt; | |||
MyTabWidget::MyTabWidget(QWidget *parent)<br /> : QTabWidget(parent)<br />{<br /> this->setGeometry(QRect(10, 10, 300, 250)); | |||
MyTabBar *bar;<br /> bar = new MyTabBar();<br /> this->setTabBar(bar);<br />}<br /></code> | |||
Calling it from '''mainwindow.cpp''' as follows:<br /><code><br /> MyTabWidget *mytab;<br /> mytab = new MyTabWidget(centralWidget());<br /></code> |
Revision as of 09:26, 24 February 2015
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 "MyTabBar.h&quot;<br />#include <QLabel&gt;
MyTabBar::MyTabBar(QWidget *parent)<br /> : QTabBar(parent)<br />{<br /> this->addTab(QString());
QLabel *lbl;<br /> lbl = new QLabel();<br /> lbl->setPixmap(QPixmap(QString::fromUtf8("../../popup/5.png&quot;)));<br /> lbl->setAlignment(Qt::AlignCenter);
// can set a larger size below too, but the icon is always center aligned<br /> lbl->setFixedSize(16,16);<br /> this->setTabButton(0, QTabBar::LeftSide, lbl);<br />}<br />
The custom tab widget class, which uses the custom tab bar
<br />#include "MyTabWidget.h&quot;<br />#include "MyTabBar.h&quot;
#include <QDebug&gt;
MyTabWidget::MyTabWidget(QWidget *parent)<br /> : QTabWidget(parent)<br />{<br /> this->setGeometry(QRect(10, 10, 300, 250));
MyTabBar *bar;<br /> bar = new MyTabBar();<br /> this->setTabBar(bar);<br />}<br />
Calling it from mainwindow.cpp as follows:
<br /> MyTabWidget *mytab;<br /> mytab = new MyTabWidget(centralWidget());<br />