Qt自定义定时器

Qt自定义定时器_第1张图片

主要代码

#include "oventimer.h"
#include "ui_oventimer.h"

#ifndef M_PI
#define M_PI    3.1415926359
#endif
//每分钟以及每秒的度数
const double DegreesPerMinute =7.0;
const double DegreesPerSecond = DegreesPerMinute/60;
const int MaxMinute=45;
const int MaxSeconds=MaxMinute*60;
const int UpdateInterval =3;    //3s转动一次

OvenTimer::OvenTimer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::OvenTimer)
{
    ui->setupUi(this);

    updateTimer=new QTimer(this);
    connect(updateTimer,SIGNAL(timeout()),this,SLOT(update()));

    finishTimer=new QTimer();
    finishTimer->setSingleShot(true);
    connect(finishTimer,SIGNAL(timeout()),this,SLOT(onTimeout()));//SLOT(timeout()) OvenTimer的

    QFont font;
    font.setPointSize(8);
    setFont(font);

}

OvenTimer::~OvenTimer()
{
    delete ui;
}

void OvenTimer::paintEvent(QPaintEvent *event)
{
    QImage image(size(),QImage::Format_ARGB32_Premultiplied);
    QPainter imagePainter(&image);       //在QImage里画
    imagePainter.setRenderHint(QPainter::Antialiasing,true);
   // QPainter painter(this);
    //painter.setRenderHint(QPainter::Antialiasing,true); //反锯齿

    int side =qMin(width(),height());  //(取宽度与高度的较小值)用来做窗口和视口的
    imagePainter.setViewport((width()-side)/2,(height()-side)/2,side,side);
    imagePainter.setWindow(-50,-50,100,100);


    draw(&imagePainter);
    imagePainter.end();

    QPainter widgetPainter(this);   //直接显示      画的交给QIamge
    widgetPainter.drawImage(0,0,image);
    //draw(&painter);
}
void OvenTimer::draw(QPainter *painter)
{
    static const int triangle[3][2]={
        {-2,-49},{2,-49},{0,-47}
    };

    QPen thickPen(palette().foreground(),1.5);   //粗笔 颜色前景色
    QPen thinPen(palette().foreground(),0.5);    //细笔
    QColor niceBlue(150,150,200);

    painter->setPen(thinPen);
    painter->setBrush(palette().foreground());
    painter->drawPolygon(QPolygon(3,&triangle[0][0]));

    QConicalGradient coneGradient(0,0,-90.0);   //锥形渐变
    coneGradient.setColorAt(0.0,Qt::darkGray);
    coneGradient.setColorAt(0.2,niceBlue);
    coneGradient.setColorAt(0.5,Qt::white);
    coneGradient.setColorAt(1.0,Qt::darkGray);

    painter->setBrush(coneGradient);
    painter->drawEllipse(-46,-46,92,92);

    QRadialGradient haloGradient(0,0,20,0,0);
    haloGradient.setColorAt(0.0,Qt::lightGray);
    haloGradient.setColorAt(0.8,Qt::darkGray);
    haloGradient.setColorAt(0.9,Qt::white);
    haloGradient.setColorAt(1.0,Qt::black);

    painter->setBrush(haloGradient);
    painter->setPen(Qt::NoPen);
    painter->drawEllipse(-20,-20,40,40);

    QRadialGradient knoGradient(-7,-25,60);
    knoGradient.setColorAt(0.0,Qt::black);
    knoGradient.setColorAt(0.2,niceBlue);
    knoGradient.setColorAt(0.3,Qt::lightGray);
    knoGradient.setColorAt(0.8,Qt::white);
    knoGradient.setColorAt(1.0,Qt::black);

    painter->rotate(duration()*DegreesPerSecond);
    painter->setBrush(knoGradient);
    painter->setPen(thinPen);
    painter->drawRoundedRect(-7,-25,14,50,9,9);

    /*画线 和数字*/
    for(int i=0;i<=MaxMinute;i++)
    {
        if(i%5 == 0)
        {
            painter->setPen(thickPen);
            painter->drawLine(0,-41,0,-44);
            painter->drawText(-15,-41,30,30,
                              Qt::AlignHCenter|Qt::AlignTop,QString::number(i));
        }
        else{
            painter->setPen(thinPen);
            painter->drawLine(0,-42,0,-44);
        }

        painter->rotate(-DegreesPerMinute);
    }
}

void OvenTimer::mousePressEvent(QMouseEvent* event)
{
    QPointF point=event->pos()-rect().center();
    double theta=qAtan2(-point.x(),-point.y())*180/M_PI;
    setDuration((duration()+int(theta/DegreesPerSecond)));
}
void OvenTimer::setDuration(int secs)
{
    secs=qBound(0,secs,MaxSeconds); //取 0或者MaxSeconds
    //secs=max(0,min(secs,MaxSeconds));
    finishTime=QDateTime::currentDateTime().addSecs(secs);

    if(secs > 0)
    {
        updateTimer->start(UpdateInterval*1000);
        finishTimer->start(secs*1000);
    }else{
        updateTimer->stop();
        finishTimer->stop();
    }


    update();
}
//返回秒数
int OvenTimer::duration() const
{
    int secs = QDateTime::currentDateTime().secsTo(finishTime);
    if(secs < 0)
        secs=0;

    return secs;
}
void OvenTimer::onTimeout()
{
    QMessageBox::information(this,tr("提示"),tr("Timer is Over"));
}
源代码链接: 点击打开链接

你可能感兴趣的:(QT)