Custom TabBar: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{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 =
<code>
// header file not shown here
#include "MyTabBar.h"
#include <QLabel>


Something I quickly put up to demonstrate how to define a custom &quot;QTabBar&amp;quot;: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).
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);


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 &quot;MyTabBar.h&amp;quot;<br />#include &lt;QLabel&amp;gt;
    // 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>


MyTabBar::MyTabBar(QWidget *parent)<br /> : QTabBar(parent)<br />{<br /> this-&gt;addTab(QString());
The custom tab widget class, which uses the custom tab bar


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);
<code>
#include "MyTabWidget.h"
#include "MyTabBar.h"


// 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 /></code>
#include <QDebug>


The custom tab widget class, which uses the custom tab bar<br /><code><br />#include &quot;MyTabWidget.h&amp;quot;<br />#include &quot;MyTabBar.h&amp;quot;
MyTabWidget::MyTabWidget(QWidget *parent)
    : QTabWidget(parent)
{
    this->setGeometry(QRect(10, 10, 300, 250));
    MyTabBar* bar;
    bar = new MyTabBar();
    this->setTabBar(bar);
}
</code>


#include &lt;QDebug&amp;gt;
Calling it from '''mainwindow.cpp''' as follows:


MyTabWidget::MyTabWidget(QWidget *parent)<br /> : QTabWidget(parent)<br />{<br /> this-&gt;setGeometry(QRect(10, 10, 300, 250));
<code>
 
    MyTabWidget *mytab;
MyTabBar *bar;<br /> bar = new MyTabBar();<br /> this-&gt;setTabBar(bar);<br />}<br /></code>
    mytab = new MyTabWidget(centralWidget());
 
</code>
Calling it from '''mainwindow.cpp''' as follows:<br /><code><br /> MyTabWidget *mytab;<br /> mytab = new MyTabWidget(centralWidget());<br /></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());