I am just trying to help
Ok, here's the code:
First you need to make a custom QTabBar class. A sample code is as follows:
Header file (.h):
#include <QTabBar>
#include <QIcon>
#include <QPainter>
class StatusTabBar
: public QTabBar{
Q_OBJECT
public:
StatusTabBar ();
virtual ~StatusTabBar () {}
protected:
QSize tabSizeHint
(int) const { return QSize(70,
27);
} };
#include <QTabBar>
#include <QIcon>
#include <QPainter>
class StatusTabBar : public QTabBar
{
Q_OBJECT
public:
StatusTabBar ();
virtual ~StatusTabBar () {}
protected:
QSize tabSizeHint (int) const { return QSize(70,27);}
void paintEvent(QPaintEvent *);
};
To copy to clipboard, switch view to plain text mode
So, you need to create a custom tab bar class by deriving from QTabBar and then overwriting the tabSizeHint (use this to specify the size of the tabs) and paintEvent (use this for custom drawing). Here's the code from .cpp file:
#include <QTabBar>
#include <QBitmap>
#include <QIcon>
#include <QStylePainter>
#include <QStyleOptionTabV2>
#include "statustabbar.h"
StatusTabBar
::StatusTabBar(): QTabBar(){
}
{
for(int i = 0; i < 3; ++i)
{
initStyleOption(&option, i);
//printf("tab text: %s\n", option.text.toLatin1().data());
//painter.drawControl(QStyle::CE_TabBarTab, option);
painter.
drawItemPixmap(option.
rect,
0,
QPixmap("selected.png"));
painter.drawItemText(option.rect, 0, palette(), 1, option.text);
}
}
#include <QTabBar>
#include <QBitmap>
#include <QIcon>
#include <QStylePainter>
#include <QStyleOptionTabV2>
#include "statustabbar.h"
StatusTabBar::StatusTabBar(): QTabBar()
{
}
void StatusTabBar::paintEvent(QPaintEvent *event)
{
QStylePainter painter(this);
for(int i = 0; i < 3; ++i)
{
QStyleOptionTabV2 option;
initStyleOption(&option, i);
//printf("tab text: %s\n", option.text.toLatin1().data());
//painter.drawControl(QStyle::CE_TabBarTab, option);
painter.drawItemPixmap(option.rect, 0, QPixmap("selected.png"));
painter.drawItemText(option.rect, 0, palette(), 1, option.text);
}
}
To copy to clipboard, switch view to plain text mode
Here, i have 3 tabs in my application, so i hard-coded the loop to run 3 times, but i it's possible to get this value from Qt. You should read more about QStylePainter and QStyleOptionTabV2 class in the Qt documentation. QStylePainter::drawItemPixmap can be used for adding a pixmap to the tabs. option.rect contains the rectangle for the entire tab, so if you want to draw the icon at a particular location, you can specify the appropriate rect. QStylePainter::drawControl can be used for drawing various parts of the tab. You can read more about it the Qt documentation.
Also, if you want to see more drawing code and how Qt draws the default tab bar, check out the paintEvent function in qtabbar.cpp in the Qt source code (qt-source/src/gui/widgets/qtabbar.cpp)
Edit: If you use custom drawing you may not be able to use style sheets. I am not sure it'll work but you can try.
I hope this helps.
Bookmarks