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);
相关推荐
wjs2024几秒前
Swift 类型转换
开发语言
秃了也弱了。22 分钟前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_4407305032 分钟前
java数组整理笔记
java·开发语言·笔记
Thera7771 小时前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
niucloud-admin1 小时前
java服务端——controller控制器
java·开发语言
夏幻灵2 小时前
JAVA基础:基本数据类型和引用数据类型
java·开发语言
cike_y2 小时前
Spring-Bean的作用域&Bean的自动装配
java·开发语言·数据库·spring
十八度的天空3 小时前
第01节 Python的基础语法
开发语言·python
yue0083 小时前
C# 字符串倒序
开发语言·c#
强子感冒了3 小时前
Java学习笔记:String、StringBuilder与StringBuffer
java·开发语言·笔记·学习