QT實現電子相簿
1、佈局按鈕和顯示部分
包含10個按鍵,開啟,播放,暫停,上一張,下一張,放大,縮小,正常,恢復,旋轉等;下面新增Qlable控制元件顯示圖片的總頁數和當前頁,空間背景為自定義圖片背景;
2、定義相關成員變數和成員函式
void setBtnIcon(QToolButton *Btn, const QString &path_image);//設定按鍵的背景圖片時呼叫
QLabel *lable; // 顯示圖片的總頁數和當前頁
QTimer *timer; //當按下播放按鈕時啟動定時器
QPixmap pix; //顯示圖片
int i;
int j;
int mm;
QString imageList[100]; //圖片名稱字串list
//qreal是Qt的資料型別,在桌面作業系統中(比如Windows, XNix等)qreal其實就是double型別;
//而在嵌入裝置系統中,qreal則等同於float 型別。
qreal w,h;
QString image_sum,image_position;
QStringList::Iterator it;
QMatrix matrix;//旋轉時用到
3、定義是用到的槽函式
void displayImage();
void on_open_clicked();
void on_litte_clicked();
void on_large_clicked();
void on_recover_clicked();
void on_rotate_clicked();
void on_stop_clicked();
void on_normal_clicked();
void on_previous_clicked();
void on_next_clicked();
主要程式:
1、 widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
i= 1;
mm = 0;
QImage image;
image.load(":/image/6631506470840007614.jpg");
QPalette palette;
palette.setBrush(this->backgroundRole(),QBrush(image));
this->setPalette(palette);
setBtnIcon(ui->open, ":/image/open.png");
setBtnIcon(ui->large, ":/image/big.png");
setBtnIcon(ui->litte, ":/image/litte.png");
setBtnIcon(ui->next, ":/image/next.png");
setBtnIcon(ui->previous, ":/image/previous.png");
setBtnIcon(ui->normal, ":/image/normal.png");
setBtnIcon(ui->recovery, ":/image/recover.png");
setBtnIcon(ui->abc, ":/image/start.png");
setBtnIcon(ui->stop, ":/image/stop.png");
setBtnIcon(ui->rotate, ":/image/zhuan.png");
setBtnIcon(ui->open, ":/image/open.png");
//set button is disable
//ui->open->setEnabled(false);
ui->large->setEnabled(false);
ui->litte->setEnabled(false);
ui->next->setEnabled(false);
ui->normal->setEnabled(false);
ui->previous->setEnabled(false);
ui->recovery->setEnabled(false);
ui->rotate->setEnabled(false);
ui->abc->setEnabled(false);
ui->stop->setEnabled(false);
lable = new QLabel(this);
ui->scrollArea->setWidget(lable);
ui->scrollArea->setAlignment(Qt::AlignCenter);
ui->image_number->setText(tr("0 / 0"));
timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(displayImage()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::setBtnIcon(QToolButton *Btn, const QString &path_image)
{
Btn->setStyleSheet("QToolButton{border:Opx}");
Btn->setIconSize(QSize(Btn->width(),Btn->height()));
QPixmap pixmap(path_image);
Btn->setIcon(QIcon(pixmap));
}
void Widget::displayImage(void)
{
qDebug() << "displayImage" <<endl;
//載入圖片陣列從第一個開始
pix.load(imageList[i]);
w = lable->width();
h = lable->height();
//設定圖片的大小和LABLE的大小,充滿控制元件顯示
pix = pix.scaled(w, h, Qt::IgnoreAspectRatio);
lable->setPixmap(pix);
image_position = QString::number(i);
i++;
if (i>j)
{
i=1;
}
//以 共幾張/第幾張 格式顯示
ui->image_number->setText(tr("%1 / %2").arg(image_sum).arg(image_position));
}
void Widget::on_open_clicked()
{
int k = 0;
QStringList list = QFileDialog::getOpenFileNames(this, tr("新增圖片"),"/","*.png *.jpg");
for (it = list.begin(); it < list.end();it++)
{
k++;
imageList[k]= *it;
}
j = k;
image_sum = QString::number(k);
image_position = QString::number(0);
ui->image_number->setText(tr("%1 / %2").arg(image_sum).arg(image_position));
//啟用按鍵
ui->open->setEnabled(true);
ui->large->setEnabled(true);
ui->litte->setEnabled(true);
ui->next->setEnabled(true);
ui->normal->setEnabled(true);
ui->previous->setEnabled(true);
ui->recovery->setEnabled(true);
ui->rotate->setEnabled(true);
ui->abc->setEnabled(true);
ui->stop->setEnabled(true);
}
void Widget::on_abc_clicked()
{
qDebug() << "on_start_clicled" <<endl;
timer->start(1000);
}
/*void Widget::on_start_clicked()
{
qDebug() << "on_start_clicled" <<endl;
timer->start(1000);
}*/
void Widget::on_litte_clicked()
{
pix.load(imageList[i]);
w *= 0.8;
h *= 0.8;
pix = pix.scaled(w,h);
lable->setPixmap(pix);
}
void Widget::on_large_clicked()
{
pix.load(imageList[i]);
w *= 1.2;
h *= 1.2;
pix = pix.scaled(w,h);
lable->setPixmap(pix);
}
void Widget::on_recover_clicked()
{
pix.load(imageList[i]);
pix = pix.scaled(315,205,Qt::IgnoreAspectRatio);
lable->setPixmap(pix);
}
void Widget::on_rotate_clicked()
{
timer->stop();
matrix.rotate(90);
pix = pix.transformed(matrix,Qt::FastTransformation);
pix = pix.scaled(lable->width(),lable->height(),Qt::IgnoreAspectRatio);
lable->setPixmap(pix);
}
void Widget::on_stop_clicked()
{
qDebug() << "on_stop_clicked" <<endl;
timer->stop();
}
void Widget::on_normal_clicked()
{
pix.load(imageList[i]);
lable->setPixmap(pix);
}
void Widget::on_previous_clicked()
{
timer->stop();
i--;
if (i<1)
i=j;
image_position = QString::number(i);
pix.load(imageList[i]);
w = lable->width();
h = lable->height();
pix = pix.scaled(w,h,Qt::IgnoreAspectRatio);
lable->setPixmap(pix);
ui->image_number->setText(tr("%1 / %2").arg(image_sum).arg(image_position));
}
void Widget::on_next_clicked()
{
}
2、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "QToolButton"
#include "QLabel"
#include "QTimer"
#include "QString"
#include "QPixmap"
#include "QPalette"
#include "QMatrix"
#include "QImage"
#include "QBrush"
#include "QFileDialog"
#include "QMessageBox"
#include "QDebug"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
void setBtnIcon(QToolButton *Btn, const QString &path_image);
QLabel *lable;
QTimer *timer;
QPixmap pix;
int i;
int j;
int mm;
QString imageList[100];
//qreal是Qt的資料型別,在桌面作業系統中(比如Windows, XNix等)qreal其實就是double型別;
//而在嵌入裝置系統中,qreal則等同於float 型別。
qreal w,h;
QString image_sum,image_position;
QStringList::Iterator it;
QMatrix matrix;//旋轉時用到
private slots:
void displayImage();
void on_open_clicked();
void on_litte_clicked();
void on_large_clicked();
void on_recover_clicked();
void on_rotate_clicked();
void on_stop_clicked();
void on_normal_clicked();
void on_previous_clicked();
void on_next_clicked();
private slots:
void on_abc_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
3、其它函式沒有改變
4、執行介面如下所示: