现在的位置: 首页 > 综合 > 正文

qt4第二例:mini图片浏览器

2012年09月11日 ⁄ 综合 ⁄ 共 2710字 ⁄ 字号 评论关闭

很简单的一个图片浏览器,可以从打开按钮打开图片文件。上一张和下一张的功能还没实现。程序代码如下

头文件: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();
}

 

 

抱歉!评论已关闭.