当前位置:网站首页>QT learning: qdropevent drag event
QT learning: qdropevent drag event
2022-07-29 05:23:00 【Shangguan Hongzhu】
QT Study :QDropEvent Drag events
Use QT Implement the following drag and drop 、 Drag and drop action , Just three steps . Mainly used dropEvent and dragEnterEvent Event processing can .
step
- 1、
setAcceptDrops(true)
Set purpose QWidget Receive drag and drop events - 2、 rewrite
dragEnterEvent
event , And correct eventsacceptProposedAction
Accept the operation - 3、 rewrite
dropEvent
event , Just deal with the main logic
Realization
stay UI Draw a good layout , Then the whole code is as follows :
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true); // 1. Whole MainWindow Receive drag events
}
// 2. Rewrite the drag in event
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction(); // 3. Set accept when dragging files
}
}
// 4. Rewrite the drag drop event , Process required
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());
}
}
stay dragEnterEvent
If the current mime
The type is text or url
( The local file is in url
Type described ), Call acceptProposedAction
To set the corresponding event occurrence flag—— Only set this flag, hinder drop That's what happened . If you want to capture all types of drag and drop events, you can directly call acceptProposedAction
.
边栏推荐
猜你喜欢
【文件下载】Easyexcel快速上手
预约中,2022京东云产业融合新品发布会线上开启
直播预告|如何节省30%人工成本,缩短80%商标办理周期?
ARFoundation入门教程10-平面检测和放置
On AspectJ framework
AI应用第一课:C语言支付宝刷脸登录
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
How to add traffic statistics codes to the legendary Development Zone website
Scikit learn -- steps and understanding of machine learning application development
How mongodb inserts, deletes and updates documents
随机推荐
Scikit learn -- steps and understanding of machine learning application development
传奇服务端如何添加地图
Arfoundation starts from scratch 8-geospatial API (geospatial) development
什么是_GLIBCXX_VISIBILITY(default)
01-01-osg GL3 环境搭建
C how to realize simple factory mode
[file download] easyexcel quick start
VirtualBox has expanded the capacity of virtual hard disk (without modifying the original data)
C语言函数实现输出I love you
MySQL many to many relationship, grouping and splicing to query multiple data to one data
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
WDDM学习
Deadlock analysis using jstack, jconsole, and jvisualvm
来!看排名一年上升16位的ClickHouse,如何在京东落地实践
vs2019编译cryengine失败问题处理
321,京东言犀×NLPCC 2022挑战赛开赛!
ARFoundation从零开始9-AR锚点(AR Anchor)
QT series - Installation
Qml类型:State 状态
CryEngine5 Shader调试