qt4第二例:mini圖片瀏覽器

來源:互聯網
上載者:User

很簡單的一個圖片瀏覽器,可以從開啟按鈕開啟圖片檔案。上一張和下一張的功能還沒實現。程式碼如下

標頭檔:picbrowser.h

/**//*
*        檔案:picbrowser.h
*        作者:yangdk
*        Email:jidacun@163.com
*        首頁:http://blog.csdn.net/yang_dk
*/

#ifndef _PICBROWSER_H_
#define _PICBROWSER_H_

#include <QDialog>

class QPushButton;
class QPixmap;
class QFileDialog;
class QHBoxLayout;    
class QVBoxLayout;
class QLabel;    //放圖片

class PicBrowser:public QDialog
...{
    Q_OBJECT
public:
    PicBrowser(QWidget *parent=0);

private:
    QPushButton *openButton,*closeButton,*prevButton,*nextButton;
    QPixmap            *pixmap;
    QHBoxLayout *bottom;
    QVBoxLayout *mainLayout;
    QLabel            *picLabel;
    
public slots:
    void openPic();
    void prevPic();
    void nextPic();
    void exitapp();
};

#endif

picbrowser.cpp

 

/**//*
*        檔案:picbrowser.cpp
*        作者:yangdk
*        Email:jidacun@163.com
*        首頁:http://blog.csdn.net/yang_dk
*/

#include "picBrowser.h"
#include <QtGui>
#include <iostream.h>

PicBrowser::PicBrowser(QWidget *parent):QDialog(parent)
...{
    openButton = new QPushButton(tr("open"));
    closeButton= new QPushButton(tr("close"));
    prevButton = new QPushButton(tr("prev"));
    nextButton = new QPushButton(tr("next"));
    connect(openButton,SIGNAL(clicked()),this,SLOT(openPic()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(exit(0)));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(exitapp()));
    connect(prevButton,SIGNAL(clicked()),this,SLOT(prevPic()));
    connect(nextButton,SIGNAL(clicked()),this,SLOT(nextPic()));
    
    pixmap = new QPixmap("1.jpg");        //預設開啟目前的目錄下的1.jpg檔案
    picLabel=new QLabel;                            //使用一個Label來顯示圖片,將圖片作為Label的Icon而已,呵呵
    picLabel->setPixmap(*pixmap);
    
    //pixmap->setRange
    

    
    //布局
    bottom = new QHBoxLayout;
    bottom->addWidget(openButton);
    bottom->addWidget(prevButton);
    bottom->addWidget(nextButton);
    bottom->addWidget(closeButton);
    
    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(picLabel);
    mainLayout->addLayout(bottom);
    
    setLayout(mainLayout);
}

void PicBrowser::openPic()
...{
    //開啟一張圖片    
    QString filename=QFileDialog::getOpenFileName(this,"open a picture","/",tr("Images(*.bmp *.jpg *.png)"));
    pixmap->load(filename);
    picLabel->setPixmap(*pixmap);
}

void PicBrowser::prevPic()
...{ 

        QMessageBox::information(this,"xiaoxi","not implement,please choose open",QMessageBox::Close);

}

void PicBrowser::nextPic()
...{
    QMessageBox::information(this,"xiaoxi","not implement,please choose open",QMessageBox::Close);
}
void PicBrowser::exitapp()
...{
    exit(0);
}

main.cpp

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

int main(int argc,char *argv[])
...{
    QApplication app(argc,argv);
    PicBrowser picBS;
    return picBS.exec();
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.