QStyle customizes the appearance of QSpinBox

Click to view detailed introduction

head File

#ifndef SPINBOX_STYLE_1_H
#define SPINBOX_STYLE_1_H

#include <QProxyStyle>
#include <QStyleOptionComplex>
#include <QPainter>

class spinbox_1 : public QProxyStyle
{
    Q_OBJECT

public:
    spinbox_1();
    ~spinbox_1();

public:
    void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = 0) const;
    void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const;
    QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const;

private:
    void drawSpinBox(SubControl sc,const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = 0)const;


};

#endif // SPINBOX_STYLE_1_H

cpp

#include "spinbox_style_1.h"
#include <QDebug>

spinbox_1::spinbox_1()
{
}

spinbox_1::~spinbox_1()
{

}

void spinbox_1::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
    if(control == CC_SpinBox)
    {
        drawSpinBox(SC_SpinBoxDown, option, painter, widget);
        drawSpinBox(SC_SpinBoxUp, option, painter, widget);

        QRect editFiled = subControlRect(CC_SpinBox,option,SC_SpinBoxEditField,widget).adjusted(-10,-10, + 10, + 10);
        painter->save();
        QLinearGradient ga(0,0,editFiled.width(),editFiled.height());
        ga.setColorAt(0.0,QColor("#381FE4"));
        ga.setColorAt(1.0,QColor("#99EC93"));
        painter->setBrush(ga);
        painter->setPen(Qt::NoPen);

        int diameter =12;
        QRect roundRect = editFiled. adjusted(1,1,-1,-1);
        int cx = 100 * diameter / roundRect. width();
        int cy = 100 * diameter / roundRect. height();

        painter->drawRoundedRect(roundRect,cx,cy);//Draw around the edit box
        painter->restore();

        QRect frameRect = subControlRect(CC_SpinBox,option,SC_SpinBoxFrame,widget);
        painter->save();
        painter->setPen(QPen(QColor(Qt::blue),2));
        painter->drawRect(frameRect);//Draw the outer border of SpinBox
        painter->restore();
    }
    else
    {
        QProxyStyle::drawComplexControl(control,option,painter,widget);
    }
}

void spinbox_1::drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
    if(element == PE_IndicatorSpinPlus)
    {
        painter->save();
        painter->translate(option->rect.x(),option->rect.y());
        QPainterPath pa;
        pa.moveTo(option->rect.width()/2,0);
        pa.lineTo(0,option->rect.height());
        pa.lineTo(option->rect.width(),option->rect.height());
        pa.lineTo(option->rect.width()/2,0);
        painter->setPen(QPen(QColor("#128bf1"), 2));
        painter->drawPath(pa);
        painter->restore();
        return;
    }
    else if(element == PE_IndicatorSpinMinus)
    {
        painter->save();
        painter->translate(option->rect.x(),option->rect.y());
        QPainterPath pa;
        pa. moveTo(0,0);
        pa.lineTo(option->rect.width(),0);
        pa.lineTo(option->rect.width()/2,option->rect.height());
        pa.lineTo(0,0);
        painter->setPen(QPen(QColor("#128bf1"), 2));
        painter->drawPath(pa);
        painter->restore();
        return;
    }
    return QProxyStyle::drawPrimitive(element, option, painter, widget);
}

QRect spinbox_1::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, const QWidget *widget) const
{
    if(cc == CC_SpinBox)
    {
        switch (sc) {
        case SC_SpinBoxUp:
            return QRect(opt->rect.x(),0,opt->rect.width()/2,opt->rect.height()*0.4).adjusted(1,1,-1,-1);
        case SC_SpinBoxDown:
            return QRect(opt->rect.width()/2,0,opt->rect.width()/2,opt->rect.height()*0.4).adjusted(1,1,-1,-1 );
        case SC_SpinBoxEditField:
            return QRect(opt->rect.x(),opt->rect.height()*0.4,opt->rect.width(),opt->rect.height()*0.6).adjusted(10,10, -10,-10);
        case SC_SpinBoxFrame:
            return opt->rect;
        default:
            break;
        }
    }
    return QProxyStyle::subControlRect(cc,opt,sc,widget);
}

void spinbox_1::drawSpinBox(QStyle::SubControl sc, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
    QRect buttonRect = option->rect;
    PrimitiveElement pe;
    if(sc == SC_SpinBoxUp)
    {
        pe = PE_IndicatorSpinPlus;
        buttonRect. translate(0,0);
    }
    else
    {
        pe = PE_IndicatorSpinMinus;
        buttonRect.translate(option->rect.width()/2,0);
    }
    buttonRect.setWidth(option->rect.width()/2);
    buttonRect.setHeight(option->rect.height()*0.4);

    painter->save();
    painter->setClipRect(buttonRect);

   // painter->setPen(Qt::NoPen);
    QLinearGradient gradient(0,0,0,option->rect. height());
    gradient.setColorAt(0.0,QColor("#5ee7df"));
    gradient.setColorAt(1.0,QColor("#b490ca"));
    painter->setBrush(gradient);
    QRect bgRect = buttonRect. adjusted(1,1,-1,-1);
    int diameter = 12;
    int cx = 100 * diameter / buttonRect. width();
    int cy = 100 * diameter / buttonRect. height();
    painter->drawRoundedRect(bgRect,cx,cy);

    QPoint p = widget->mapFromGlobal(QCursor::pos());//Mouse click position conversion

    if(option->state & amp;(State_On | State_Sunken) & amp; & amp;bgRect.contains(p))//mouse press position
    {
        QColor pressColor(0,0,0,63);
        painter->setBrush(pressColor);
        painter->drawRoundedRect(bgRect,cx,cy);
    }
    painter->restore();

    QStyleOption plusIcon(*option);
    QRect subRect = subControlRect(CC_SpinBox,option,sc,widget);
    plusIcon.rect = subRect.adjusted(subRect.width()*0.3,subRect.height()*0.3,-subRect.width()*0.3,-subRect.height()*0.3);

    drawPrimitive(pe, & amp; plusIcon, painter, widget);
   // QProxyStyle::drawPrimitive(pe, &plusIcon,painter,widget);

}