QT风格(QStyle):绘制一个自定义QSpinBox(3)

按照之前的方法再来绘制两个QSpinBox。

设计图:

 根据这个设计图改写子控件位置:

QRect mySpinboxStyle::subControlRect(ComplexControl whichControl,const QStyleOptionComplex *option,SubControl whichSubControl,const QWidget *widget) const
{
    if (whichControl == CC_SpinBox)
    {
        switch (whichSubControl)
        {
            case SC_SpinBoxFrame:
                return option->rect;
            case SC_SpinBoxEditField:
                return QRect(0, option->rect.height() * 0.3 , option->rect.width(), option->rect.height() * 0.4).adjusted(+10, +10, -10, -10);
            case SC_SpinBoxDown:
                return QRect(0,option->rect.height() * 0.7,option->rect.width(),option->rect.height()*0.3);
            case SC_SpinBoxUp:
                return QRect(0,0,option->rect.width(),option->rect.height()*0.3);
            default:
                return QRect();
        }
    }
    else
    {
        return QProxyStyle::subControlRect(whichControl, option,whichSubControl, widget);
    }
}

绘制背景和图标:

void mySpinboxStyle::drawBronzeSpinBoxButton(SubControl which, const QStyleOptionComplex *option,QPainter *painter,const QWidget * widget) const
{
    PrimitiveElement element;
    QRect buttonRect = option->rect;

    if (which == SC_SpinBoxUp)//上按钮
    {
        buttonRect.translate(0, 0);//translate矩形移到指定位置
        element = PE_IndicatorSpinPlus;//PE_IndicatorSpinPlus
    }
    else if(which == SC_SpinBoxDown)
    {
        buttonRect.translate(0, buttonRect.height() * 0.7);
        element = PE_IndicatorSpinMinus;
    }
    buttonRect.setHeight(buttonRect.height() * 0.3);
    QStyleOption buttonOpt(*option);
    buttonOpt.rect = buttonRect;

    //绘制背景开始
    painter->save();
    painter->setClipRect(buttonRect);//在此范围内绘制背景
    if (option->activeSubControls != which)//不是当前活动的子控件
    {
        buttonOpt.state &= ~(State_MouseOver/*在鼠标下面*/ | State_On/*按下*/ | State_Sunken/*凹陷*/);
    }
    QLinearGradient gradient(buttonOpt.rect.topLeft(),buttonOpt.rect.bottomRight());
    gradient.setColorAt(0.0, QColor("#5ee7df"));
    gradient.setColorAt(1.0, QColor("#b490ca"));
    painter->setBrush(gradient);

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

    painter->drawRoundRect(roundRect, cx, cy);//绘制圆角矩形
    if (buttonOpt.state & (State_On | State_Sunken))//按下时,绘制一层暗色
    {
        QColor slightlyOpaqueBlack(0, 0, 0, 63);
        painter->setBrush(slightlyOpaqueBlack);
        painter->drawRoundRect(roundRect, cx, cy);//绘制圆角矩形
    }
    //绘制背景结束
    painter->restore();

    //绘制图标
    QStyleOption arrowOpt(buttonOpt);
    QRect subRect = subControlRect(CC_SpinBox, option, which);
    arrowOpt.rect = subRect.adjusted(+subRect.width() * 0.3, +subRect.height() * 0.3,
                                     -subRect.width() * 0.3, -subRect.height() * 0.3);
    drawPrimitive(element, &arrowOpt, painter);
}

其他的都和上一篇一样。

效果:

 

再来画最后一个,

和前面的整体区别不大就是椭圆形的

 其他的地方不需要改,只需要改这里,绘制上下键背景的时候由绘制圆角矩形改成绘制弧形:

void mySpinboxStyle::drawBronzeSpinBoxButton(SubControl which, const QStyleOptionComplex *option,QPainter *painter,const QWidget * widget) const
{
    PrimitiveElement element;
    QRect buttonRect = option->rect;

    if (which == SC_SpinBoxUp)//上按钮
    {
        buttonRect.translate(0, 0);//translate矩形移到指定位置
        element = PE_IndicatorSpinPlus;//PE_IndicatorSpinPlus
    }
    else if(which == SC_SpinBoxDown)
    {
        buttonRect.translate(0, buttonRect.height() * 0.7);
        element = PE_IndicatorSpinMinus;
    }
    buttonRect.setHeight(buttonRect.height() * 0.3);
    QStyleOption buttonOpt(*option);
    buttonOpt.rect = buttonRect;

    //绘制背景开始
    painter->save();
    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setClipRect(buttonRect);//在此范围内绘制背景
    if (option->activeSubControls != which)//不是当前活动的子控件
    {
        buttonOpt.state &= ~(State_MouseOver/*在鼠标下面*/ | State_On/*按下*/ | State_Sunken/*凹陷*/);
    }
    QLinearGradient gradient(buttonOpt.rect.topLeft(),buttonOpt.rect.bottomRight());
    gradient.setColorAt(0.0, QColor("#5ee7df"));
    gradient.setColorAt(1.0, QColor("#b490ca"));
    painter->setBrush(gradient);
    painter->setPen(Qt::NoPen);

    QRect roundRect = buttonOpt.rect.adjusted(+1, +1, -1, -1);
    int startAngle = 0 * 16;//起始角度,角度可以为负值,如-30*16
    int spanAngle = 180 * 16;//覆盖的角度,绘制方向为逆时针方向
    roundRect.setHeight(roundRect.height() * 2);

    if(which == SC_SpinBoxDown)
    {
        startAngle = 180 * 16;
        spanAngle = 180 * 16;
        roundRect.moveTo(0,option->rect.height()*0.4);
    }

    painter->drawPie(roundRect, startAngle, spanAngle);
    if (buttonOpt.state & (State_On | State_Sunken))//按下时,绘制一层暗色
    {
        QColor slightlyOpaqueBlack(0, 0, 0, 63);
        painter->setBrush(slightlyOpaqueBlack);
        painter->drawPie(roundRect, startAngle, spanAngle);
    }
    //绘制背景结束
    painter->restore();

    //绘制图标
    QStyleOption arrowOpt(buttonOpt);
    QRect subRect = subControlRect(CC_SpinBox, option, which);
    arrowOpt.rect = subRect.adjusted(+subRect.width() * 0.3, +subRect.height() * 0.3,
                                     -subRect.width() * 0.3, -subRect.height() * 0.3);
    drawPrimitive(element, &arrowOpt, painter);
}

效果: 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值