QTabWidget 应用 QProxyStyle 自定义样式

#ifndef CUSTOMTABSTYLE_H
#define CUSTOMTABSTYLE_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 

const QColor TABBARCOLOE_NORMAL = QColor(192, 204, 224);
const int ct_radius = 5;

class CustomTabStyle :public QProxyStyle
{
public:
    CustomTabStyle(QStyle *style = nullptr)
        :QProxyStyle(style)
    {
    }

    virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const override
    {
        QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
       if(CT_TabBarTab == type)
       {
             const QStyleOptionTab * taboption = qstyleoption_cast<const QStyleOptionTab*>(option);
             if (QTabBar::Shape::RoundedWest == taboption->shape)
             {
                  s.transpose();
                  s.rwidth() = 140;
                  s.rheight() = 40;
             }
       }
        return  s;
    };

    virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = nullptr) const override
    {

        if (CE_TabBarTab == element)
        {
            const QStyleOptionTab * taboption = qstyleoption_cast<const QStyleOptionTab*>(option);
            if (taboption)
            {
                 painter->setRenderHint(QPainter::Antialiasing,true);
                 painter->setRenderHint(QPainter::HighQualityAntialiasing, false);
                painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
                 QRect allRect = taboption->rect;

                 if ((QStyle::State_Selected & taboption->state))
                 {
                     painter->save();

                     painter->setPen(TABBARCOLOE_NORMAL);
                     painter->setBrush(TABBARCOLOE_NORMAL);
                     painter->drawRect(allRect);

                        allRect.adjust(0,0,1,0);
                     const QPalette  defaultPalette =  taboption->palette;
                     QColor backgroundColor = defaultPalette.color(QPalette::Button);

                     QPainterPath path;
                     path.moveTo(allRect.right(), allRect.bottom()); //右下角
                     path.lineTo(allRect.left()+ ct_radius, allRect.bottom()); //左下角
                     path.quadTo(allRect.left(),allRect.bottom(), allRect.left(), allRect.bottom() -ct_radius ); //左下圆角
                     // path.arcTo(allRect.left(), allRect.bottom() - ct_radius*2 , ct_radius * 2, ct_radius * 2, -90,-90 );
                     path.lineTo(allRect.left(),allRect.top()+ ct_radius); //左上角

                     path.quadTo(allRect.left(),allRect.top(), allRect.left() +ct_radius,allRect.top()); //左上圆角
                      //   path.arcTo(allRect.left(), allRect.top(), ct_radius * 2, ct_radius * 2, 180,-90 );
                     path.lineTo(allRect.right() + 2,allRect.top() );      //右上角

                     QPen pen;
                     pen.setColor(Qt::black);
                     pen.setWidthF(0.8);
                    painter->setPen(pen);
                    painter->setBrush(backgroundColor);
                    painter->drawPath(path);


                     painter->restore();
                 }
                 else
                {
                    allRect.adjust(0,-2,1,1);
                    painter->save();
                    painter->setPen(TABBARCOLOE_NORMAL);
                    painter->setBrush(TABBARCOLOE_NORMAL);
                    painter->drawRect(allRect);

                    QPen pen;
                    pen.setColor(Qt::black);
                    pen.setWidth(1);
                    painter->setPen(pen);
                    painter->drawLine(allRect.topRight(), allRect.bottomRight());
                    painter->restore();
                }

                painter->setPen(Qt::black);
                painter->drawText(allRect.adjusted(2,2,-2,-2), Qt::AlignCenter,taboption->text);
                qDebug()<<"option:"<< taboption->state <<endl;
                 return;
            }
        }

         //qDebug()<<"element:"<< element <
        return QProxyStyle::drawControl(element,option,  painter, widget);
    }
};

//子QTabWidget 样式
/*
QWidget{
background-color: rgb(215, 228, 246);
}
QTabWidget#tabWidget:pane{	
border-top:1px solid #000000;;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
border-left:1px solid #000000;
left: -0.1em;
}
*/
#endif // CUSTOMTABSTYLE_H

QTabWidget 应用 QProxyStyle 自定义样式_第1张图片

注意事项:

1.ControlElement 有优先级 ,例如CE_TabBarTab 响应后直接返回,则不会进入CE_TabBarTabLabel
2.QStyleOption *option 的状态判断,要用 &, 因为状态一般是好几个组合的,只能判断是否含有该状态。同时,也要注意,后面想要的状是否被前面的状态判断条件获取了。例如 把
QStyle::State_Selected & taboption->state
放在前面,则不会进入
QStyle::State_HasFocus& taboption->state
判断,因为被前面的判断条件截胡了

待解决:

1.tabbar按钮的边框颜色设置了黑色却无效。

你可能感兴趣的:(QT,前端,qt)