利用QT實現(xiàn)圖片瀏覽器的示例詳解
1、概述
案例:制作一個小的圖片瀏覽器,要求可以顯示jpg、jpeg、png、bmp??梢詮碾娔X上拖動圖到窗口并顯示出來?!?/p>
實現(xiàn)步驟:
1.創(chuàng)建一個QWidget
2.在QWidget的構(gòu)造方法中設(shè)置一個QLabel用戶顯示pixmap
3.在QWidget的protected中定義三個函數(shù),dragEnterEvent(QDragEnterEvent *event)、dropEvent(QDropEvent *event)、resizeEvent(QResizeEvent *event);并在.cpp中實現(xiàn)這三個方法
4.在QWidget的構(gòu)造函數(shù)中加入setAccessDrop(true),設(shè)置可向窗口拖拽內(nèi)容
5.實現(xiàn)dragEnterEvent(拖拽)、dropEvent(拖拽放下)、resizeEvent(窗口重置)
6.實現(xiàn)第5步的三個方法
7.在drawEnterEvent中過濾可拖拽的文件
void watershedwindow::dragEnterEvent(QDragEnterEvent *event){
QStringList acceptedFileTypes;
acceptedFileTypes.append("jpg");
acceptedFileTypes.append("jpeg");
acceptedFileTypes.append("bmp");
acceptedFileTypes.append("png");
if(event->mimeData()->hasUrls()&&event->mimeData()->urls().count()==1){
QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
if(acceptedFileTypes.contains(file.suffix().toLower())){
event->acceptProposedAction();//表明用戶可以在窗口部件上拖放對象
}
}
}
8.在dropEvent方法中實現(xiàn)文件拖拽放下后的情況,即顯示圖片
void watershedwindow::dropEvent(QDropEvent *event){
QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
if(pixmap.load(file.absoluteFilePath())){
label->setPixmap(pixmap.scaled(label->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}else{
QMessageBox::critical(this,tr("Error"),tr("The image file count not be read"));
}
}
9.resizeEvent方法中對圖像進行重置
void watershedwindow::resizeEvent(QResizeEvent *event){
Q_UNUSED(event);
if(!pixmap.isNull()){
label->setPixmap(pixmap.scaled(label->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
}
10.結(jié)束
ps:下面是完整代碼
2、代碼示例
#include "opencv2/opencv.hpp"
#include <QWidget>
#include <QSize>
#include <iostream>
#include <QPixmap>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <QFileInfo>
#include <QMessageBox>
#include <QResizeEvent>
#include <QStringList>
#include <QLabel>
using namespace cv;
using namespace std;
class watershedwindow : public QWidget
{
Q_OBJECT
private:
Mat src,gray,result,distanceImage;
QPixmap pixmap;
QLabel *label;
public:
explicit watershedwindow(QWidget *parent = nullptr);protected:
/**
* 拖進事件
* @brief dragEnterEvent
* @param event
*/
void dragEnterEvent(QDragEnterEvent *event);
/**
* 拖進放下事件
* @brief dropEvent
* @param event
*/
void dropEvent(QDropEvent *event) ;
void resizeEvent(QResizeEvent *event);
signals:
};
#include "watershedwindow.h"
watershedwindow::watershedwindow(QWidget *parent) : QWidget(parent)
{
this->setAcceptDrops(true);//設(shè)置允許向窗口拖入圖片
this->setFixedSize(QSize(320,480));
label = new QLabel(this);
label->setFixedSize(this->width(),this->height());
}
void watershedwindow::dragEnterEvent(QDragEnterEvent *event){
QStringList acceptedFileTypes;
acceptedFileTypes.append("jpg");
acceptedFileTypes.append("jpeg");
acceptedFileTypes.append("bmp");
acceptedFileTypes.append("png");
if(event->mimeData()->hasUrls()&&event->mimeData()->urls().count()==1){
QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
if(acceptedFileTypes.contains(file.suffix().toLower())){
event->acceptProposedAction();//表明用戶可以在窗口部件上拖放對象
}
}
}
void watershedwindow::dropEvent(QDropEvent *event){
QFileInfo file(event->mimeData()->urls().at(0).toLocalFile());
if(pixmap.load(file.absoluteFilePath())){
label->setPixmap(pixmap.scaled(label->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}else{
QMessageBox::critical(this,tr("Error"),tr("The image file count not be read"));
}
}
void watershedwindow::resizeEvent(QResizeEvent *event){
Q_UNUSED(event);
if(!pixmap.isNull()){
label->setPixmap(pixmap.scaled(label->size(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
}
}
}
3、演示圖片


到此這篇關(guān)于利用QT實現(xiàn)圖片瀏覽器的示例詳解的文章就介紹到這了,更多相關(guān)QT圖片瀏覽器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c語言實現(xiàn)把文件中數(shù)據(jù)讀取并存到數(shù)組中
下面小編就為大家?guī)硪黄猚語言實現(xiàn)把文件中數(shù)據(jù)讀取并存到數(shù)組中。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
解析VC中創(chuàng)建DLL,導(dǎo)出全局變量,函數(shù)和類的深入分析

