使用QT,重写painteven使用QPainter画家画时钟,背景使用渐变色。
运行效果截图:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void paintEvent(QPaintEvent *);
private:
void DrawClock(QPainter *p, QRect rcClock);
private:
QTimer *timer;
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QDebug>
Widget::Widget(QWidget *parent):
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/icon/cat.png"));
setWindowTitle("electronic clock");
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(100);
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);//设置图抗锯齿 字抗锯齿
DrawClock(&painter, rect());
}
void Widget::DrawClock(QPainter *p, QRect rcClock)
{
p->save();
QTime time = QTime::currentTime();
int ms = time.second() * 1000 + time.msec();//毫秒
p->setRenderHint(QPainter::Antialiasing);//反锯齿
// 原点移动
p->translate( rcClock.width() / 2, rcClock.height() / 2);
int side = qMin(rcClock.width(), rcClock.height());
p->scale(side / 200.0, side / 200.0); // 画面比例为200*200 相当于下面的绘制都基于200*200
//background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #F0BF62, stop:1 #444444);
// 绘制渐变背景
QRect rcBg(-95, -95, 190, 190);
QLinearGradient changecolor(QPointF(-95, -95), QPointF(190, 190));
if (time.second() < 5)
{
changecolor.setColorAt(0.0, QColor(QString("#B5C7FA")));
changecolor.setColorAt(1.0, QColor(QString("#FDD6E3")));
}
else if (time.second() < 10)
{
changecolor.setColorAt(0.0, QColor(QString("#DA88F2")));
changecolor.setColorAt(1.0, QColor(QString("#A2F6ED")));
}
else if (time.second() < 15)
{
changecolor.setColorAt(0.0, QColor(QString("#FFCCEB")));
changecolor.setColorAt(1.0, QColor(QString("#FFE1B7")));
}
else if (time.second() < 20)
{
changecolor.setColorAt(0.0, QColor(QString("#74DCF8")));
changecolor.setColorAt(1.0, QColor(QString("#FEC5C3")));
}
else if (time.second() < 25)
{
changecolor.setColorAt(0.0, QColor(QString("#fcdcda")));
changecolor.setColorAt(1.0, QColor(QString("#dbeaf8")));
}
else if (time.second() < 30)
{
changecolor.setColorAt(0.0, QColor(QString("#fdd2ae")));
changecolor.setColorAt(1.0, QColor(QString("#def9dc")));
}
else if (time.second() < 35)
{
changecolor.setColorAt(0.0, QColor(QString("#b8e8da")));
changecolor.setColorAt(1.0, QColor(QString("#edc6c1")));
}
else if (time.second() < 40)
{
changecolor.setColorAt(0.0, QColor(QString("#fbfcdb")));
changecolor.setColorAt(1.0, QColor(QString("#e9defa")));
}
else if (time.second() < 45)
{
changecolor.setColorAt(0.0, QColor(QString("#CCFBFF")));
changecolor.setColorAt(1.0, QColor(QString("#EF96C5")));
}
else if (time.second() < 50)
{
changecolor.setColorAt(0.0, QColor(QString("#f4d03f")));
changecolor.setColorAt(1.0, QColor(QString("#16a085")));
}
else if (time.second() < 55)
{
changecolor.setColorAt(0.0, QColor(QString("#f7ebe1")));
changecolor.setColorAt(1.0, QColor(QString("#f7dff1")));
}
else if (time.second() < 60)
{
changecolor.setColorAt(0.0, QColor(QString("#f7d7ef")));
changecolor.setColorAt(1.0, QColor(QString("#f6bfb8")));
}
p->setPen(Qt::NoPen);
p->setBrush(changecolor);
p->drawRoundedRect(rcBg, 10, 10);
// 绘制刻度
p->save();
p->setPen(Qt::NoPen);
p->setPen(QColor(255, 255, 255));
int curkd = 0;
for (int j = 0; j < 120; ++j)
{
p->drawLine(0, -70, 0, -80);
//寻找当前时间刻度
if (j*500==ms ||(j * 500 < ms && (j + 1) * 500 > ms))
curkd = j;
p->rotate(3.0);
}
p->restore();
// 绘制当前时间波形
p->save();
const int nRange = 6;
p->setPen(Qt::NoPen);
p->setPen(QColor(255, 255, 255));
p->rotate(3.0 * (curkd - nRange));
int nLastLen = 1;
for (int j = 0; j <= nRange; ++j)
{
p->drawLine(0, -80, 0, -80 - (++nLastLen));
p->rotate(3.0);
}
for (int j = 0; j < nRange; ++j)
{
p->drawLine(0, -80, 0, -80 - (--nLastLen));
p->rotate(3.0);
}
p->restore();
// 绘制秒针点
p->save();
p->setPen(Qt::NoPen);
p->setBrush(QColor("#FFA500"));
//p->rotate(1.0 * (ms / (1.0 / 6.0 * 1000)));
//p->rotate(3*(ms/500));
p->rotate(1*(3*ms/500));
p->drawEllipse(QPointF(0, -62), 4, 4);
p->restore();
// 绘制时间文字
p->save();
QFont ft = p->font();
QFont ftTmp = ft;
ftTmp.setPointSize(20);
p->setFont(ftTmp);
p->setPen(QColor("#FFFFFF"));
p->drawText(QRect(-70, -70, 140, 140), Qt::AlignCenter, time.toString("hh:mm"));
p->restore();
}
Widget::~Widget()
{
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}