其他分享
首页 > 其他分享> > QT绘制时钟和进度条

QT绘制时钟和进度条

作者:互联网

效果

void Clock::paintEvent(QPaintEvent *event)
{
    /** 绘制时钟 */
    qint64 msec = QDateTime::currentMSecsSinceEpoch() % 60000;
    QPainter painter(this);
    painter.save();
    //设置反锯齿
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);

    QPen pen;
    pen.setColor(QColor(67, 67, 67));
    pen.setWidth(10);

    /** 绘制时钟外围圆环 */
    painter.setPen(pen);
    painter.setBrush(Qt::transparent);
    painter.drawEllipse(50, 50, 384, 384);

    /** 角度渐变 */
    QConicalGradient conicalGradient;
    conicalGradient.setAngle(90);
    conicalGradient.setCenter(242, 242);
    conicalGradient.setColorAt(0, QColor(255, 0, 0));
    conicalGradient.setColorAt(1, QColor(64, 156, 249));

    pen.setBrush(conicalGradient);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pen);
    painter.drawArc(50, 50, 384, 384, -270 * 16, -360*16 * msec / 60000);

    /** 遮挡角度渐变的点 */
    painter.save();
    QPen pen2(pen);
    pen2.setBrush(QColor(64, 156, 249));
    painter.setPen(pen2);
    painter.drawArc(50, 50, 384, 384, -270 * 16, 1);
    painter.restore();

    /** 绘制点状指针 */
    if (msec > 59000)
    {
        pen.setColor(Qt::red);
    } else if (msec < 1000) {
        pen.setColor(QColor(64, 156, 249));
    }
    pen.setWidth(25);
    painter.setPen(pen);
    painter.drawArc(50, 50, 384, 384, -270 * 16 - 360*16 * msec / 60000, 1);

    /** 数字时间 */
    painter.setPen(Qt::white);
    QFont f(font());
    f.setPixelSize(120);
    painter.setFont(f);
    painter.drawText(50, 50, 384, 384, Qt::AlignCenter, QDateTime::currentDateTime().toString("hh:mm"));

    /** 绘制刻度 */
    painter.save();
    painter.translate(242, 242);
    painter.setPen(Qt::transparent);
    painter.setBrush(QColor(65, 65, 65));
    for (int i = 0; i < 60; i++) {
        painter.drawRect(220, -2, 24, 4);
        painter.rotate(6);
    }
    painter.restore();

    /** 绘制进度条底色 */
    pen.setWidth(10);
    pen.setBrush(QColor(65, 65, 65));
    painter.setPen(pen);
    painter.drawLine(20, 490, 120, 490);
    /** 绘制进度条进度 */
    pen.setBrush(QColor(64, 156, 249));
    painter.setPen(pen);
    painter.save();
    QPainterPath pp;
    /** 设置剪切路径 */
    pp.addRoundedRect(0, 485, 15+110 * msec / 60000, 10, 5, 5);
    painter.setClipPath(pp);
    painter.drawLine(20, 490, 120, 490);
    painter.restore();


    event->accept();
    painter.restore();
}

标签:QColor,QT,进度条,50,pen,384,setPen,painter,时钟
来源: https://www.cnblogs.com/kevinjen/p/15975896.html