Custom TabBar: Difference between revisions
Jump to navigation
Jump to search
AutoSpider (talk | contribs) (Add "cleanup" tag) |
AutoSpider (talk | contribs) (Convert ExpressionEngine links) |
||
Line 7: | Line 7: | ||
= Custom QTabBar = | = Custom QTabBar = | ||
Something I quickly put up to demonstrate how to define a custom | Something I quickly put up to demonstrate how to define a custom [http://doc.qt.io/qt-5.0/qtwidgets/qtabbar.html 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 | The custom tab bar class, here only showing one tab being added, the same can be extended to more tabs |
Revision as of 08:18, 4 March 2015
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine. Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean. |
Custom QTabBar
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)
{
this->addTab(QString());
QLabel *lbl;
lbl = new QLabel();
lbl->setPixmap(QPixmap(QString::fromUtf8("../../popup/5.png")));
lbl->setAlignment(Qt::AlignCenter);
// can set a larger size below too, but the icon is always center aligned
lbl->setFixedSize(16,16);
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();
this->setTabBar(bar);
}
Calling it from mainwindow.cpp as follows:
MyTabWidget *mytab;
mytab = new MyTabWidget(centralWidget());