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);
相关推荐
Shaoshing14 小时前
消息顺序
java·开发语言
云水初14 小时前
【项目篇】基于LanGraph的自动处理客服邮件的 Agent汇总
开发语言·笔记·python·学习·fastapi·langgraph
茉莉玫瑰花茶14 小时前
GO [ 内存与变量逃逸分析 ]
开发语言·后端·golang
lhh_qrsly15 小时前
OTA 加密完整方案(嵌入式 IoT 常用)
开发语言·物联网·php
Nontee15 小时前
Quartz 新手入门:定时任务的两个承诺
开发语言·windows·python
\光辉岁月/15 小时前
2.异常类
java·开发语言·异常类
淡海水15 小时前
05-03-源码-Boehm GC分配与碎片管理
开发语言·算法·unity·c#·游戏引擎·gc
努力实干15 小时前
C++ 面向对象与工程架构
开发语言·c++·架构
代钦塔拉15 小时前
【C++/Qt 进阶】设计模式系列(6):桥接模式——把“两个独立变化的维度“拆开
c++·qt·设计模式
十年Java程序媛15 小时前
JVM 基础与 GC 垃圾回收详解|可达性分析、分代模型、垃圾收集器、STW
java·开发语言·jvm·spring boot·后端·面试