按照之前的方法再来绘制两个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);
}
效果: