QT 实现鼠标拖动(drag)和放下(drop)代码(图形操作四)

article/2025/6/27 7:32:10

 拖动的对象:

#include "outlistitem.h"
#include "ui_outlistitem.h"OutListItem::OutListItem(QWidget *parent) :QWidget(parent),ui(new Ui::OutListItem)
{ui->setupUi(this);
}OutListItem::~OutListItem()
{delete ui;
}void OutListItem::mousePressEvent(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){QDrag *drag = new QDrag(this);QMimeData *mimeData = new QMimeData;drag->setMimeData(mimeData);Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);this->setStyleSheet("background:#000");}
}void OutListItem::mouseMoveEvent(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){qDebug()<<"mouseMoveEvent"<<endl;}
}void OutListItem::mouseReleaseEvent(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){qDebug()<<"mouseReleaseEvent"<<endl;}
}void OutListItem::setLabelTxt(QString str)
{this->ui->label->setText(str);
}
#ifndef OUTLISTITEM_H
#define OUTLISTITEM_H
#include <QDebug>
#include <QWidget>
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>namespace Ui {
class OutListItem;
}class OutListItem : public QWidget
{Q_OBJECTpublic:explicit OutListItem(QWidget *parent = nullptr);~OutListItem();QString name;void setLabelTxt(QString str);void setName(QString str){name = str;}QString getName(){return name;}protected:void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseDoubleClickEvent(QMouseEvent *event);private:Ui::OutListItem *ui;
};#endif // OUTLISTITEM_H

被拖动的对象放下的位置 对象:

