ctai/src/ctai_tabbar.cpp
2025-02-23 19:26:30 +08:00

58 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ctai_tabbar.h"
ctai_tabbar::ctai_tabbar()
{
// 设置图标与文字间距
setIconSize(QSize(20, 20));
}
ctai_tabbar::~ctai_tabbar()
{
}
QSize ctai_tabbar::tabSizeHint(int index) const
{
QSize size = QTabBar::tabSizeHint(index);
return QSize(size.width() + 60, size.height()-30); // 扩大标签尺寸
}
void ctai_tabbar::paintEvent(QPaintEvent *event)
{
//创建一个QStylePainter对象painter用于绘制此小部件的样式元素
QStylePainter painter(this);
//创建一个QStyleOptionTab对象opt用于指定绘制选项
QStyleOptionTab opt;
//遍历所有选项卡
for(int i = 0;i < count();i++)
{
//用索引i初始化opt对象的样式选项
initStyleOption(&opt,i);
//指示绘图器使用opt中指定的样式来绘制选项卡的形状
painter.drawControl(QStyle::CE_TabBarTabShape, opt);
//保存当前绘图器的状态
painter.save();
//从opt对象中获取选项卡的大小
QSize s = opt.rect.size();
//转置大小s
s.transpose();
//创建一个矩形r其左上角在原点大小为s
QRect r(QPoint(), s);
//将矩形r的中心移动到原始选项卡矩形的中心
r.moveCenter(opt.rect.center());
//将opt对象中的矩形更新为新位置
opt.rect = r;
//计算索引i处选项卡的中心点c
QPoint c = tabRect(i).center();
//将绘图器平移到中心点c
painter.translate(c);
//将绘图器旋转90度
painter.rotate(90);
//将绘图器平移到原始位置
painter.translate(-c);
//指示绘图器使用opt中指定的样式来绘制选项卡的标签
painter.drawControl(QStyle::CE_TabBarTabLabel,opt);
//将绘图器恢复到应用变换之前保存的状态
painter.restore();
}
}