Ubuntu上Qt之簡單圖片瀏覽器

 >>主要功能:ide

    (1)圖片切換瀏覽,上一張/下一張oop

    (2)圖片放大、縮小。包括兩種機制:鼠標滾輪和按鈕放大/縮小。ui

    (3)圖片自動循環播放,間隔2s。點擊播放後,其餘操做均無效,直至點擊暫停this

    (4)圖片順時鐘旋轉,每次90度。可在放大/縮小狀態下旋轉,或者相反。spa

    (5)在圖片被放大/縮小/旋轉後,點擊還原或者切換圖片時,自動恢復爲默認大小。code

 >>最終效果:orm

(1)點擊播放按鈕:blog

(2)暫停後,點擊下一張:圖片

(3)點擊放大(或鼠標滾輪往前滾動):資源

(4)點擊還原:

(5)點擊縮小(或鼠標滾輪日後滑動):

(6)點擊上一張:

(7)點擊旋轉:

(8)點擊縮小(或鼠標滾輪日後):

(9)點擊還原:

 

>>添加程序:

  新建Gui工程,名爲MyPictureView,類名MyPictureView,基類選擇QWidget。

mypictureview.h

#ifndef MYPICTUREVIEW_H #define MYPICTUREVIEW_H #include <QWidget> #include <QVector> #include <QPixmap> #include <QTimer> #include <QFileDialog> #include <QString> #include <QLabel> #include <QWheelEvent>


namespace Ui { class MyPictureView; } class MyPictureView : public QWidget { Q_OBJECT public: MyPictureView(QVector<QPixmap *> &pictures); ~MyPictureView(); private slots: void on_btn_prev_clicked(); void on_btn_spin_clicked(); void on_btn_play_stop_clicked(); void on_btn_orig_clicked(); void on_btn_next_clicked(); void on_btn_big_clicked(); void on_btn_smal_clicked(); void wheelEvent(QWheelEvent * event); void pic_showloop(); private: Ui::MyPictureView *ui; QVector<QPixmap *>  &pictures_; QTimer *timer; QPixmap pix; QLabel *label; void pic_show1(); void pic_show2(); bool isPlaying; float scale; int size; int currentIndex; int imageAngle; }; #endif // MYPICTUREVIEW_H
mypictureview.h

mypictureview.cpp

#include "mypictureview.h" #include "ui_mypictureview.h" MyPictureView::MyPictureView(QVector<QPixmap *> &pictures) : pictures_(pictures), ui(new Ui::MyPictureView) { ui->setupUi(this); scale = 1; currentIndex = 0;     // Current picture index
    size = pictures_.size(); isPlaying = false; imageAngle = 0; label = new QLabel; ui->scrollArea->setWidget(label); ui->scrollArea->setAlignment(Qt::AlignCenter);   //顯示位置,對齊方式
 timer = new QTimer; timer->setInterval(2 * 1000);   //2s
    connect(timer,SIGNAL(timeout()),this,SLOT(pic_showloop())); label->setAlignment(Qt::AlignCenter); if(size > 0) pic_show1(); } MyPictureView::~MyPictureView() { delete ui; } void MyPictureView::on_btn_prev_clicked()       //上一張
{ timer->stop(); scale = 1; imageAngle = 0; currentIndex--; if(currentIndex<0) currentIndex=size-1; pic_show1(); } void MyPictureView::on_btn_spin_clicked()       //旋轉
{ imageAngle += 1; imageAngle = imageAngle % 4; pic_show2(); } void MyPictureView::on_btn_play_stop_clicked()      //播放、暫停,間隔2s
{ isPlaying = !isPlaying; if(isPlaying) { ui->btn_play_stop->setText(tr("暫停")); timer->start(); ui->btn_big->setEnabled(false); ui->btn_next->setEnabled(false); ui->btn_orig->setEnabled(false); ui->btn_prev->setEnabled(false); ui->btn_smal->setEnabled(false); ui->btn_spin->setEnabled(false); } else { ui->btn_play_stop->setText(tr("播放")); timer->stop(); ui->btn_big->setEnabled(true); ui->btn_next->setEnabled(true); ui->btn_orig->setEnabled(true); ui->btn_prev->setEnabled(true); ui->btn_smal->setEnabled(true); ui->btn_spin->setEnabled(true); } } void MyPictureView::on_btn_orig_clicked()       //還原
{ timer->stop(); scale = 1; imageAngle = 0; pic_show1(); } void MyPictureView::on_btn_next_clicked()       //下一張
{ timer->stop(); scale = 1; imageAngle = 0; currentIndex++; if(currentIndex>size-1) currentIndex = 0; pic_show1(); } void MyPictureView::on_btn_big_clicked()        //放大
{ timer->stop(); scale = scale*1.25;         //和0.8對應,放大次數和縮小次數點擊相同次,會還原到原來的樣子

    if(imageAngle != 0) pic_show2(); else pic_show1(); } void MyPictureView::on_btn_smal_clicked()       //縮小
{ timer->stop(); scale = scale*0.8; if(imageAngle != 0) pic_show2(); else pic_show1(); } void MyPictureView::wheelEvent(QWheelEvent *event)          //滾輪放大或縮小
{ int num = event->delta(); if(!isPlaying) { if(num > 0) on_btn_big_clicked(); else on_btn_smal_clicked(); } } void MyPictureView::pic_show1()          //顯示圖片
{ pix = (*pictures_[currentIndex]).scaled(640*scale,360*scale,Qt::KeepAspectRatio); label->setPixmap(pix); } void MyPictureView::pic_show2()     //旋轉後的顯示
{ QMatrix leftmatrix; leftmatrix.rotate(90*imageAngle); pix = (*pictures_[currentIndex]).scaled(640*scale,360*scale,Qt::KeepAspectRatio); label->setPixmap(pix.transformed(leftmatrix,Qt::SmoothTransformation)); } void MyPictureView::pic_showloop()      //循環顯示圖片
{ scale = 1; currentIndex ++; if(currentIndex > size-1) currentIndex = 0; pic_show1(); }
mypictureview.cpp

main.cpp

#include "mypictureview.h" #include <QApplication>

int main(int argc, char *argv[]) { QApplication a(argc, argv); QVector<QPixmap *> pictures; pictures.push_back(new QPixmap(":/pictures/1")); pictures.push_back(new QPixmap(":/pictures/2")); pictures.push_back(new QPixmap(":/pictures/3")); pictures.push_back(new QPixmap(":/pictures/4")); pictures.push_back(new QPixmap(":/pictures/5")); pictures.push_back(new QPixmap(":/pictures/6")); pictures.push_back(new QPixmap(":/pictures/7")); pictures.push_back(new QPixmap(":/pictures/8")); MyPictureView view(pictures); // Disable maximize and minimize button
 view.setWindowFlags(view.windowFlags() & ~Qt::WindowMaximizeButtonHint & ~Qt::WindowMinimizeButtonHint); view.show(); return a.exec(); }
main.cpp

 添加資源:

(1)資源名隨便起一個,我這裏起的是pictures,而後我在工程目錄下新建了一個文件夾來存放圖片資源,取名爲picture。

(2)在資源管理器中,先修改一下前綴,能夠直接輸入一個「/」  。我這裏輸入的是「/pictures」。

(3)能夠選擇爲每一個圖片起一個別名,我這裏分別用數字1~8代替圖片的名字。

 如今,咱們就能夠使用QPixmap(":/pictures/1")來加載別名爲1的圖片。

相關文章
相關標籤/搜索