Qt学习(五)-----自定义电子时钟

来源:互联网 发布:网络招生策划方案 编辑:程序博客网 时间:2024/04/30 07:37

一、自定义电子时钟的实现
自己参照(http://blog.csdn.net/yiyaaixuexi/article/details/6747737),然后自己琢磨实现了一下,在ui设计器上添加几个6个Button(显示数字)和两个label(“:”);通过setIcon设置图标,将图片显示到Button上

二、代码实现

1、新建项目Clock,基类选择Widget
这里写图片描述

2、将所需的图片复制到工程目录下

这里写图片描述

3、widget.h

添加private slots:    void  showTimeSlot();//显示时间

4、widget.cpp

在析构函数添加QTimer *timer = new QTimer(this);connect(timer, SIGNAL(timeout()), this, SLOT(showTimeSlot()));timer->start(1000);showTimeSlot();

添加自定义函数

void Widget::showTimeSlot(){    //lcd       QTime time = QTime::currentTime();       QString text = time.toString("hh:mm:ss");       ui->label_3->setText(text);       //ourclock       ui->hourh->setIcon(QPixmap(this->getPngName(text[0])));       ui->hourl->setIcon(QPixmap(this->getPngName(text[1])));       ui->minh->setIcon(QPixmap(this->getPngName(text[3])));       ui->minl->setIcon(QPixmap(this->getPngName(text[4])));       ui->sech->setIcon(QPixmap(this->getPngName(text[6])));       ui->secl->setIcon(QPixmap(this->getPngName(text[7])));}QString Widget::getPngName(QChar x){     return "../Clock/"+(x+QString(".png"));//获取图片路径}

三。运行结果

这里写图片描述

5 0