其他分享
首页 > 其他分享> > Qt 小例子2 TabBar

Qt 小例子2 TabBar

作者:互联网

Qt小例子

TabBar

#ifndef TABWIDGET_H
#define TABWIDGET_H

#include <QTabWidget>
#include <QStylePainter>
#include <QStyleOption>

class TabBar : public QTabBar
{
public:
    TabBar(const QHash<QString, QColor>& colors, QWidget* parent = 0)
        : QTabBar(parent)
    {
        m_Colors = colors;
    }
protected:
    void paintEvent(QPaintEvent*)
    {
        QStylePainter painter(this);
        QStyleOptionTab opt;
        for(int i = 0; i < count(); i++)
        {
            initStyleOption(&opt, i);
            if(m_Colors.contains(opt.text))
            {
                opt.palette.setColor(QPalette::Button, m_Colors[opt.text]);
            }
            painter.drawControl(QStyle::CE_TabBarTabShape, opt);
            painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
        }
    }
private:
    QHash<QString, QColor> m_Colors;
};

class TabWidget : public QTabWidget
{
public:
    TabWidget(QWidget* parent = 0) : QTabWidget(parent)
    {
        QHash<QString, QColor> dict;
        dict["Tab1"] = QColor("yellow");
        dict["Tab2"] = QColor("#87ceeb");
        dict["Tab3"] = QColor("#90EE90");
        dict["Tab4"] = QColor("pink");
        dict["Tab5"] = QColor("#800080");
        dict["Tab6"] = QColor("yellow");
        dict["Tab7"] = QColor("yellow");
        dict["Tab8"] = QColor("#87ceeb");
        setTabBar(new TabBar(dict));
    }
};
#endif // TABWIDGET_H

在这里插入图片描述

标签:QColor,opt,Qt,parent,例子,dict,TabBar,public
来源: https://blog.csdn.net/TM1695648164/article/details/119595759