Qt调整QTabBar图标和文字的间距

原生的QTabBar放上图标和文字,固定QTab的宽度时,间距较大。通过自定义样式,轻松解决间距问题

cpp 复制代码
#include <QProxyStyle>

class MyTabStyle : public QProxyStyle
{
    Q_OBJECT
public:
    explicit MyTabStyle (QStyle *style = nullptr) : QProxyStyle(style)
    {
    }

    virtual void drawItemText(QPainter *painter, const QRect &rect,
                              int flags, const QPalette &pal, bool enabled,
                              const QString &text,
                              QPalette::ColorRole textRole) const Q_DECL_OVERRIDE
    {
        QStyle::drawItemText(painter, rect, Qt::AlignLeft | Qt::AlignVCenter,
                             pal, enabled, text, textRole);
    }
};

应用示例:

cpp 复制代码
ui->tabWidget->tabBar()->setStyle(new MyTabBar);
相关推荐
AC赳赳老秦4 分钟前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
提笔了无痕6 分钟前
如何用Go实现整套RAG流程
开发语言·后端·golang
(Charon)8 分钟前
【C++ 面试高频基础:指针、引用、const、static、new/delete 总结】
java·开发语言
2601_9618752443 分钟前
法考考试时间安排及科目|时间表|资料已整理
开发语言·c#·inverted-index·suffix-tree·sstable·r-tree·lsm-tree
AI科技星1 小时前
数术工坊第八卷:算力革命
c语言·开发语言·网络·量子计算·agi
金色熊族1 小时前
QTransform使用心得(二)--仿射变换、非仿射变换、矩阵
qt·线性代数·矩阵
geovindu1 小时前
go: Generators Pattern
开发语言·后端·设计模式·golang·生成器模式
码云骑士2 小时前
13-列表append的底层真相(上)-listobject源码中的预分配策略
开发语言·python
.道阻且长.3 小时前
C++ string 操作指南:接口解析
java·c语言·开发语言·c++
蚰蜒螟3 小时前
Java 对象的内存密语:从字段偏移量计算到 Unsafe 访问的完整链路
java·开发语言