今天写个自己做的小时钟,这是在放假在家的一个晚上,发现房间里的时钟看着很不舒服,首先:
1、没有夜光,半夜起来了想看时间不知道几点
2、每走一秒秒针发出的声音哒哒哒的很大,嘈杂环境感觉不到,但是关灯后准备入睡声音尤其清楚
3、最重要的一点,钟表没电了一时间我找不到5号电池
用QT制作一个简单的时钟,开着我的笔记本在那里开一个晚上就行,所以开QT弄了个简单的时钟(主要我记得在QT的例子里有个时钟的小样,所以代码可以直接参考,省了不少功夫)
网上找了些好看的照片作为背景,自己用画板画了个表盘和各个指针,最后看起来还像那么回事:)
今天自己又打开这个程序做了些修改(无非就是改改背景,改改颜色),有考虑加些功能上去,比如弄个可以调节指针颜色的,弄个可以选择背景图片的,后来放弃了不想弄了,简单才是美,弄这个的时候就想弄个极简的,也就不想去弄那么多花里胡哨的功能了
void Widget::paintEvent(QPaintEvent *event){
QPainter painter2(this);
QPixmap pix;
pix.load(":/new/prefix1/image/b_back2.jpg");//图片的位置
painter2.drawPixmap(0,0,this->width(),this->height(),pix);//根据窗口的宽高来将图片画在窗口上
painter2.setRenderHint(QPainter::Antialiasing); //反走样?
painter2.translate(this->width()/2,this->height()/3); //设置后面画表盘等东西时的相对坐标的原点
int side = qMin(this->width(), this->height()); //取较小的值?
painter2.scale(side / 300.0, side / 300.0); //用于窗口像素大小
static const QPointF minute_points[3] = {
QPointF(3, -10),
QPointF(-3, -10),
QPointF(0, -60)
};
static const QPointF hour_points[3] = {
QPointF(5, -7),
QPointF(-5, -7),
QPointF(0, -30)
};
static const QPointF second_points[3] = {
QPointF(1, -20),
QPointF(-1, -20),
QPointF(0, -87)
};
static const QPointF one_points[3] = {
QPointF(-4, -101),
QPointF(4, -101),
QPointF(0, -99)
};
QPen pen1,pen2;
pen1.setColor(Qt::white);
pen1.setStyle(Qt::SolidLine);
painter2.setBrush(QBrush(Qt::white,Qt::SolidPattern));
pen1.setWidth(1);
painter2.setPen(pen1);
QTime time = QTime::currentTime();
painter2.save(); //保存当前画家状态
painter2.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter2.drawConvexPolygon(hour_points, 3);
painter2.drawEllipse(QPoint(0,-93),3,3);
painter2.restore(); //还原当前画家状态
painter2.save();
painter2.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter2.drawConvexPolygon(minute_points, 3);
painter2.drawEllipse(QPoint(0,-93),2,2);
painter2.restore();
painter2.save();
painter2.rotate(6.0 * (time.second() + time.msec() /1000.0));
painter2.drawConvexPolygon(second_points, 3);
//painter2.drawEllipse(QPoint(0,-96),1,1);
painter2.drawConvexPolygon(one_points, 3);
painter2.restore();
//painter2.drawEllipse(QPoint(0,0),3,3); //中心那个小圆
//表盘
//秒盘
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter2.drawLine(91, 0, 95, 0);
painter2.rotate(6.0);
}
for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter2.drawLine(93, 0, 95, 0);
painter2.rotate(6.0);
}
//十分钟位置钟盘,加长秒盘而已
for (int i = 0; i < 12; ++i) {
painter2.drawLine(87, 0, 95, 0);
painter2.rotate(30.0);
}
其他部分(头文件,main函数)没有什么功能方面的,喜欢这个钟表的可以自己也做个噢