QT實(shí)現(xiàn)多文件拖拽獲取路徑的方法
本文實(shí)例為大家分享了QT實(shí)現(xiàn)多文件拖拽獲取路徑的具體代碼,供大家參考,具體內(nèi)容如下
功能
將多個(gè)文件拖拽進(jìn)界面中,顯示文件的路徑。
實(shí)現(xiàn)
1、啟用窗體放下操作
this->setAcceptDrops(true);//啟用放下操作
2、重寫dragEnterEvent()函數(shù),用于篩選拖拽事件
void dragEnterEvent(QDragEnterEvent *e);
void MainWindow::dragEnterEvent(QDragEnterEvent *e) { ? ? //對(duì)拖放事件進(jìn)行篩選 ? ? if (true) ? ? { ? ? ? ? e->acceptProposedAction();?? ?//放行,否則不會(huì)執(zhí)行dropEvent()函數(shù) ? ? } }
3、重寫dropEvent()函數(shù),用于處理拖拽事件
void dropEvent(QDropEvent *e);
void MainWindow::dropEvent(QDropEvent *e) { ? ? QList<QUrl> urls = e->mimeData()->urls(); ? ? if(urls.isEmpty()) ? ? ? ? return ; ? ? qDebug()<< urls.size(); ? ? foreach (QUrl u, urls) ? ? { ? ? ? ? QString filepath = u.toLocalFile(); ? ? ? ? pathlist.append(filepath); ? ? } }
4、重復(fù)文件去除
QList存儲(chǔ)文件路徑,由于Qset是沒有重復(fù)項(xiàng)的,故可先轉(zhuǎn)換為set,再轉(zhuǎn)回為list即可。
pathlist = pathlist.toSet().toList();
效果
UI布局
最后效果
源碼
頭文件
#ifndef MAINWINDOW_H #define MAINWINDOW_H ? #include <QMainWindow> #include <QTextEdit> ? namespace Ui { class MainWindow; } ? class MainWindow : public QMainWindow { ? ? Q_OBJECT ? public: ? ? explicit MainWindow(QWidget *parent = 0); ? ? ~MainWindow(); ? protected: ? ? void dragEnterEvent(QDragEnterEvent *e); ? ? void dropEvent(QDropEvent *e); ? private slots: ? ? void on_pushButton_upload_clicked(); ? private: ? ? Ui::MainWindow *ui; ? ? QList<QString> pathlist; }; ? #endif // MAINWINDOW_H
源文件
#include "mainwindow.h" #include "ui_mainwindow.h" ? #include <QDragEnterEvent> #include <QFile> #include <QDebug> #include <QMimeData> MainWindow::MainWindow(QWidget *parent) : ? ? QMainWindow(parent), ? ? ui(new Ui::MainWindow) { ? ? ui->setupUi(this); ? ? ? ui->textEdit->setAcceptDrops(false); //禁用控件的放下操作 ? ? this->setAcceptDrops(true);//啟用放下操作 } ? MainWindow::~MainWindow() { ? ? delete ui; } ? void MainWindow::dragEnterEvent(QDragEnterEvent *e) { ? ? //對(duì)拖放事件進(jìn)行篩選 ? ? if (true) ? ? { ? ? ? ? e->acceptProposedAction();?? ?//放行,否則不會(huì)執(zhí)行dropEvent()函數(shù) ? ? } } ? void MainWindow::dropEvent(QDropEvent *e) { ? ? QList<QUrl> urls = e->mimeData()->urls(); ? ? if(urls.isEmpty()) ? ? ? ? return ; ? ? qDebug()<< urls.size(); ? ? foreach (QUrl u, urls) ? ? { ? ? ? ? QString filepath = u.toLocalFile(); ? ? ? ? pathlist.append(filepath); ? ? } ? ? //去掉重復(fù)路徑 ? ? pathlist = pathlist.toSet().toList(); ? ? ui->textEdit->clear(); ? ? for(int i=0;i<pathlist.size();++i) ? ? { ? ? ? ? QString path = pathlist.at(i); ? ? ? ? ui->textEdit->append(path); ? ? } } ? ? void MainWindow::on_pushButton_upload_clicked() { ? ? qDebug()<<pathlist; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c++矩陣計(jì)算性能對(duì)比:Eigen和GPU解讀
這篇文章主要介紹了c++矩陣計(jì)算性能對(duì)比:Eigen和GPU解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12C++11?nullptr實(shí)現(xiàn)初始化空指針
避免產(chǎn)生“野指針”最有效的方法,就是在定義指針的同時(shí)完成初始化操作,本文主要介紹了C++11?nullptr初始化空指針,感興趣的可以了解一下2022-01-01DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--浮點(diǎn)與定點(diǎn)概述
本文主要介紹DSP中浮點(diǎn)與定點(diǎn)概述,很值得學(xué)習(xí)一下,需要的朋友可以參考一下。2016-06-06C++類型轉(zhuǎn)換運(yùn)算符的實(shí)例詳解
這篇文章主要介紹了C++類型轉(zhuǎn)換運(yùn)算符的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09C++使用一個(gè)棧實(shí)現(xiàn)另一個(gè)棧的排序算法示例
這篇文章主要介紹了C++使用一個(gè)棧實(shí)現(xiàn)另一個(gè)棧的排序算法,結(jié)合實(shí)例形式分析了C++借助輔助棧實(shí)現(xiàn)棧排序算法的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作未指定的錯(cuò)誤)
這篇文章主要介紹了VS2019添加引用出錯(cuò):對(duì)COM組件的調(diào)用返回了錯(cuò)誤HRESULT E_FAIL(未能完成操作。未指定的錯(cuò)誤),需要的朋友可以參考下2020-07-07