#include "widget.h" #include "ui_widget.h" #include <QTime> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //显示系统时间 //实例化对象 this->timer=new QTimer(this); QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //信号和槽函数关联 connect(this->timer,&QTimer::timeout,this,&Widget::update_time); //启动定时器 this->timer->start(1000); //启动计时器 this->count=0; //实例化对象 this->timer2=new QTimer(this); //信号和槽函数关联 connect(this->timer2,&QTimer::timeout,this,&Widget::update_time2); //开始计时 this->timer2->start(1000); } Widget::~Widget() { delete ui; } void Widget::on_home_btn_clicked() { //首页 } void Widget::on_per_btn_clicked() { //上一页 } void Widget::on_next_btn_clicked() { //下一页 } void Widget::on_last_btn_clicked() { //尾页 } void Widget::update_time() { //获取系统时间 QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //显示 this->ui->time_lab->setText(curtime); } void Widget::update_time2() { //计时 this->count++; QTime time(0,0,0,0); //QTime转换成QString QString sec=time.addSecs(this->count).toString("mm:ss"); //更新到LCDNumber显示 this->ui->lcdNumber->display(sec); }增加代码使用QStringList实现电子相册
时间: 2023-06-16 11:07:26 浏览: 183
QT.rar_Qt clock
在Widget类的构造函数中添加以下代码:
```cpp
//初始化图片列表
QStringList images;
images.append(":/images/image1.jpg");
images.append(":/images/image2.jpg");
images.append(":/images/image3.jpg");
images.append(":/images/image4.jpg");
images.append(":/images/image5.jpg");
images.append(":/images/image6.jpg");
images.append(":/images/image7.jpg");
images.append(":/images/image8.jpg");
images.append(":/images/image9.jpg");
images.append(":/images/image10.jpg");
//保存图片列表到成员变量中
this->images = images;
//设置初始显示图片
this->current_image_index = 0;
this->ui->image_label->setPixmap(QPixmap(this->images.at(this->current_image_index)));
```
然后添加以下槽函数实现图片切换:
```cpp
void Widget::on_previous_btn_clicked()
{
//切换到上一张图片
this->current_image_index--;
if (this->current_image_index < 0) {
this->current_image_index = this->images.size() - 1;
}
this->ui->image_label->setPixmap(QPixmap(this->images.at(this->current_image_index)));
}
void Widget::on_next_btn_clicked()
{
//切换到下一张图片
this->current_image_index++;
if (this->current_image_index >= this->images.size()) {
this->current_image_index = 0;
}
this->ui->image_label->setPixmap(QPixmap(this->images.at(this->current_image_index)));
}
```
最后在.ui文件中添加两个按钮,命名为`previous_btn`和`next_btn`,并将它们分别与`on_previous_btn_clicked()`和`on_next_btn_clicked()`槽函数关联即可。
阅读全文