Qt设计时钟

与系统时钟对比:
Qt设计时钟_第1张图片

建立工程
新建Qt控件 Qt Gui应用
命名为clock
基类QWidGet
类名Clock

clock.h
——————————————————————————————————————————————————————————————————————————
#ifndef CLOCK_H
#define CLOCK_H


#include 
#include 
#include 
#include 
#include 

static QPoint sed[4]={QPoint(0,-70),QPoint(2,0),QPoint(0,10),QPoint(-2,0)};
static QPoint min[4]={QPoint(0,-60),QPoint(4,0),QPoint(0,12),QPoint(-4,0)};
static QPoint hour[4]={QPoint(0,-50),QPoint(6,0),QPoint(0,13),QPoint(-6,0)};

namespace Ui {
class Clock;
}

class Clock : public QWidget
{
    Q_OBJECT

public:
    explicit Clock(QWidget *parent = 0);
    ~Clock();
protected:
    void paintEvent(QPaintEvent *);

private:
    Ui::Clock *ui;
};

#endif // CLOCK_H

clock.cpp
————————————————————————————————————————————————————————————————————————————————————————
#include "clock.h"
#include "ui_clock.h"

Clock::Clock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Clock)
{
    //resize(200,200);

    QTimer *timer = new QTimer(this);
    timer->start(1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    ui->setupUi(this);
}

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

void Clock::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
    QTime time = QTime::currentTime();
    paint.translate(100,100);
    paint.setRenderHint(QPainter::Antialiasing);
    paint.setBrush(Qt::red);
    paint.setPen(Qt::red);
    paint.save();
    paint.rotate(6.0*time.second());
    paint.drawConvexPolygon(sed,4);
    paint.restore();

    paint.setBrush(Qt::blue);
    paint.setPen(Qt::blue);
    paint.save();
    paint.rotate(6.0*time.minute()+time.second()/60.0);
    paint.drawConvexPolygon(min,4);
    paint.restore();



    paint.setBrush(Qt::black);
    paint.setPen(Qt::black);
    paint.save();
    paint.rotate(30.0*time.hour()+time.minute()/60.0);
    paint.drawConvexPolygon(hour,4);
    paint.restore();

    for(int i = 0; i < 12; i++)
    {
        paint.rotate(30);
        paint.drawLine(0,-88,0,-98);
    }
}

main.cpp
——————————————————————————————————————————————————————————————————————————————————————-
#include <QtGui/QApplication>
#include "clock.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Clock w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(Qt,qt,应用)