如下:通过重载
void OvenTimer::paintEvent(QPaintEvent *event)
事件,实现如上的旋钮绘制:
void OvenTimer::draw(QPainter *p)
{
static const int t[3][2] = {{-2, -49}, {+2, -49}, {0, -47}};
QPen tckP(palette().foreground(), 1.5);
QPen tinPen(palette().foreground(), 0.5);
QColor nice(150,150,200);
p->setPen(tinPen);
p->setBrush(palette().foreground());
p->drawPolygon(QPolygon(3, &t[0][0]));
QConicalGradient c(0,0,-90.0);
c.setColorAt(0.0,Qt::darkGray);
c.setColorAt(0.2,nice);
c.setColorAt(0.5,Qt::white);
c.setColorAt(1.0,Qt::darkGray);
p->setBrush(c);
p->drawEllipse(-46,-46,92,92);
QRadialGradient h(0,0,20,0,0);
h.setColorAt(0.0,Qt::lightGray);
h.setColorAt(0.2,Qt::darkGray);
h.setColorAt(0.5,Qt::white);
h.setColorAt(1.0,Qt::black);
p->setPen(Qt::NoPen);
p->setBrush(h);
p->drawEllipse(-20,-20,40,40);
QLinearGradient k(-7,-25,7,-25);
k.setColorAt(0.0,Qt::black);
k.setColorAt(0.2,nice);
k.setColorAt(0.3,Qt::lightGray);
k.setColorAt(0.8,Qt::white);
k.setColorAt(1.0,Qt::black);
p->rotate(duration() * DegreesPerSecond);
p->setBrush(k);
p->setPen(tinPen);
p->drawRoundRect(-7,-25,14,50,90,49);
for(int i = 0; i <= MaxMinutes; i++)
{
if(i % 5 == 0)
{
p->setPen(tckP);
p->drawLine(0, -41, 0, -44);
p->drawText(-15, -41,30,30, Qt::AlignHCenter|Qt::AlignTop, QString::number(i));
}
else
{
p->setPen(tinPen);
p->drawLine(0,-42, 0, -44);
}
p->rotate(-DegreesPerMinute);
}
}
void OvenTimer::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
int side = qMin(width(), height());
p.setViewport((width() - side) / 2, (height() - side)/2, side, side);
p.setWindow(-50,-50,100,100);
draw(&p);
}
需要实现源码:https://download.csdn.net/download/qiaoleung/11329839