一、自定义电子时钟的实现
自己参照,然后自己琢磨实现了一下,在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"));//获取图片路径
}
三。运行结果