This is the same question as in: How to change text alignment in QTabWidget?
I tried to port that python code into C++ but it doesn't seem to work.
Here is header file:
#include <QTabBar>
class HorizontalTabWidget : public QTabBar
{
Q_OBJECT
public:
explicit HorizontalTabWidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *);
QSize sizeHint() const;
};
Here is source file:
void HorizontalTabWidget::paintEvent(QPaintEvent *)
{
for(int index = 0; index < count(); index++)
{
QPainter * painter = new QPainter(this);
painter->begin(this);
painter->setPen(Qt::blue);
painter->setFont(QFont("Arial", 10));
QRect tabrect = tabRect(index);
painter->drawText(tabrect, Qt::AlignVCenter | Qt::TextDontClip, tabText(index));
painter->end();
}
}
QSize HorizontalTabWidget::sizeHint() const
{
return QSize(130, 130);
}
I use it by creating NewTabWidget class that inherits QTabWidget. In the constructor of NewTabWidget I use:
setTabBar(new HorizontalTabWidget);
This is done just to be able to use that tabWidget because setTabBar is protected. Here is what I get:
What am I missing?
Edit: What I want to create is this but with icons on the top and the labels under the icons (as in Qt Creator):
Vasiliy, thanks for fixing the double QPainter bug.
However, calling setTabIcon() and setTabText() from within paintEvent() leads to an infinite recursion. Remember that tab is a local object, so
does not affect tabText().
So, the example can also be written without making temporary copies and do
the problem should be in the paint method; check if an example below would work for you, it should draw tabs the same way QTCreator does. I've reused the original tab style QStyleOptionTabV3 to do majority of paint work and then just rendered icon and tab's text on top of the image it produced:
tabwidget init:
this worked fine on my ubuntu, hope it gonna work for you, regards
This example does not work. leads to a fall program.
bring your own example with minor edits - my system qt 4.6.3 for Windows and VS2008