如何用QT实现支持多文件拖拽并获取文件路径的功能?

更新于
2026-10-10 13:46:51
2阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计676个文字,预计阅读时间需要3分钟。

如何用QT实现支持多文件拖拽并获取文件路径的功能?

本例展示了如何使用QT实现多文件拖拽获取路径的具体代码。功能包括:将多个文件拖拽进界面,显示文件的路径。实现步骤如下:

1. 启用窗口拖拽操作cppthis->setAcceptDrops(true);

2. 重写dragEnterEvent函数,允许拖拽操作cppvoid MainWindow::dragEnterEvent(QDragEnterEvent *event) { event->acceptProposedAction();}

3. 重写dropEvent函数,获取文件路径cppvoid MainWindow::dropEvent(QDropEvent *event) { const QList &urls=event->mimeData()->urls(); for (const QUrl &url : urls) { QString path=url.toLocalFile(); // 处理路径,例如显示在界面上 }}

本文实例为大家分享了QT实现多文件拖拽获取路径的具体代码,供大家参考,具体内容如下

功能

将多个文件拖拽进界面中,显示文件的路径。

实现

1、启用窗体放下操作

this->setAcceptDrops(true);//启用放下操作

2、重写dragEnterEvent()函数,用于筛选拖拽事件

void dragEnterEvent(QDragEnterEvent *e);

void MainWindow::dragEnterEvent(QDragEnterEvent *e) {     //对拖放事件进行筛选     if (true)     {         e->acceptProposedAction();    //放行,否则不会执行dropEvent()函数     } }

3、重写dropEvent()函数,用于处理拖拽事件

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、重复文件去除

QList存储文件路径,由于Qset是没有重复项的,故可先转换为set,再转回为list即可。

如何用QT实现支持多文件拖拽并获取文件路径的功能?

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();    //放行,否则不会执行dropEvent()函数     } }   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);     }     //去掉重复路径     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; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签:方法本文

本文共计676个文字,预计阅读时间需要3分钟。

如何用QT实现支持多文件拖拽并获取文件路径的功能?

本例展示了如何使用QT实现多文件拖拽获取路径的具体代码。功能包括:将多个文件拖拽进界面,显示文件的路径。实现步骤如下:

1. 启用窗口拖拽操作cppthis->setAcceptDrops(true);

2. 重写dragEnterEvent函数,允许拖拽操作cppvoid MainWindow::dragEnterEvent(QDragEnterEvent *event) { event->acceptProposedAction();}

3. 重写dropEvent函数,获取文件路径cppvoid MainWindow::dropEvent(QDropEvent *event) { const QList &urls=event->mimeData()->urls(); for (const QUrl &url : urls) { QString path=url.toLocalFile(); // 处理路径,例如显示在界面上 }}

本文实例为大家分享了QT实现多文件拖拽获取路径的具体代码,供大家参考,具体内容如下

功能

将多个文件拖拽进界面中,显示文件的路径。

实现

1、启用窗体放下操作

this->setAcceptDrops(true);//启用放下操作

2、重写dragEnterEvent()函数,用于筛选拖拽事件

void dragEnterEvent(QDragEnterEvent *e);

void MainWindow::dragEnterEvent(QDragEnterEvent *e) {     //对拖放事件进行筛选     if (true)     {         e->acceptProposedAction();    //放行,否则不会执行dropEvent()函数     } }

3、重写dropEvent()函数,用于处理拖拽事件

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、重复文件去除

QList存储文件路径,由于Qset是没有重复项的,故可先转换为set,再转回为list即可。

如何用QT实现支持多文件拖拽并获取文件路径的功能?

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();    //放行,否则不会执行dropEvent()函数     } }   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);     }     //去掉重复路径     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; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

标签:方法本文