当前位置:网站首页>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控件。
边栏推荐
猜你喜欢

Minecraft group service opening

Getting started with k8s: building MySQL with Helm

C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)

Analysis of the use of comparable, comparator and clonable interfaces

Nacos 下载启动、配置 MySQL 数据库

STM32-新建工程(参考正点原子)
![[blackmail virus data recovery] suffix Rook3 blackmail virus](/img/46/debc848d17767d021f3f41924cccfe.jpg)
[blackmail virus data recovery] suffix Rook3 blackmail virus

Minecraft群组服开服

顺序表基本功能函数的实现

kubernetes部署loki日志系统
随机推荐
Minecraft群組服開服
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
程序猿学英语-指令式编程
Honeypot attack and defense drill landing application scheme
Matlab - autres
类和对象(类和类的实例化,this,static关键字,封装)
When a custom exception encounters reflection
Gateway 简单使用
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Analysis of the use of comparable, comparator and clonable interfaces
Getting started with k8s: building MySQL with Helm
Web安全--核心防御机制
ARP及ARP欺骗
Use Wireshark to grab TCP three handshakes
kubeadm部署kubernetes v1.23.5集群
[flask] ORM one-to-one relationship
判断是否是数独
ARP and ARP Spoofing
Flex layout
Matlab-其它