当前位置:网站首页>Qt的拖动事件
Qt的拖动事件
2022-07-02 06:30:00 【代码海贼团船长】
Qt的文件拖动事件
1、怎样让qt界面识别到拖动的文件?
在界面构造函数中添加如下代码
setAcceptDrops(true);//设置支持拖拽
这样在拖动文件到Qt程序的界面时,就会触发Qt的拖动事件;
2、如何处理拖动文件操作
QWidget类和继承QWidget的子类,都可以通过定义的虚函数接口重写拖动事件。这四个有关的虚函数是
void dragEnterEvent(QDragEnterEvent *event); //拖拽进入Qt桌面控件时触发
void dragMoveEvent(QDragMoveEvent *event); //拖拽在Qt桌面控件内移动时触发
void dragLeaveEvent(QDragLeaveEvent *event); //拖拽离开Qt桌面控件时触发
void dropEvent(QDropEvent *event); //放下文件在Qt桌面控件时触发
void Widget::dragEnterEvent(QDragEnterEvent *event)
{
qDebug()<<event->type();
if (event->mimeData()->hasFormat("text/uri-list")) //判断是否是文件
{
event->acceptProposedAction();
}
}
void Widget::dragMoveEvent(QDragMoveEvent *event)
{
qDebug()<<event->type();
}
void Widget::dragLeaveEvent(QDragLeaveEvent *event)
{
qDebug()<<event->type();
}
void Widget::dropEvent(QDropEvent *event)
{
qDebug()<<event->type();
QList<QUrl> urls = event->mimeData()->urls();
qDebug()<<urls;
setText(urls[0].toString());
}
从代码可以看出,拖拽文件到Qt控件中时,不仅支持单个文件,也支持多个文件的拖拽,通过上面的代码,将拖拽文件的路径传给Qt控件。
边栏推荐
- When a custom exception encounters reflection
- Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
- IP协议与IP地址
- Linux安装Oracle Database 19c
- CarSim learning experience - rough translation 1
- web安全--逻辑越权
- c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
- Linux二进制安装Oracle Database 19c
- 整理秒杀系统的面试必备!!!
- C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
猜你喜欢
sqli-labs第8关(布尔盲注)
How to build the alliance chain? How much is the development of the alliance chain
Chrome debugging
KubeSphere 虚拟化 KSV 安装体验
Realization of basic function of sequence table
Kubedm deploys kubernetes v1.23.5 cluster
Gateway 简单使用
Kubesphere virtualization KSV installation experience
HCIA - data link layer
DWORD ptr[]
随机推荐
Routing foundation - dynamic routing
文件上传-upload-labs
CarSim learning experience - rough translation 1
Chrome debugging
IP协议与IP地址
Rotating linked list (illustration)
Short video with goods source code, double-click to zoom in when watching the video
Call Stack
HCIA - data link layer
gocv图片读取并展示
Nacos 下载启动、配置 MySQL 数据库
Linux安装Oracle Database 19c RAC
Gateway 简单使用
How to apply for a secondary domain name?
TCP/IP—传输层
Sentinel easy to use
Linux安装Oracle Database 19c
Kubesphere virtualization KSV installation experience
2022 Heilongjiang latest food safety administrator simulation exam questions and answers
Shortcut key to comment code and cancel code in idea