很簡單的一個圖片瀏覽器,可以從開啟按鈕開啟圖片檔案。上一張和下一張的功能還沒實現。程式碼如下
標頭檔: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();
}