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: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (Wieland moved page CustomTabBar to Custom TabBar: underscores)
(Cleanup)
 
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:snippets]]
[[Category:snippets]]
Something I quickly put up to demonstrate how to define a custom {{DocLink|QTabBar}}, which shows an image always center aligned (The default QTabBar aligns the icon to the left always).


'''English''' | [[CustomTabBar_German|Deutsch]] | [[CustomTabBar_Bulgarian|Български]]
The custom tab bar class, here only showing one tab being added, the same can be extended to more tabs


= Custom QTabBar =
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
<code>
<code>
// header file not shown here
// header file not shown here
Line 15: Line 10:
#include <QLabel>
#include <QLabel>


MyTabBar::MyTabBar(QWidget *parent)
MyTabBar::MyTabBar(QWidget* parent)
: QTabBar(parent)
    : QTabBar(parent)
{
{
this->addTab(QString());
    this->addTab(QString());
    QLabel* lbl;
    lbl = new QLabel();
    lbl->setPixmap(QPixmap(QString::fromUtf8("../../popup/5.png")));
    lbl->setAlignment(Qt::AlignCenter);


QLabel *lbl;
    // can set a larger size below too, but the icon is always center aligned
lbl = new QLabel();
    lbl->setFixedSize(16,16);
lbl->setPixmap(QPixmap(QString::fromUtf8("../../popup/5.png")));
    this->setTabButton(0, QTabBar::LeftSide, lbl);
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);
}
}
</code>
</code>


The custom tab widget class, which uses the custom tab bar
The custom tab widget class, which uses the custom tab bar
<code>
<code>
#include "MyTabWidget.h"
#include "MyTabWidget.h"
Line 39: Line 34:


MyTabWidget::MyTabWidget(QWidget *parent)
MyTabWidget::MyTabWidget(QWidget *parent)
: QTabWidget(parent)
    : QTabWidget(parent)
{
{
this->setGeometry(QRect(10, 10, 300, 250));
    this->setGeometry(QRect(10, 10, 300, 250));
 
    MyTabBar* bar;
MyTabBar *bar;
    bar = new MyTabBar();
bar = new MyTabBar();
    this->setTabBar(bar);
this->setTabBar(bar);
}
}
</code>
</code>


Calling it from '''mainwindow.cpp''' as follows:
Calling it from '''mainwindow.cpp''' as follows:
<code>
<code>
MyTabWidget *mytab;
    MyTabWidget *mytab;
mytab = new MyTabWidget(centralWidget());
    mytab = new MyTabWidget(centralWidget());
</code>
</code>

Latest revision as of 13:30, 28 June 2015

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)
{
    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());