QT中用label標簽顯示本地圖片的方法


#define CIMAGE_H
#include<QtGui/QDialog>
#include<QLabel>

class QLabel;
class QLineEdit;
class QPushButton;

class CImage:public QDialog
{
Q_OBJECT
public:
CImage(QWidget *parent=0);
virtual ~CImage() {}
private:
QLabel *ImageLabel;
QPushButton *ImageButton;
public slots:
void IsertImage();
};

#endif // CIMAGE_H


#include <QtGui/QtGui>
#include <QMovie>
#include "CImage.h"

CImage::CImage(QWidget *parent):QDialog(parent)
{
ImageButton=new QPushButton(tr("瀏覽"));
ImageLabel=new QLabel(tr("圖片"));
QVBoxLayout *vboxLayout=new QVBoxLayout;
vboxLayout->addWidget(ImageLabel);
vboxLayout->addWidget(ImageButton);
setLayout(vboxLayout);

connect(ImageButton,SIGNAL(clicked()),this,SLOT(IsertImage()));
setWindowTitle(tr("圖片預覽"));
resize(200,300);
}

void CImage::IsertImage()
{
ImageButton=qobject_cast<QPushButton*>(sender());
QString fileName=QFileDialog::getOpenFileName( this,
tr("打開文件"),
"/usr/local/Trolltech",
tr("任何文件(*.*)"
";;文本文件(*.txt)"
";;XML文件(*.xml)"
";;Images (*.png *.xpm *.jpg)"));
QMovie *move=new QMovie(fileName);

ImageLabel->setMovie(move);
move->start();
}


#include<QtGui/QtGui>
#include"CImage.h"

int main(int argc,char*argv[])
{
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("gb18030"));
CImage dlg;
return dlg.exec();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM