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

Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)

Valin cable: BI application promotes enterprise digital transformation

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

Generate database documents with one click, which can be called swagger in the database industry

STM32-新建工程(参考正点原子)

Web security -- core defense mechanism

2022 Heilongjiang latest construction eight members (materialman) simulated examination questions and answers

Function ‘ngram‘ is not defined

Minecraft插件服开服

Minecraft空岛服开服
随机推荐
Web技术发展史
File upload and download performance test based on the locust framework
Sentinel easy to use
Kubesphere virtualization KSV installation experience
16: 00 interview, came out at 16:08, the question is really too
Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
Application of kotlin - higher order function
Minecraft空岛服开服
Openshift build image
Dip1000 implicitly tagged with fields
文件上传-upload-labs
Pointer initialization
D interface and domain problems
cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
Gateway 简单使用
实现双向链表(带傀儡节点)
Use Wireshark to grab TCP three handshakes
TCP/IP—传输层
方法递归(斐波那契数列,青蛙跳台阶,汉诺塔问题)
Programming ape learning English - imperative programming