使用带有UI界面的QWidget实现电子相册
1、实现功能
1、定时器的使用,在当前页面的停止总时长。
2、显示当前时间
3、图片的上一张与下一张
4、图片的显示
5、进度展示、一共十张图片、进度条的初始值为10。
2、widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QDateTime>
#include <QLabel>
#include <QTimer>
#include<QStringList>
#include<QtDebug>
#include<QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();public slots://自己新增的槽函数是publicvoid update_text();void update_time();//UI界面,新增的槽函数是private
private slots:void on_down_btn_clicked();void on_up_btn_clicked();private:Ui::Widget *ui;QTimer *time;//当前时间,定时器对象QTimer *timer;//定时器对象,计时器QTime *time1;//时间对象,时分秒,计时器QStringList imageList;int index;
};
#endif // WIDGET_H
3、widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//1.实现当前时间this->time = new QTimer(this);//定时器的实例化this->time->setInterval(1000);//设置时长、毫秒//信号和槽函数的关联,要在启动之前,先关联好,QT5connect(this->time,&QTimer::timeout,this,&Widget::update_time);this->time->start();//启动定时器//2.实现计时器this->timer = new QTimer(this);//this->time1 = new QTime(0,0,0);//设计时间的初始值,从零开始,是按秒更新this->time1 = new QTime(0,0,0,0);//设计时间的初始值,从零开始,是按毫秒更新//this->timer->start(1000);//1000毫秒是1秒,这个是按秒更新this->timer->start(1);//多久启动一次,单位是毫秒,这个是按1毫秒更新//QT4的连接connect(this->timer,SIGNAL(timeout()),this,SLOT(update_text()));//槽函数要另外定义,private slots://3.图片展示this->ui->img_lab->setPixmap(QPixmap("./image/0.jpg").scaled(this->ui->img_lab->size()));imageList << "./image/0.jpg" << "./image/1.jpg"<< "./image/2.jpg"<< "./image/3.jpg"<< "./image/4.jpg"<< "./image/5.jpg"<< "./image/6.jpg"<< "./image/7.jpg"<< "./image/8.jpg"<< "./image/9.jpg";index=0;//4.进度条显示this->ui->progressBar->setRange(0,100);//先给进度条设置范围this->ui->progressBar->setValue(10);//再设置初始值
}Widget::~Widget()
{delete ui;
}void Widget::update_time()
{//获取当前的系统时间QString time = QDateTime::currentDateTime().toString("yyyy年MM月dd日 hh:mm:ss");//给标签设置时间this->ui->loctime_lab->setText(time);
}void Widget::update_text()
{//*this->time1=this->time1->addSecs(1);//秒*this->time1=this->time1->addMSecs(1);//毫秒//this->ui->counttime_lab->setText(this->time1->toString("hh:mm:ss"));//使用标签完成时间的显示,不太合适//使用QLCDNumber完成时间的显示,更符合场景//this->ui->lcdNumber->display(this->time1->toString("hh:mm:ss"));//秒this->ui->lcdNumber->display(this->time1->toString("hh:mm:ss:zzz"));//毫秒
}void Widget::on_down_btn_clicked()
{if(index<9){index = (index + 1) % imageList.size(); // 循环显示图片QPixmap pixmap(imageList.at(index));pixmap = pixmap.scaled(ui->img_lab->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);ui->img_lab->setPixmap(pixmap);//进度条大小this->ui->progressBar->setValue((index+1)*10);}else{QMessageBox::warning(this, "提示", "已经是最后一张了");}}void Widget::on_up_btn_clicked()
{if(index>0){index = (index - 1) % imageList.size(); // 循环显示图片QPixmap pixmap(imageList.at(index));pixmap = pixmap.scaled(ui->img_lab->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);ui->img_lab->setPixmap(pixmap);//进度条大小this->ui->progressBar->setValue((index+1)*10);}else{QMessageBox::information(this, "提示", "已经是第一张了");}//三种关联方式:QT5、QT4、匿名函数
// connect(this->time,&QTimer::timeout,this,&Widget::update_time);
// connect(this->timer,SIGNAL(timeout()),this,SLOT(update_text()));}