How to change text alignment in QTabWidget in C ++?

This is the same question as in: How to change text alignment in QTabWidget?

I tried porting this code to Python in C ++, but it does not work.

Here is the 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 the 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 a NewTabWidget class that inherits from QTabWidget. In the NewTabWidget constructor, I use:

setTabBar(new HorizontalTabWidget);

This is only to use this tabWidget, because setTabBar is protected. Here is what I get: enter image description here

What am I missing?

Edit: I want to create this, but with the icons on top and the captions under the icons (as in Qt Creator): enter image description here

+3
source share
3 answers

; , , , QTCreator. QStyleOptionTabV3, , , :

class TestTabBar : public QTabBar
{
public:
    explicit TestTabBar(QWidget* parent=0) : QTabBar(parent)
    {
        setIconSize(QSize(80, 80));
    }

protected:
    QSize tabSizeHint(int) const
    {
        return QSize(80, 80);
    }
    void paintEvent(QPaintEvent *)
    {
        QStylePainter p(this);
        for (int index = 0; index < count(); index++)
        {
            QStyleOptionTabV3 tab;
            initStyleOption(&tab, index);

            QIcon tempIcon = tab.icon;
            QString tempText = tab.text;
            tab.icon = QIcon();
            tab.text = QString();

            p.drawControl(QStyle::CE_TabBarTab, tab);

            QPainter painter;
            painter.begin(this);
            QRect tabrect = tabRect(index);
            tabrect.adjust(0, 8, 0, -8);
            painter.drawText(tabrect, Qt::AlignBottom | Qt::AlignHCenter, tempText);
            tempIcon.paint(&painter, 0, tabrect.top(), tab.iconSize.width(), tab.iconSize.height(), Qt::AlignTop | Qt::AlignHCenter);    
            painter.end();
        }
    }
};

class TestTabWidget : public QTabWidget
{
public:
    explicit TestTabWidget(QWidget *parent = 0) : QTabWidget(parent)
    {
        setTabBar(new TestTabBar());
    }
};

tabwidget init:

TestTabWidget* test = new TestTabWidget(this);
test->setGeometry(20, 20, 300, 200);
test->addTab(new QWidget(), QIcon("icon0.png"), "test0");
test->addTab(new QWidget(), QIcon("icon1.png"), "test1");
test->setTabPosition(QTabWidget::West);

ubuntu, , ,

+2

, QPainter.

setTabIcon() setTabText() paintEvent() . , ,

tab.text = QString();

tabText().

,

p.drawText(tabrect, Qt::AlignBottom | Qt::AlignHCenter, tabText(index));
tabIcon(index).paint(&p, tabrect, Qt::AlignTop | Qt::AlignHCenter);
+1

This example does not work. causes the program to crash.

give your own example with minor changes - my qt 4.6.3 system for Windows and VS2008

class TestTabBar : public QTabBar
{
public:
    explicit TestTabBar(QWidget* parent=0) : QTabBar(parent)
    {
        setIconSize(QSize(58, 68));
    }

protected:
    QSize tabSizeHint(int) const
    {
        return QSize(58, 68);
    }

    void paintEvent(QPaintEvent *)
    {
        QStylePainter p(this);

        for (int index = 0; index < count(); index++)
        {
            QStyleOptionTabV3 tab;
            initStyleOption(&tab, index);

            QIcon tempIcon = tabIcon(index);
            QString tempText = this->tabText(index);

            QRect tabrect = tabRect(index);

            tab.icon = QIcon();
            tab.text = QString();

            p.drawControl(QStyle::CE_TabBarTab, tab);
            tabrect.adjust(0, 3, 0, -3);

            p.setPen(Qt::black);    
            p.setFont(QFont("Arial", 7));

            p.drawText(tabrect, Qt::AlignBottom | Qt::AlignHCenter, tempText );
            tempIcon.paint(&p, tabrect, Qt::AlignTop | Qt::AlignHCenter);

            this->setTabIcon(index, tempIcon );
            this->setTabText( index, tempText);
        }
    }
};

class TestTabWidget : public QTabWidget
{
public:
    explicit TestTabWidget(QWidget *parent = 0) : QTabWidget(parent)
    {
        setTabBar(new TestTabBar());
    }
};
0
source

Source: https://habr.com/ru/post/1787881/


All Articles