QT 实现鼠标拖动(drag)和放下(drop)代码(图形操作四)
拖动的对象:
#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
鼠标按住把左侧拖动到右侧:
放下后把名字改掉:
QT 实现鼠标拖动(drag)和放下(drop)代码(图形操作四)相关推荐
- 【QT 5 设置自定义标题栏+学习:《QT实现鼠标拖动调整窗口大小》+基础样例】
[QT 5 设置自定义标题栏+学习:<QT实现鼠标拖动调整窗口大小>+基础样例] 1.说明 2.实验环境 3.实验目的 4.参考文章 5.实验步骤 (1)下载代码,运行没有错误. (2)加 ...
- Qt窗体鼠标拖动放大缩小
不费话直接上效果图 功能已经实现窗体缩小放大,拖动. 源码我就不贴了 项目源码地址:https://download.csdn.net/download/sicuan5540/11002181
- unity3d用鼠标拖动物体的一段代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 这是一段拖动物体的代码,比较简洁明了,对了解uni ...
- html5 jquery 鼠标拖动例子,jquery实现鼠标拖动实现DIV排序示例代码
先上效果图: 对比传统的排序,这是一个很不错的尝试,希望对大家有启发. 大家可以参考我的上一篇博文://www.jb51.net/article/42087.htm html部分: 指标: 注:可以通 ...
- android div拖动排序,鼠标拖动实现DIV排序示例代码
先上效果图: 对比传统的排序,这是一个很不错的尝试,希望对大家有启发. 大家可以参考我的上一篇博文:https://www.jb51.net/article/42087.htm html部分: 指标: ...
- Qt 自定义标题栏,最小化、最大化、关闭窗口,双击最大化,鼠标拖动等效果实现
文章目录 前言 效果 代码 .pro文件 widget.h widget.cpp widget.ui title.h title.cpp title.ui 前言 本次实验内容为Qt自定义标题栏,最小化 ...
- H5鼠标拖动事件(drag)
H5鼠标拖动事件 一.元素拖动 二.相关事件 1.拖拽元素 (1)dargstart (2)drag (3)dragend 2.目标元素 (1)drop (2)dragover (3)dragente ...
- Qt鼠标拖动绘制基本几何图形
概述 用Qt鼠标事件实现基本几何图形的绘制,支持直线.矩形.圆形.椭圆.后期可以在此基础上进行扩展. 效果图 实现 本示例使用QGraphics体系来实现,因为要移动对象,所以生成的图形必须是一个单独 ...
- jquery 鼠标拖动排序Li或Table
1.前端页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="拖动排序Li或Ta ...
最新文章
- Mysql练习题14-至少有5名直接下属的经理
- Linux中Apache2fail,apache2 启动失败,出现下列错误,请问怎么解决,谢谢
- Xcode8.0 去除控制台多余打印
- goldengate mysql_使用GoldenGate实现MySQL到Oracle的数据实时同步
- 北美KubeCon新风,正把K8S魔力带向边缘计算
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
- 我是怎么找电子书的?
- Fisker大师用ZBrush制作兽人萨尔全过程
- 用flash做古诗动画_《古诗三首》Flash动画课件
- 华为云CDN+芒果TV,是如何做出“云”榜样?
- 玩转数据可视化之R语言ggplot2:(九)网络图绘制
- MySQL DBLE_MySQL 中间件 - DBLE 简单使用
- 小熊的十万个为什么?
- frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
- ols残差_多元回归方程的OLS残差
- tipask 不能正常解析
- SQLite封锁机制
- 群晖NAS教程(二十一)、利用Docker安装DDNS动态域名解析,自动更新域名解析到公网IP(支持阿里云、腾讯云dnspod、Cloudflare、华为云)
- 用51单片机实现抽水水泵
- 作业1.老狼几点了。凌晨,上午,下午,晚上。