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