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);
相关推荐
坐吃山猪15 分钟前
WebClient内存配置解析
开发语言·springboot·webflux
jdksjw43 分钟前
同步与异步、阻塞与非阻塞、多线程、协程超详细讲解(Python并发编程从入门到精通)
开发语言·python
Zach_菠萝侠1 小时前
【DeepSeek Harness 研究】进化方向4:安全加固 思考、设计与实现
开发语言·安全·deepseek
SL-staff1 小时前
技术实践:HR如何用JVS-Logic可视化编排实现考勤数据同步(含节点配置与异常处理)
开发语言·python·低代码·钉钉·可视化编排·jvs-logic·hr技术
SomeB1oody2 小时前
【RustyML入门】6.0. 数学工具
开发语言·后端·机器学习·rust·教程
进制树3 小时前
【飞控开发实战·⑲】ROS2无人机开发环境搭建:Jazzy安装、工作空间与hello_drone节点实战
开发语言·安全·无人机·课程设计
牛马也想出海3 小时前
亚马逊爬虫代理IP:IP类型选择与配置指南
开发语言·php
菜冻鱼3 小时前
Python-pytorch-高级技巧
开发语言·人工智能·pytorch·python·深度学习·神经网络·聚类
lpfasd1233 小时前
python webview打包版卡死、开发版正常
开发语言·python
weixin_461408584 小时前
npm npx yarn
开发语言·前端·javascript