美化QTabWidget[通俗易懂]

美化QTabWidget[通俗易懂]美化QTabWidget1.效果展示2.用法展示MainWindow::MainWindow(QWidget*parent):QMainWindow(parent),ui(newUi::MainWindow){ui->setupUi(this);setupUI();ui->tabWidget->addTab2(newQWidget(),tr(“thisisfirsttab”));}MainWindo

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

美化QTabWidget

在这里插入图片描述

1.效果展示

在这里插入图片描述

2.用法展示

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{ 
   
    ui->setupUi(this);
    setupUI();
    ui->tabWidget->addTab2(new QWidget(), tr("this is first tab"));
}

MainWindow::~MainWindow()
{ 
   
    delete ui;
}

void MainWindow::setupUI()
{ 
   
    connect(ui->tabWidget, SIGNAL(TabInserted(int)), this, SLOT(OnTabInserted(int)));
    connect(ui->tabWidget, SIGNAL(AddBtnClicked()), this, SLOT(OnAddBtnClicked()));
    connect(ui->tabWidget, SIGNAL(TabClosed(int)), this, SLOT(OnCloseTab(int)));
}

void MainWindow::OnTabInserted(int index)
{ 
   
    QPushButton *button = new QPushButton();
    button->setFixedSize(this->iconSize());
    button->setStyleSheet("border-image: url(:/images/x-capture-options.png);");
    ui->tabWidget->setTabButton2(index, QTabBar::LeftSide, button);

    button = new QPushButton();
    button->setStyleSheet("QPushButton{border-image: url(:/images/close.png)}"
                          "QPushButton:hover{border-image: url(:/images/close_hover.png)}");
    ui->tabWidget->setTabButton2(index, QTabBar::RightSide, button);
}

void MainWindow::OnTabClosed(int index)
{ 
   
    //todo something
}

void MainWindow::OnAddBtnClicked()
{ 
   
    ui->tabWidget->addTab2(new QWidget(), tr("this is first tab"));
}

3.原理简介

主要原理就是对paintEvent重写,以及QProxyStyle继承重新实现。我也是翻了很多资料,看了实现的源码才知道的。

  • QExtTabBarStyle继承自QProxyStyle
void QExtTabBarStyle::drawPrimitive(PrimitiveElement pe, 
                const QStyleOption *option, 
                QPainter *painter,
                const QWidget *widget) const
{ 
   
    if (pe == QStyle::PE_IndicatorArrowLeft) { 
   
        drawArrow(pe, option, painter, widget);
    } else if (pe == QStyle::PE_IndicatorArrowRight) { 
   
        drawArrow(pe, option, painter, widget);
    } else{ 
   
        QProxyStyle::drawPrimitive(pe, option, painter, widget);
    }
}

int QExtTabBarStyle::pixelMetric(PixelMetric metric, 
            const QStyleOption *option, 
            const QWidget *widget) const
{ 
   
    if (PM_TabBarScrollButtonWidth == metric) { 
   
        return 30;
    } else { 
   
        return QProxyStyle::pixelMetric(metric, option, widget);
    }
}

QRect QExtTabBarStyle::subElementRect(SubElement element, 
                                        const QStyleOption *option, 
                                        const QWidget *widget) const
{ 
   
    if (SE_TabBarTabLeftButton == element) { 
   
        return calcIconRect(true, option);
    } else if (SE_TabBarTabRightButton == element) { 
   
        return calcIconRect(false, option);
    } else { 
   
        return QProxyStyle::subElementRect(element, option, widget);
    }
}

QRect QExtTabBarStyle::calcIconRect(bool left, const QStyleOption *option) const
{ 
   
    const QStyleOptionTab *tab_option = qstyleoption_cast<const QStyleOptionTab *>(option);
    QSize icon_size = tab_option->iconSize;
    const QRect tab_rect = tab_option->rect;
    QPoint center_pos;
    QRect button_rect;
    const int icon_padding = 8;
    if (left) { 
   
        center_pos = QPoint(icon_padding+icon_size.width()/2+tab_rect.x(), 
                            tab_rect.y()+(tab_rect.height()-icon_size.height())/2+icon_size.height()/2);
    } else { 
   
        center_pos = QPoint(tab_rect.x()+tab_rect.width()-icon_padding-icon_size.width()/2, 
                                tab_rect.y()+(tab_rect.height()-icon_size.height())/2+icon_size.height()/2);
    }
    button_rect = QRect(QPoint(0, 0), icon_size);
    button_rect.moveCenter(center_pos);
    return button_rect;
}

void QExtTabBarStyle::drawArrow(PrimitiveElement pe, const QStyleOption *option, 
                                QPainter *painter,
                                const QWidget *widget) const
{ 
   
    const QToolButton *tool_btn = static_cast<const QToolButton *>(widget);
    if (nullptr != tool_btn && !tool_btn->isVisible()) 
        return;
    painter->save();
    const QStyleOptionTab *tab_option = qstyleoption_cast<const QStyleOptionTab *>(option);
    QBrush rect_brush;
    if (!tool_btn->isEnabled()) { 
   
        rect_brush = Qt::transparent;
    } else if (tool_btn->underMouse()) { 
   
        rect_brush = QColor(214, 214, 214);
    } else { 
   
        rect_brush = Qt::transparent;
    }
    QRect draw_rect = QRect(0, 0, 16, 16);
    draw_rect.moveCenter(option->rect.center());
    RoundShadowHelper round_helper;
    round_helper.FillRoundShadow(painter, option->rect,rect_brush.color(), 4);
    painter->restore();
    QProxyStyle::drawPrimitive(pe, option, painter, widget);
}
  • QTabBar中的paintEvent重写
