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);
相关推荐
电饭叔5 小时前
利用类来计算点是不是在园内《python语言程序设计》2018版--第8章18题第3部分
开发语言·python
零小陈上(shouhou6668889)5 小时前
YOLOv8+PyQt5玉米病害检测系统(yolov8模型,从图像、视频和摄像头三种路径识别检测)
python·qt·yolo
martian6655 小时前
深入解析C++驱动开发实战:优化高效稳定的驱动应用
开发语言·c++·驱动开发
HappRobot5 小时前
python类和对象
开发语言·python
蓝天智能5 小时前
QT实战:qrc资源动态加载
qt
鸡吃丸子5 小时前
React Native入门详解
开发语言·前端·javascript·react native·react.js
盼哥PyAI实验室5 小时前
Python YAML配置管理:12306项目的灵活配置方案
开发语言·python
漂亮的小碎步丶5 小时前
【启】Java中高级开发51天闭关冲刺计划(聚焦运营商/ToB领域)
java·开发语言
hd51cc5 小时前
MFC运行时
开发语言·mfc
wniuniu_6 小时前
ceph一些细节处理
开发语言·ceph