QPainter应用实例复古计时器旋钮

如下:通过重载

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值