網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了QT實現(xiàn)多文件拖拽獲取路徑的具體代碼,供大家參考,具體內(nèi)容如下
功能
將多個文件拖拽進界面中,顯示文件的路徑。
實現(xiàn)
1、啟用窗體放下操作
this->setAcceptDrops(true);//啟用放下操作
2、重寫dragEnterEvent()函數(shù),用于篩選拖拽事件
void dragEnterEvent(QDragEnterEvent *e);
void MainWindow::dragEnterEvent(QDragEnterEvent *e) { ? ? //對拖放事件進行篩選 ? ? if (true) ? ? { ? ? ? ? e->acceptProposedAction();?? ?//放行,否則不會執(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存儲文件路徑,由于Qset是沒有重復(fù)項的,故可先轉(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) { ? ? //對拖放事件進行篩選 ? ? if (true) ? ? { ? ? ? ? e->acceptProposedAction();?? ?//放行,否則不會執(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; }
原文鏈接:https://blog.csdn.net/qq_40602000/article/details/104719164
相關(guān)推薦
- 2022-08-25 Python中如何使用Matplotlib庫繪制圖形_python
- 2022-03-19 C語言完全平方整數(shù)的判斷_C 語言
- 2022-10-23 Python中unittest的數(shù)據(jù)驅(qū)動詳解_python
- 2022-03-19 .NET中的Husky工具及安裝方式_實用技巧
- 2022-09-04 Python可視化tkinter詳解_python
- 2024-03-17 WSL子系統(tǒng)啟動報錯 Wsl/Service/CreateInstance/CreateVm/HCS
- 2022-07-07 在Kubernetes集群中搭建Istio微服務(wù)網(wǎng)格的過程詳解_云其它
- 2022-12-07 C++?類this及返回自身對象的引用方式_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支