Timer_Qt

今天写个简单的时钟的程序,秒钟、分钟,时钟能够自动的正确的显示现在的时间,但是不能够修改。这个程序非常简单需要的基本知识是:

1.Qpainter类

2.QTimer

步骤:

1.先新建一个widget

2.重新定义void paintEvent(QPainter *painter)

3.定义一个画图的函数 void draw(QPainter *painter)

main.cpp

#include

#include "timer.h"
#include
   
 
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    Timer w;
    w.show();
 
 
    return a.exec();
}
 
 

timer.h

#ifndef TIMER_H

#define TIMER_H
 
 
#include 
   
 
 
namespace Ui {
    class Timer;
}
 
 
class Timer : public QWidget
{
    Q_OBJECT
 
 
public:
    explicit Timer(QWidget *parent = 0);
    ~Timer();
    void paintEvent(QPaintEvent *);
    void draw(QPainter *painter);
 
 
private:
    Ui::Timer *ui;
};
 
 
#endif // TIMER_H
 
 

timer.cpp

#include "timer.h"

#include "ui_timer.h"
#include
   
 
 
Timer::Timer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Timer)
{
    ui->setupUi(this);
    QTimer *timer = new QTimer;//新建一个定时器来定时刷新图
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
 timer->start(1000);//每隔1秒钟重新绘制一下
 
 
    setWindowTitle(tr("深海的小鱼儿"));//定义widget的标题
    resize(200,200);//初始固定大小为200x200
}
 
 
Timer::~Timer()
{
    delete ui;
}
 
 
void Timer::paintEvent(QPaintEvent *)
{
    QPainter painter(this); //定义一个画笔
    painter.setRenderHint(QPainter::Antialiasing);//开启反转使你画的图比较光滑
    int side = qMin(width(), height());//取长宽的最小值来作为你放大和缩小的标准值(也就是说你的
    painter.translate(width() / 2, height() / 2);//长宽是一样的,这样缩放时不会变形。移到中心点,这时width/height=200
    painter.scale(side / 200.0, side / 200.0);
    draw(&painter);//开始画图
}
 
 
void Timer::draw(QPainter *painter)
{
    static const QPoint hourHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -40)
    };//定义时针的图像
    static const QPoint minuteHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -65)
    };//定义分针的图像
    static const QPoint secondHand[3] = {
        QPoint(7, 8),
        QPoint(-7, 8),
        QPoint(0, -85)
    };//定义秒针的图像
 
 
 
 
    QColor hourColor(127, 0, 127);//定义时针的颜色
    QColor minuteColor(0, 127, 127, 191);//定义分针的颜色
    QColor secondColor(0, 0,255);//定义秒针的颜色
    QTime time = QTime::currentTime();//取系统的时间
 
 
    painter->setPen(Qt::NoPen);//开始画秒针的在系统时间的位置
    painter->setBrush(secondColor);
    painter->save();
    painter->rotate(time.second()*6);//360/60=6
    painter->drawConvexPolygon(secondHand, 3);
    painter->restore();
 
 
    painter->setPen(Qt::NoPen);//开始画分针的在系统时间的位置
    painter->setBrush(hourColor);
    painter->save();
    painter->rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
    painter->drawConvexPolygon(hourHand, 3);
    painter->restore();
    painter->setPen(hourColor);
    for (int i = 0; i < 12; ++i)
    {
        painter->drawLine(88, 0, 96, 0);
        painter->rotate(30.0);
    }
 
 
    painter->setPen(Qt::NoPen);//开始画时针的在系统时间的位置
    painter->setBrush(minuteColor);
    painter->save();
    painter->rotate(6.0 * (time.minute() + time.second() / 60.0));
    painter->drawConvexPolygon(minuteHand, 3);
    painter->restore();
    painter->setPen(minuteColor);
    for (int j = 0; j < 60; ++j)
    {
        if ((j % 5) != 0)
            painter->drawLine(92, 0, 96, 0);
        painter->rotate(6.0);
    }
Timer_Qt_第1张图片 
 
 
}

你可能感兴趣的:(Timer_Qt)