当前位置:网站首页>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。
边栏推荐
- Cache penetration, cache breakdown, cache avalanche and Solutions
- Scikit learn -- steps and understanding of machine learning application development
- "Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
- Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
- Deadlock to be resolved
- 自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量
- Soft link & hard link
- ARFoundation从零开始3-创建ARFoundation项目
- 三层项目的架构分析及构造方法的参数名称注入
- About the configuration and use of thymeleaf
猜你喜欢

JS (foreach) return cannot end the function solution

【2022新生学习】第三周要点

What if excel is stuck and not saved? The solution of Excel not saved but stuck

Apache POI实现Excel导入读取数据和写入数据并导出

Architecture analysis of three-tier project and parameter name injection of construction method

This article takes you to understand the implementation of surround notification @around and final notification @after

Self join and joint query of MySQL

时间序列分析的表示学习时代来了?

Numpy Foundation

Pivot table of odoo development tutorial
随机推荐
What servers are needed to build mobile app
< El table column> place multiple pictures
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
ARFoundation入门教程10-平面检测和放置
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
Apache POI实现Excel导入读取数据和写入数据并导出
sql日志
Mysql语句中的函数
Numpy Foundation
Do you remember the process analysis and function implementation of post notification?
Let you understand several common traffic exposure schemes in kubernetes cluster
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
How does excel filter out the content you want? Excel table filtering content tutorial
ARFoundation从零开始3-创建ARFoundation项目
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
后置通知的流程分析与功能实现有哪些内容你还记得吗?
Activity workflow table structure learning
Deep learning brush a bunch of tricks of SOTA
Deadlock analysis using jstack, jconsole, and jvisualvm
Ros1 dead chicken data is stored in txt and SQLite