qt4第二例:mini图片浏览器
2007-06-08 23:46
127 查看
很简单的一个图片浏览器,可以从打开按钮打开图片文件。上一张和下一张的功能还没实现。程序代码如下
头文件: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();
}
头文件: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();
}
相关文章推荐
- QT4 DESIGNER 初体验
- qt4中如何调用C函数(linux下)
- QT4向s3c2410平台移植
- Qt4 學習筆記[转]
- Qt4中如何去掉窗口标题栏
- Qt4怎样设置应用程序的图标-windows平台
- 在QT4中使用类似QVBox类似功能
- linux窗口程序设计 Qt4精彩实例分析 pyqt实现 实例2 标准对话框的使用
- qmake使用实践:包含动态库的Qt4工程
- apt-get 安装QT4
- 用KDevelop开发Qt4程序
- vs2005+qt4 问题记录
- QT4_SDK
- qt4 sqlite fedora
- 使用QT4和OSSBuild的gstreamer库开发视频播放程序
- Qt4中的QButtonGroup类使用
- redhat enterprise 5 下安装QT4
- QT4的键盘事件
- QT4 用QLabel实时显示时间
- Qt4精彩实例分析笔记--各个位置信息