void QtExtTabBar::paintEvent(QPaintEvent *event)
{ 
   
    QPainter painter(this);
    drawTab(&painter);
    if (tab_add_button_.draw_plus_btn_)
        drawPlusBtn(&painter);
}

void QtExtTabBar::drawTab(QPainter *painter)
{ 
   
    RoundShadowHelper helper(6,4);
    int border = helper.GetShadowWidth()/2.0;
    painter->save();
    int tab_count = tab_add_button_.draw_plus_btn_ ? count()-1 : count();
    QStyleOptionTabV3 option;
    for (int index = 0; index < tab_count; index++) { 
   
        QRect rect = tabRect(index);
        initStyleOption(&option, index);
        // draw background 
        QRect draw_rect = QRect(QPoint(rect.x()+border, rect.y() + border), QSize(rect.width()-border*2, rect.height()-border*2));
        drawTabBg(painter, helper, option, draw_rect, rect);
        drawTabText(painter, draw_rect, option);
    }
    painter->restore();
}

void QtExtTabBar::drawTabText(QPainter *painter, const QRect &draw_rect, const QStyleOptionTabV3 &option)
{ 
   
    // draw text
    QColor text_color = tb_text_color_.Normal_;
    QRect text_rect = draw_rect.marginsAdded(margins_);
    if (QStyle::State_Selected & option.state) { 
   
        text_color = tb_text_color_.Selected_;
    } else if (QStyle::State_MouseOver & option.state) { 
   
        text_color = tb_text_color_.Hover_;
    }
    painter->setPen(text_color);
    QString text = fontMetrics().elidedText(option.text, Qt::ElideRight, text_rect.width(), 0); 
    painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, text);
}

void QtExtTabBar::drawTabBg(QPainter *painter, RoundShadowHelper &helper, 
                            const QStyleOptionTabV3 &option, QRect draw_rect, QRect real_rect)
{ 
   
    painter->setPen(Qt::NoPen);
    if(QStyle::State_Selected & option.state) { 
   
        helper.RoundShadow(painter, real_rect);
        helper.FillRoundShadow(painter, draw_rect, tb_bg_color_.Selected_, helper.GetRadius());
    } else if(QStyle::State_MouseOver & option.state) { 
   
        helper.FillRoundShadow(painter, draw_rect, tb_bg_color_.Hover_, helper.GetRadius());
    }
}

void QtExtTabBar::drawPlusBtn(QPainter *painter)
{ 
   
    painter->save();
    int last_index = count()-1;
    QStyleOptionTabV3 option;
    initStyleOption(&option, last_index);
    QRect draw_rect = QRect(QPoint(0, 0), tab_add_button_.tab_add_btn_size_);
    draw_rect.moveCenter(tabRect(last_index).center());
    DrawCircle::Draw(painter, draw_rect, tab_btn_add_color_);
    DrawCharacter::DrawPlus(painter, draw_rect);
    painter->restore();
}

4.属性设置

  • 可以自定义tab中的左右button。

  • 可以自定义tab中文字的颜色。

  • 可以设置是否需要绘制“+”按钮。

其他的都可以在代码中找到

5.todo list

  • 不支持tab拖拽
  • 不支持tab, tab button 贴图。

关注微信公众号,获取源码。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190078.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 指令重排详解_cpu指令重排序

    指令重排详解_cpu指令重排序指令重排:编译器指令重排,cpu指令重排,内存指令重排。编译器可能会调整顺序,如下图,左边是c++源码,右边是优化后顺序一条汇编指令的执行是可以分为很多步骤的,分为不同的硬件执行取指IF译码和取寄存器操作数ID执行或者有效地址计算EX(ALU逻辑计算单元)存储器访问MEM写回WB(寄存器)指令重排只可能发生在毫无关系的指令之间,如果指令之间存在依赖关系,则不会重排。单线程内程序的执行结果不能被改变。1原子性是指一个操作是不可中断的.

    2022年10月17日
    2
  • H5面试题—new操作符具体干了什么呢[通俗易懂]

    H5面试题—new操作符具体干了什么呢

    2022年3月8日
    329
  • 有趣的一行 Python 代码

    有趣的一行 Python 代码

    2021年10月21日
    48
  • 横向滚动条的css样式

    padding-bottom:10px;overflow-x:scroll;width:1000px;margin-bottom:20px;

    2022年4月8日
    46
  • hibernate的hql语句批量删除

    hibernate的hql语句批量删除hibernate使用hql语句,根据条件进行批量删除操作;比如:1.给方法传递一个参数,我传递的是userid2.hql语句编写,根据userid进行删除(我使用了拼接userid的方式进行条件拼接)3.通过createQuery(hql),先到数据库查询满足条件的数据。然后通过executeUpdata()方法进行了批量更新删除操作;具体的代码如下:publicvoidemptycartItm(Integeruserid){//根据条件批量删除Stringhql=“delete

    2022年6月16日
    51
  • 一篇教会你写90%的shell脚本_flash动画脚本怎么写

    一篇教会你写90%的shell脚本_flash动画脚本怎么写如果将运维比作手链,将珍珠比作服务,那么将珍珠串起来的线就是Shell,作为实现Linux系统自动管理以及自动化运维所必备的Shell脚本,其重要性便不言而喻。每一位合格的Linux系统管理员或运维工程师,都应该将熟练编写Shell脚本视为基本技能之一,只有这样才能减少不必要的劳动。但是新人在编写脚本时却只会:#!/bin/bash,所以今天就给大家分享100个经典实用的脚本范例,手把手教你写Shell脚本。这100个脚本范例从基础到进阶均涵盖,有侧重地提高编写能力,结构清晰准确,PDF格式,可

    2022年10月3日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号