一、项目要求
设计一个电子相册,点击上一张,切换到上一张图片,点击下一张,切换到下一张图片。
要求:图片的展示可以循环(QList<QString>)
要求:界面美观
二、项目代码
本质是通过添加图片进入到程序中,然后根据要求切换这些图片
在ui界面上设置四个pushbutton和一个Qlabel,按钮通过槽函数控制图片切换,Qlabel用来展示图片
cpp
//头文件dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPixmap>
#include <QSize>
#include<QList>
#include <QDebug>
#include <QTimer>
#include <QDateTime>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QTimer *timer;
private slots:
void btnClickSlot( );
void btnClickSlot1( );
void btnClickSlot2( );
void btnClickSlot3( );
void timeoutSlot();
};
#endif // DIALOG_H
cpp
//dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
static int flag=1;
static int flag2=1;
int lastPic;
QList<QString> alt;
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
QString s1(":/new/prefix1/1.png");
QString s2(":/new/prefix1/2.png");
QString s3(":/new/prefix1/3.png");
QString s4(":/new/prefix1/4.png");
QString s5(":/new/prefix1/5.png");
QString s6(":/new/prefix1/6.png");
QString s7(":/new/prefix1/7.png");
QString s8(":/new/prefix1/8.png");
QString s9(":/new/prefix1/9.png");
QString s10(":/new/prefix1/10.png");
QString s11(":/new/prefix1/11.png");
QString s12(":/new/prefix1/12.png");
alt.prepend(s1);// 向前追加
alt.append(s2);// 向后追加
alt.append(s3);// 向后追加
alt.append(s4);// 向后追加
alt.append(s5);// 向后追加
alt.append(s6);// 向后追加
alt.append(s7);// 向后追加
alt.append(s8);// 向后追加
alt.append(s9);// 向后追加
alt.append(s10);// 向后追加
alt.append(s11);// 向后追加
alt.append(s12);// 向后追加
connect(ui->pushButton,SIGNAL(clicked()),
this,SLOT(btnClickSlot()));
connect(ui->pushButton_2,SIGNAL(clicked()),
this,SLOT(btnClickSlot1()));
connect(ui->pushButton_3,SIGNAL(clicked()),
this,SLOT(btnClickSlot2()));
connect(ui->pushButton_4,SIGNAL(clicked()),
this,SLOT(btnClickSlot3()));
QPixmap pic(alt.at(flag));
QSize size(ui->label->width(),ui->label->height());
pic = pic.scaled(size,Qt::IgnoreAspectRatio,Qt::FastTransformation);
ui->label->setPixmap(pic);
timeoutSlot();
// 创建定时器对象
timer = new QTimer(this);
// 设置触发时间
timer->setInterval(50);
// 设置为周期触发
timer->setSingleShot(false);
connect(timer,SIGNAL(timeout()),
this,SLOT(timeoutSlot()));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::btnClickSlot()
{
flag++;
if(flag==6)
{
flag=0;
}
qDebug()<<"右翻";
QPixmap pic(alt.at(flag));
QSize size(ui->label->width(),ui->label->height());
pic = pic.scaled(size,Qt::IgnoreAspectRatio,Qt::FastTransformation);
ui->label->setPixmap(pic);
}
void Dialog::btnClickSlot1()
{
flag--;
if(flag<0)
{
flag=11;
}
qDebug()<<"左翻";
QPixmap pic(alt.at(flag));
QSize size(ui->label->width(),ui->label->height());
pic = pic.scaled(size,Qt::IgnoreAspectRatio,Qt::FastTransformation);
ui->label->setPixmap(pic);
}
//定时
void Dialog::timeoutSlot()
{
flag++;
if(flag==12)
{
flag=0;
}
qDebug()<<"变化";
QPixmap pic(alt.at(flag));
QSize size(ui->label->width(),ui->label->height());
pic = pic.scaled(size,Qt::IgnoreAspectRatio,Qt::FastTransformation);
ui->label->setPixmap(pic);
}
//按钮切换
void Dialog::btnClickSlot2()
{
flag2++;
if(flag2>1)
{
flag2=0;
}
if(flag2==0)
{
timer->start();
qDebug()<<"循环";
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(false);
ui->pushButton_4->setEnabled(false);
ui->pushButton_3->setText("停止");
}
else if(flag2==1)
{
qDebug()<<"单步";
timer->stop();
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(true);
ui->pushButton_4->setEnabled(true);
ui->pushButton_3->setText("自动");
}
}
//随机
void Dialog::btnClickSlot3()
{
qint64 time=QDateTime::currentMSecsSinceEpoch();
qsrand(time);
int rand=qrand()%12;
if(rand==lastPic)
{
btnClickSlot3();
}
lastPic=rand;
QPixmap pic(alt.at(rand));
QSize size(ui->label->width(),ui->label->height());
pic = pic.scaled(size,Qt::IgnoreAspectRatio,Qt::FastTransformation);
ui->label->setPixmap(pic);
qDebug()<<"随机";
}