当前位置:网站首页>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控件。
边栏推荐
- kubeadm部署kubernetes v1.23.5集群
- PCL calculates the intersection of three mutually nonparallel planes
- Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
- Intelligent manufacturing solutions digital twin smart factory
- Minecraft模组服开服
- Causes of laptop jam
- OpenShift 部署应用
- 旋转链表(图解说明)
- Sqli labs level 1
- In depth understanding of prototype drawings
猜你喜欢
OpenFeign 简单使用
Generate database documents with one click, which can be called swagger in the database industry
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
TCP/IP—传输层
Honeypot attack and defense drill landing application scheme
Zipkin is easy to use
Application of kotlin - higher order function
Minecraft群組服開服
旋转链表(图解说明)
Sentinel 简单使用
随机推荐
OpenFeign 简单使用
Minecraft模组服开服
web安全--逻辑越权
Analysis of the use of comparable, comparator and clonable interfaces
Programming ape learning English - imperative programming
gocv拆分颜色通道
c语言将字符串中的空格替换成%20
Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
Viewing JS array through V8
Rotating linked list (illustration)
HCIA—数据链路层
File upload and download performance test based on the locust framework
Global and Chinese markets of tilting feeders 2022-2028: Research Report on technology, participants, trends, market size and share
Sqli labs Level 2
Qunhui NAS configuring iSCSI storage
Flex layout
HCIA—应用层
Web技术发展史
Solid principle: explanation and examples
Hcia - Application Layer