Qt时钟界面
参考网上的代码,
目前成都创新互联公司已为上千的企业提供了网站建设、域名、雅安服务器托管、网站改版维护、企业网站设计、松滋网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
直接附上源码
clock.cpp
#include "clock.h"
#include
#include
#include
#include
CLOCK::CLOCK(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
timer->start(1000);//一秒钟
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
QFont font;
font.setPointSize(10);//字体大小设置为10
setFont(font);
}
CLOCK::~CLOCK()
{
}
void CLOCK::paintEvent(QPaintEvent * /* event */)
{
//下面三个数组用来定义表针的三个顶点,以便后面的填充
static const QPoint hourHand[3] = {
QPoint(3, 8),
QPoint(-3, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(3, 8),
QPoint(-3, 8),
QPoint(0, -70)
};
static const QPoint secondHand[3] = {
QPoint(3, 8),
QPoint(-3, 8),
QPoint(0, -90)
};//秒针
//填充表针的颜色
QColor hourColor(127, 0, 127); //分针颜色(第四个表示不透明度)
QColor minuteColor(0, 127, 127, 191);
QColor secondColor(127, 127, 0, 127);
int side = qMin(width(), height()); //绘制的范围(宽、高中最小值)
QTime time = QTime::currentTime(); //获取当前的时间
QPainter painter(this); //声明用来绘图用的painter
painter.setRenderHint(QPainter::Antialiasing);//绘制的图像反锯齿
painter.translate(width() / 2, height() / 2);//重新定位坐标起始点,把坐标原点放到窗体的中央
painter.scale(side / 300.0, side / 300.0);//设定画布的边界,用窗体宽高的最小值来计算时钟的大小,防止窗体拉伸导致的时钟变形以及显示不全
painter.setPen(Qt::red); //填充时针,不需要边线所以NoPen
QString timeStr= QTime::currentTime().toString(); //绘制当前的时间
painter.drawText(-40,30,80,30,Qt::AlignHCenter | Qt::AlignTop, timeStr);
painter.setPen(Qt::NoPen); //填充时针,不需要边线所以NoPen
painter.setBrush(hourColor); //画刷颜色设定
painter.save(); //保存painter的状态,保存的是当前的坐标状态,如果不保存,画完之后坐标以改变不方便画下一个
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))); //将painter(的”视角“)根据时间参数转移(30° * (小时 + 分钟 / 60))
painter.drawConvexPolygon(hourHand, 3); //填充时针的区域
painter.restore();
painter.setPen(hourColor); //下面画表示小时的刻度,此时要用到画笔(因为要划线)
for (int i = 0; i < 12; ++i) {
painter.drawLine(0, -88, 0, -96); //写上刻度数字
if (i == 0) painter.drawText(-10,-88,20,20,Qt::AlignHCenter | Qt::AlignTop,QString::number(12));
else painter.drawText(-10,-88,20,20,Qt::AlignHCenter | Qt::AlignTop,QString::number(i));
painter.rotate(30.0);
}
//后面的跟前面的类似,分别绘制了分针和秒针,及相应的刻度
painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0)); //设旋转(角度 = 6° * (分钟 + 秒 / 60))
painter.drawConvexPolygon(minuteHand, 3); //填充分针部分
painter.restore();
painter.setPen(minuteColor);
for (int j = 0; j < 60; ++j) { //循环60次,绘制表盘(其实可以从1开始,到59,提高一点效率)
if ((j % 5) != 0) //判断是否能被5整除(能被5整除表示是正点刻度,暂不绘制)
painter.drawLine(0, -92, 0, -96); //不是正点刻度,绘制长4个像素的直线
painter.rotate(6.0); //循环60次,每次旋转6度,所以不用save和restore
}
painter.setPen(Qt::NoPen); //填充分针,不需要边线所以NoPen
painter.setBrush(secondColor);
painter.save(); //设置画刷颜色
painter.rotate(6.0 * time.second()); //设置旋转(6° * 秒)
painter.drawConvexPolygon(secondHand, 3); //设置填充
painter.restore(); //恢复保存前状态
painter.setBrush(Qt::black); //画上中心原点
painter.drawEllipse(QPoint(0,0),2,2);
painter.setBrush(Qt::NoBrush); //画上外圆圈
painter.setPen(Qt::black);
painter.drawEllipse(QPoint(0,0),97,97);
}
void CLOCK::draw(QPainter *painter)
{
QPen thickPen(palette().foreground(), 1.5);//设置刻度线为粗黑
QPen thinPen(palette().foreground(), 0.5);//设置刻度线为灰
QTime time = QTime::currentTime();
painter->translate(100,100);//重新设定坐标原点
painter->setRenderHint(QPainter::Antialiasing);//消锯齿
painter->setBrush(Qt::red);
painter->setPen(Qt::red);
painter->save();//保存坐标系,防止坐标系跑偏了
painter->rotate(6.0*time.second());//注意是6.0,不是6
//painter->drawConvexPolygon(sed,4);
painter->restore();//复位之前的坐标系
painter->setBrush(Qt::blue);
painter->setPen(Qt::blue);
painter->save();
painter->rotate(6.0*(time.minute()+time.second()/60.0));
//painter->drawConvexPolygon(min,4);
painter->restore();
painter->setBrush(Qt::black);
painter->setPen(Qt::black);
painter->save();
painter->rotate(30.0*(time.hour()+time.minute()/60.0));
//painter->drawConvexPolygon(hour,4);
painter->restore();
// //painter->drawLine(0,-98,0,-88);
// for(int i=0;i<12;i++)
// {
// painter->rotate(30);//坐标轴旋转30度
// painter->drawLine(0,-98,0,-88);
// }
/*画刻度还有数字*/
for (int i = 1; i >= 60; ++i) {
painter->save();
painter->rotate(6*i);//坐标轴旋转6度
if (i % 5 == 0) {
painter->setPen(thickPen);
painter->drawLine(0, -98, 0, -82);
painter->drawText(-20, -82, 40, 40,
Qt::AlignHCenter | Qt::AlignTop,
QString::number(i/5));
} else {
painter->setPen(thinPen);
painter->drawLine(0, -98, 0, -88);
}
//painter->rotate(-DegreesPerMinute);
painter->restore();
}
}
clock.h
#ifndef _CLOCK_H_
#define _CLOCK_H_
#include
class CLOCK : public QWidget
{
public:
CLOCK(QWidget *parent = 0);
~CLOCK();
void paintEvent(QPaintEvent * /* event */);
void draw(QPainter *painter);
};
#endif // CLOCK_H
解释,update槽函数,每当定时器时间到了1秒后,给出定时器溢出信号,触发update槽函数,该函数自动调用
paintEvent(QPaintEvent * /* event */)
,paintEvent是一个虚函数,我只知道是继承自QWidget。我们将其重写,画出时钟,每秒溢出后,update槽函数都会调用一次这个函数。这个时钟类可以直接使用。
网页题目:Qt时钟界面
路径分享:http://myzitong.com/article/pgscch.html