#include "screen.h"
#include <QDebug>
#include <QStyleOption>
#include <QPainter>
#include "screenctr.h"Screen::Screen(QWidget *parent) : QWidget(parent)
{this->setCursor(QCursor(Qt::SizeAllCursor));setAcceptDrops(true);
}void Screen::mousePressEvent(QMouseEvent *event){if(event->button()==Qt::LeftButton){}
}void Screen::mouseReleaseEvent(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){}
}void Screen::mouseMoveEvent(QMouseEvent *event)
{}void Screen::paintEvent(QPaintEvent *event)
{//自定义的类需要paintEvent来重新绘制背景Q_UNUSED(event);QStyleOption opt;opt.init(this);QPainter p(this);style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}void Screen::dragEnterEvent(QDragEnterEvent *event)
{qDebug() << "Screen::dragEnterEvent 111 ";event->setDropAction(Qt::MoveAction);event->accept();
}void Screen::dragMoveEvent(QDragMoveEvent *event)
{qDebug() << "Screen::dragMoveEvent 222 ";}void Screen::dropEvent(QDropEvent *event)
{qDebug() << "Screen::dropEvent 333 ";OutListItem* source = qobject_cast<OutListItem*>(event->source());if(source != NULL){qDebug() << "source name ===- " << source->getName();this->scrName = source->getName();QLabel* label = this->findChild<QLabel*>();qDebug() << "label ===- " << label;label->setText(source->getName());}}void Screen::dragLeaveEvent(QDragLeaveEvent *event)
{qDebug() << "Screen::dragLeaveEvent 444 ";
}
#ifndef SCREEN_H
#define SCREEN_H#include <QWidget>
#include "paintergragh.h"
#include <QLineEdit>
#include <QMouseEvent>
#include <QPoint>
#include <QCursor>
#include <QRect>
#include <QtWidgets/QLabel>using namespace neb;
class Screen : public QWidget
{Q_OBJECT
public:explicit Screen(QWidget *parent = nullptr);void paintEvent(QPaintEvent *event);
public:QString scrName;
protected:void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void dragEnterEvent(QDragEnterEvent *e);virtual void dragMoveEvent(QDragMoveEvent *e);//允许子类重写覆盖virtual void dropEvent(QDropEvent *e);virtual void dragLeaveEvent(QDragLeaveEvent *e);
};#endif // SCREEN_H

 鼠标按住把左侧拖动到右侧:

 放下后把名字改掉:


http://chatgpt.dhexx.cn/article/N1NNGBOq.shtml

相关文章

放下鼠标 全键盘定义你的浏览器

在linux上用过vim的朋友相信都被这个编辑器惊艳到了吧, 但是它只是在linux系统中存在而且是在字符操作界面。但是如果将这种可以全键盘的操作搬到windows上这样的图形界面又是怎样令人去感到惊艳呢&#xff1f; 本文介绍的插件是基于谷歌和火狐的&#xff0c;它可以使你在很大的…

外企工作日常:全英文CrossTalk反串讲-讲前紧张,讲后淡定

起因&#xff1a; 在三周之前我接到了一个任务&#xff0c;更准确讲是我自己“作死”主动领取了一个更复杂的任务。这个任务包含了三个相关联的需求。这个需求说简单也简单&#xff0c;说复杂也复杂&#xff5e; 简单在&#xff1a;就是更改一下更新token时间的一个逻辑&am…

linux周六串讲

esc. //粘贴复制上一条命令的参数 cat /etc/resolv.conf //查看DNS地址 route -n //查看网关 hostname //临时修改主机名 hostnamectl set-hostname 名称 //永久修改主机名 ssh root192.168.10.233 //用windows远程的格式&#xff0c;在CMD窗口输入这个命令 …

【Linux】Linux指令串讲

大家好&#xff0c;今天要开启一个新的专题&#xff1a;Linux 今天的内容是指令还有一些基本的Linux知识补充 由于Linux的知识很难明确写出分类&#xff0c;所以目录就不会做的特别详细完全 喜欢的小伙伴点赞收藏一下不迷路哦 目录 1.目录 2.文件 3.路径 1.目录 1.创建目录…

kafka 串讲:架构模型、角色功能梳理

kafka 串讲&#xff1a;架构模型、角色功能梳理 kafka 的 what why how&#xff0c;先有一个粗略宏观的理解 rabbitmq、各种 mq 的技术选型、横向对比 首先&#xff0c;kafka 是一个消息中间件。我们从一个本质的点聊起&#xff0c;我们有一个系统 service&#xff0c;如果这…

【视频理解】论文串讲

视频理解论文串讲 2020-视频理解综述-A Comprehensive Study of Deep Video Action Recognition2014-CVPR-Large-scale Video Classification with Convolutional Neural Networks2014-NIPS-Two-Stream Convolutional Networks for Action Recognition in Videos2015-CVPR-Beyo…

李沐论文精度系列之八:视频理解论文串讲

文章目录 一 、前言二、 DeepVideo&#xff08;IEEE 2014&#xff09;2.1 模型结构2.2 实验结果2.3 总结 三、双流网络及其变体3.1 Two-Stream&#xff08;NeurIPS 2014&#xff09;3.1.1 简介3.1.2 改进工作 3.2 Two stream LSTM&#xff08;CVPR 2015 &#xff09;3.2.1 模型…

【Java】JVM 知识串讲

1. JVM 是什么 JVM 是 Java 虚拟机的简称&#xff0c;我们在下载 Java 运行环境时&#xff08;jre&#xff09;&#xff0c;就已经包含了 JVM 了&#xff0c;JVM 是 Java实现跨平台的最核心的部分&#xff0c;所有的 Java 程序会首先被编译为 .class 的类文件&#xff0c;这种…

视频理解论文串讲——学习笔记

文章目录 DeepVideoTwo-StreamBeyond-short-SmippetsConvolutional FusionTSN C3DI3DNon-localR&#xff08;21&#xff09;DSlowFast Timesformer 本文是对视频理解领域论文串讲的笔记记录。 一篇相关综述&#xff1a;Yi Zhu, Xinyu Li, Chunhui Liu, Mohammadreza Zolfaghari…

CGB2202串讲-java复习

java基础&#xff1a; day01&#xff1a; 1&#xff1a;java的开发环境&#xff1a; 编译器&#xff1a;.java源文件&#xff0c;编译成.class的字节码文件 运行期&#xff1a;jvm加载并运行.class字节码文件 2&#xff1a;名词解释&#xff1a; jvm&#xff1a; 加载并运行.cl…

我的论文串讲「一」

文章目录 [Done]2022-arXiv-It’s DONE Direct ONE-shot learning without training optimizationAbstractIntroductionRelated workMethodologyImplementation and DatasetResults and Discussion [Phase]2021-SPIE-Deep neural networks to improve the dynamic range of Zer…

多模态串讲(上)

多模态的学习在最近几年异常火爆&#xff0c;除了普通的多模态学习&#xff0c;比如视觉问答&#xff0c;图文检索等,其实之前讲的所有这种Language Guided Detection&#xff0c;或者Language Guided Segmentation&#xff0c;这些任务都是多模态的&#xff0c;还有最近火的文…

C++【引用】——串讲

【引用】——串讲&#xff08;视频89-94&#xff09; Note: i.视频为黑马程序员C视频&#xff0c;系列文章为视频听课笔记; ii.引用不仅包含定义及简单应用&#xff0c;在类与对象…中也有涉及&#xff1b; iii.难度指数&#xff1a; iv.不论变量、函数名、标识符形式怎样复杂…

CLIP 改进工作串讲(下)

CLIP 改进工作串讲&#xff08;下&#xff09; 本文为 CLIP 改进工作串讲&#xff08;下&#xff09;【论文精读】 的学习笔记。 图像生成 最近一年图像生成领域扩散模型大火&#xff0c;尤其是文本生成图像&#xff0c;DALL-E、imagen 等工作层出不穷&#xff0c;有机会专门…

操作系统串讲

前言 文章内容来源&#xff1a;东北大学的操作系统MOOC视频在操作系统的不同阶段&#xff0c;计算机的工作形式也不同&#xff0c;不要在一开始就将操作系统在心里预设成现代的 windows 或 linux 操作系统&#xff0c;以及将计算机预设成个人电脑等&#xff0c;防止代错对象、…

4.7串讲

4.7串讲 Java基础API 文章目录 4.7串讲 Java基础API字符串字符字符串StringBulider&#xff0c;StringBuffer 内存结构正则表达式字符字符类预定义字符类POSIX字符类&#xff08;仅US-ASCII&#xff09;java.lang.Character 类&#xff08;简单的 java 字符类型&#xff09;Uni…

计算机网络知识串讲复习(超全)

本篇我总结了学习计算机网络时必备的知识&#xff0c;可以用于巩固知识或者期末复习~~~如果觉得博主爆肝总结的不错&#xff0c;感谢点赞! 目录 1、计算机网络的几个发展阶段 2、计算机网络的组成 3、协议的概念 4、实体,协议,服务,和服务访问点 5、计算机网络的分类 6、…

CLIP改进工作串讲(上)

CLIP改进工作串讲&#xff08;上&#xff09; 本文为 CLIP 改进工作串讲&#xff08;上&#xff09;【论文精读】 的学习笔记。 CLIP 改进方向 语义分割 Lseg、GroupViT 目标检测 ViLD、GLIP v1/v2 视频理解 VideoCLIP、CLIP4clip、ActionCLIP 图像生成 VQGAN-CLIP、CLIPass…

毕业知识串讲

1. 京淘后端项目架构 1.1 数据库主从同步 1.1.1 业务需求说明 当数据库发生宕机的现象,如果配置了数据库主从同步,则可以从从库中获取现有记录,显示数据的恢复. 1.2 数据库主从同步原理 1.2.1 数据库冷备份 说明: 人工的将数据库文件定期导出,一式三份 如果其中一份丢失…

如何做好技术串讲

首先&#xff0c;如何做好演讲是个好话题&#xff0c;不过这过于大众的主题不想写&#xff0c;除非哪天能想到点 新意 再说&#xff1b; 其次&#xff0c;串讲和演讲有啥区别呢&#xff1f;其实也没啥区别&#xff0c;一会儿说说我自己的理解&#xff1b; 最后&#xff0c;什么…