当前位置:网站首页>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控件。
边栏推荐
- PCL calculates the intersection of three mutually nonparallel planes
- Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
- Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
- OpenShift 部署应用
- ICMP协议
- First week of JS study
- gocv边界填充
- 文件上传-upload-labs
- Sqli labs level 1
- Development of digital collection trading website development of metauniverse digital collection
猜你喜欢

STM32 new project (refer to punctual atom)

Web技术发展史

Web security -- Logical ultra vires

Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away

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

Kubesphere virtualization KSV installation experience

Luogu greedy part of the backpack line segment covers the queue to receive water

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

commands out of sync. did you run multiple statements at once

IP protocol and IP address
随机推荐
Minecraft空岛服开服
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Short video with goods source code, double-click to zoom in when watching the video
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
Use C language to receive JSON strings
Realize bidirectional linked list (with puppet node)
[flask] ORM one-to-one relationship
Solid principle: explanation and examples
[blackmail virus data recovery] suffix Hydra blackmail virus
HCIA - application layer
程序猿学英语-指令式编程
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
[untitled]
Data asset management function
程序猿学英语-Learning C
使用wireshark抓取Tcp三次握手
File upload Labs
D interface and domain problems
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads