当前位置:网站首页>QT学习:QDropEvent拖拽事件
QT学习:QDropEvent拖拽事件
2022-07-29 05:07:00 【上官宏竹】
使用QT实现一个如下的拖拽、拖放动作,只需要三步。主要是使用dropEvent和dragEnterEvent事件处理即可以。
步骤
- 1、
setAcceptDrops(true)
设置目的QWidget接收拖拽拖放事件 - 2、重写
dragEnterEvent
事件,并对正确的事件进行acceptProposedAction
接受操作 - 3、重写
dropEvent
事件,处理主要的逻辑即可
实现
在UI中画好布局,然后全部代码如下所示:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // 1. 整个MainWindow接收拖拽事件
}
// 2.重写拖拽进入事件
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction(); // 3. 有拖拽文件时设置接受
}
}
// 4.重写件拖拽放下事件,处理所需
void MainWindow::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasUrls()) {
QList<QUrl> urls = event->mimeData()->urls();
if(urls.isEmpty()) {
return;
}
ui->textEdit->setText(urls.first().toLocalFile());
}
}
在dragEnterEvent
中判断如果当前mime
类型为文本或者url
(本地文件是以url
类型描述的),则调用acceptProposedAction
来设置对应的事件发生flag——只有设置了这个flag,后面的drop事件才会发生。如果要捕获所有类型的拖放事件则可以直接调用acceptProposedAction
。
边栏推荐
- Mysql多对多关系,分组拼接把多个数据查询到一条数据上
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
- ODOO开发教程之图表
- Deep learning brush a bunch of tricks of SOTA
- Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
- P2181 diagonal
- 【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】
- Arfoundation starts from scratch 8-geospatial API (geospatial) development
- Self join and joint query of MySQL
- Cache penetration, cache breakdown, cache avalanche and Solutions
猜你喜欢
Northeast University Data Science Foundation (matlab) - Notes
The difference between the two ways of thread implementation - simple summary
office2010每次打开都要配置进度怎么解决?
Getting started with solidity
Solution | get the relevant information about the current employees' highest salary in each department |
电脑无法打开excel表格怎么办?excel打不开的解决方法
Force deduction ----- sort odd and even subscripts respectively
MySQL regularly calls preset functions to complete data update
SM integration is as simple as before, and the steps are clear (detailed)
Let you understand several common traffic exposure schemes in kubernetes cluster
随机推荐
Vivo market API event reporting and docking
office2010每次打开都要配置进度怎么解决?
Solution | get the relevant information about the current employees' highest salary in each department |
Northeast University Data Science Foundation (matlab) - Notes
浅谈AspectJ框架
This article takes you to understand the implementation of surround notification @around and final notification @after
MySQL sorts the queried result set according to the specified sequence
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
传奇如何一台服务器配置多个版本微端更新
How to add traffic statistics codes to the legendary Development Zone website
JS daily question (11)
MySQL many to many relationship, grouping and splicing to query multiple data to one data
时间序列分析的表示学习时代来了?
传奇开区网站如何添加流量统计代码
What if the office prompts that the system configuration cannot run?
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
Functions in MySQL statements
How does word view document modification traces? How word views document modification traces
一文带你搞懂环绕通知@Around与最终通知@After的实现
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!