图形视图框架 事件处理(item)

news/2023/12/4 7:01:05

在图形界面框架中的事件都是先由视图进行接收,然后传递给场景,再由场景传递给图形项。通过键盘处理的话,需要设置焦点,在QGraphicsScene中使用setFoucesItem()函数可以设置焦点,或者图形项使用setFouce()获取焦点。

默认的如果场景中没有获取焦点,那么所有的键盘事件将会被丢弃。如果场景中的setFouce()函数或图形项获取了焦点,那么场景也会自动获取焦点。

对于鼠标悬停效果,QGraphicsScene会调度悬停事件。如果一个图形项可以接收悬停事件,那么当鼠标进入它的区域之中时,它就会收到一个GraphicsSceneHoverEnter事件。如果鼠标继续在图形项的区域之中进行移动,那么QGraphicsScene就会向该图形项发送GraphicsSceneHoverMove事件。当鼠标离开图形项的区域时,它将会收到一个GraphicsSceneHoverLeave事件。图形项默认是无法接收悬停事件的,可 以使用QGraphicsItem类的setAcceptHoverEvents()函数使图形项可以接收悬停事件。

所有的鼠标事件都会传递到当前鼠标抓取的图形项,一个图形项如果可以接收鼠标事件(默认可以)而且鼠标在它的上面被按下,那么它就会成为场景的鼠标抓取的图形项

事件主要分为:

  • 鼠标事件
  • 悬停事件
  • 键盘事件
  • 拖拽事件
  • 上下文菜单事件

由于内容比较多,这里就单个单个介绍。

鼠标事件: 

mouseDoubleClickEvent()鼠标双击事件
mouseMoveEvent()鼠标移动事件
mousePressEvent()鼠标点击事件
MouseReleaseEvent()鼠标松开事件

例子:

一个矩形项,鼠标单机的话为红色,双击的话为蓝色,移动的话为绿色,松开的话为黄色,

默认为黑色

MyItem.h文件 

#ifndef MYITEM_H
#define MYITEM_H
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QMouseEvent>
class MyItem:public QGraphicsItem
{
public:MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );void mouseMoveEvent(QGraphicsSceneMouseEvent *event) ;//鼠标移动事件void mousePressEvent(QGraphicsSceneMouseEvent *event) ;//鼠标点击事件void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) ;//鼠标松开事件void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) ;//鼠标双击事件
private:QColor color;//颜色
};#endif // MYITEM_H

MyItem.cpp文件 

每次执行完之后要使用updata()更新一下数据,不然会卡顿。版本为(Qt5.9.9)

#include "myitem.h"MyItem::MyItem()
{color=QColor(Qt::black);//默认为黑色}
QRectF MyItem::boundingRect() const
{qreal penwidget=1;return QRectF(-penwidget/2,-penwidget/2,100+penwidget,100+penwidget);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{painter->setBrush(color);painter->drawRect(0,0,100,100);//画矩形
}
void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) //鼠标移动事件
{color=QColor(Qt::green);update();
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event) //鼠标点击事件
{setFocus();//设置焦点color=QColor(Qt::red);update();
}
void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) //鼠标松开事件
{color=QColor(Qt::yellow);update();
}
void MyItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) //鼠标双击事件
{color=QColor(Qt::blue);update();
}

main文件:

#include "widget.h"
#include "myitem.h"
#include <QApplication>
#include<QGraphicsScene>
#include<QGraphicsView>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QGraphicsScene scene(-200,-200,400,400); //场景MyItem item; //项scene.addItem(&item);QGraphicsView view; //视图view.setScene(&scene);view.show();return a.exec();
}

运行结果:

         默认:                  单击:                     松开:                   双击:            鼠标移动:

 

 停靠事件:

hoverEnterEvent()悬停输入事件
hoverLeaveEvent()悬停离开事件
hoverMoveEvent()悬停移动事件

 默认情况下,不会接收悬停事件,需要使用setAcceptHoverEvents()开启接收悬停事件。

 例子:

默认为黑色,悬停离开为蓝色,悬停移动为绿色.

 MyItem.h文件 

#ifndef MYITEM_H
#define MYITEM_H
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QHoverEvent>
class MyItem:public QGraphicsItem
{
public:MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );void hoverMoveEvent(QGraphicsSceneHoverEvent *event) ;//悬停移动void hoverEnterEvent(QGraphicsSceneHoverEvent *event) ;//悬停进入void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) ;//悬停离开
private:QColor color;//颜色
};#endif // MYITEM_H

MyItem.cpp

#include "myitem.h"MyItem::MyItem()
{color=QColor(Qt::black);//默认为黑色setAcceptHoverEvents(true);//开启接收悬停}
QRectF MyItem::boundingRect() const
{qreal penwidget=1;return QRectF(-penwidget/2,-penwidget/2,100+penwidget,100+penwidget);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{painter->setBrush(color);painter->drawRect(0,0,100,100);//画矩形
}
void MyItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) //悬停移动
{color=QColor(Qt::green);//绿色update();
}
void MyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) //悬停输入
{
}
void MyItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) //悬停离开
{color=QColor(Qt::blue);//蓝色update();
}
int main(int argc, char *argv[])
{QApplication a(argc, argv);QGraphicsScene scene(-200,-200,400,400);MyItem item;scene.addItem(&item);QGraphicsView view;view.setScene(&scene);view.show();return a.exec();
}

键盘事件:

keyPressEvent键盘点击
keyReleaseEvent键盘松开

使用键盘事件需要注意的事项:

  • 使用键盘事件的控件需要获取焦点,QGraphicsItem的话使用 setFocus()开启
  • 需要使用setFlag()函数开启标志。(不开启这个不能使用,是一个坑)

 enum QGraphicsItem::GraphicsItemFlag:(这几个是常见的,想要更加了解的话可以翻看官方文档)

QGraphicsItem::ItemIsMovable支持使用鼠标进行交互式移动。通过单击该项目然后拖动,该项目将与鼠标光标一起移动。
QGraphicsItem::ItemIsSelectable支持选择。启用此功能将启用 setSelected() 来切换项目的选择。
QGraphicsItem::ItemIsFocusable该项支持键盘输入焦点(即,它是输入项)。启用此标志将允许项目接受焦点
QGraphicsItem::ItemClipsToShape项目将剪辑到其自己的形状。该项目无法绘制或接收鼠标、平板电脑、拖放或将事件悬停在其形状之外。默认情况下处于禁用状态
QGraphicsItem::ItemClipsChildrenToShap项目将其所有后代的绘画剪辑成自己的形状。作为此项目的直接或间接子项的项不能在此项的形状之外绘制。
QGraphicsItem::ItemIgnoresTransformations项目忽略继承的变换,此标志可用于使文本标签项保持水平且不缩放,因此在转换视图时它们仍可读。

开启键盘的话,需要使用setFlag(QGraphicsItem::ItemIsFocusable)

例子:使用鼠标选取项,然后使用键盘的上下左右来移动矩形项,每次移动10

MyItem.h

#ifndef MYITEM_H
#define MYITEM_H
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QKeyEvent>
#include<QMouseEvent>
class MyItem:public QGraphicsItem
{
public:MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );void keyPressEvent(QKeyEvent *event) ;//键盘点击void mousePressEvent(QGraphicsSceneMouseEvent *event);//鼠标点击事件
private:QColor color;//颜色
};#endif // MYITEM_H

 MyItem.cpp

#include "myitem.h"MyItem::MyItem()
{color=QColor(Qt::black);//默认为黑色this->setFlag(QGraphicsItem::ItemIsFocusable);//设置标志
}
QRectF MyItem::boundingRect() const
{qreal penwidget=1;return QRectF(-penwidget/2,-penwidget/2,100+penwidget,100+penwidget);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{painter->setBrush(color);painter->drawRect(0,0,100,100);//画矩形
}
void MyItem::keyPressEvent(QKeyEvent *event) //键盘点击
{if(event->key()==Qt::Key_Up)//向上{moveBy(0,-10);}else if(event->key()==Qt::Key_Down)//向下{moveBy(0,10);}else if(event->key()==Qt::Key_Left)//向左{moveBy(-10,0);}else if(event->key()==Qt::Key_Right)//向右{moveBy(10,0);}else{}
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{setFocus();//设置焦点
}

 

拖拽事件:

dragEnterEvent()拖动输入事件
dragLeaveEvent()拖拽离开事件
dragMoveEvent()拖动移动事件
dragEvent()拖拽事件

使用时需要注意的事项:

  • 默认不会开启拖拽,需要使用  setAcceptDrops(true)开启
  • 想要实现拖动控件的话还要开启 setFlag(QGraphicsItem::ItemIsMovable);

开启这两个函数即可实现拖拽控件:

//MyItem.h文件#ifndef MYITEM_H
#define MYITEM_H
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QDropEvent>
class MyItem:public QGraphicsItem
{
public:MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );
private:QColor color;//颜色
};#endif // MYITEM_H//MyItem.cpp文件#include "myitem.h"MyItem::MyItem()
{color=QColor(Qt::black);//默认为黑色this->setFlag(QGraphicsItem::ItemIsMovable);setAcceptDrops(true);//开启拖拽
}
QRectF MyItem::boundingRect() const
{qreal penwidget=1;return QRectF(-penwidget/2,-penwidget/2,100+penwidget,100+penwidget);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{painter->setBrush(color);painter->drawRect(0,0,100,100);//画矩形
}

可以使用上面的几个事件实现你想要的结果,这里就不详细赘述。

上下文菜单事件:

通俗的讲就是: 你右键一个项,会弹出一些选择

contextMenuEvent()重新实现此事件处理程序以处理上下文菜单事件
void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{QMenu menu;//创建一个菜单QAction *removeAction = menu.addAction("Remove");//创建QAction创建行为 ...//可以有多个menu.exec(event->screenPos());//显示menu,设置在上下文菜单时鼠标光标在屏幕坐标中的位置connect();//使用connect()来连接对应的处理结果}

例子:

MyItem.h

#ifndef MYITEM_H
#define MYITEM_H
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QGraphicsView>
#include<QDropEvent>
#include<QDebug>
class MyItem:public QGraphicsItem
{
public:MyItem();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget );void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;//上下文事件
private:QColor color;//颜色
};#endif // MYITEM_H

MyItem.cpp

#include "myitem.h"#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>MyItem::MyItem()
{color=QColor(Qt::black);//默认为黑色this->setFlag(QGraphicsItem::ItemIsMovable);setAcceptDrops(true);//开启拖拽
}
QRectF MyItem::boundingRect() const
{qreal penwidget=1;return QRectF(-penwidget/2,-penwidget/2,100+penwidget,100+penwidget);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{painter->setBrush(color);painter->drawRect(0,0,100,100);//画矩形
}void MyItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{QMenu menu;QAction *A = menu.addAction("A");QAction *B = menu.addAction("B");QAction *C = menu.addAction("C");QAction *D = menu.addAction("D");QObject::connect(A,&QAction::triggered,[=](){qDebug()<<"A";});QObject::connect(B,&QAction::triggered,[=](){qDebug()<<"B";});QObject::connect(C,&QAction::triggered,[=](){qDebug()<<"C";});QObject::connect(D,&QAction::triggered,[=](){qDebug()<<"D";});menu.exec(event->screenPos());
}

main函数:

int main(int argc, char *argv[])
{QApplication a(argc, argv);QGraphicsScene scene(-200,-200,400,400);MyItem item;scene.addItem(&item);QGraphicsView view;view.setScene(&scene);view.show();return a.exec();
}

运行效果:

右键点击该控件:

分别点击ABCD,执行相应的输出:

 

参考文档:

QGraphicsItem Class | Qt Widgets 5.15.13


http://www.ppmy.cn/news/33733.html

相关文章

技术分享 | PBM备份恢复

作者&#xff1a;张洪 爱可生南区 DBA 团队成员&#xff0c;主要负责mysql故障处理及相关技术支持。爱好旅游&#xff0c;摄影。 本文来源&#xff1a;原创投稿 *爱可生开源社区出品&#xff0c;原创内容未经授权不得随意使用&#xff0c;转载请联系小编并注明来源。 概述 Per…

下载并导入MySQL示例数据库employees

MySQL示例数据库employees一、下载employees数据库二、MySQL官方参考手册三、具体步骤3.1 下载test_db3.2 在test_db-master中打开cmd(进入test_db-master目录)3.3 run-install3.4 验证employee数据3.5 show databases\tables & select * from departments**3.6 select * f…

前端git必备技能,如何合并分支以及出现合并冲突后如何解决

文章目录一、合并分支二、可能出现的冲突和解决三、过程分享一、合并分支 注意&#xff0c;我们常说的master或main主干也可以理解为分支&#xff0c;可以是分支合并到主干&#xff0c;或分支合并到分支。 需求&#xff1a;cloudweb的2.6.0和2.6.1是并行开发的&#xff0c;现…

linux中写定时任务

场景&#xff1a;我们生产环境中有大量的日志记录&#xff0c;但是我们的磁盘没有太大&#xff0c;需要定时清理磁盘 文章目录crond 定时任务详解安装定时任务crontab服务启动与关闭crontab操作crontab 命令test.sh查看日志丢弃linux中的执行日志Linux进入nano模式方式一方式二…

【8】核心易中期刊推荐——人工智能与机器人

🚀🚀🚀NEW!!!核心易中期刊推荐栏目来啦 ~ 📚🍀 核心期刊在国内的应用范围非常广,核心期刊发表论文是国内很多作者晋升的硬性要求,并且在国内属于顶尖论文发表,具有很高的学术价值。在中文核心目录体系中,权威代表有CSSCI、CSCD和北大核心。其中,中文期刊的数…

【代码随想录-刷题学习JavaScript】day2-part02数组

继续数组的部分 977.有序数组的平方 &#xff0c;209.长度最小的子数组 &#xff0c;59.螺旋矩阵II 今天会有个小结 一、LeetCode977.有序数组的平方 文章讲解 视频讲解 二、LeetCode 209.长度最小的子数组 题目建议&#xff1a; 本题关键在于理解滑动窗口&#xff0c;这个滑动…

太强了,英伟达面对ChatGPT还有这一招...

大家好&#xff0c;我是 Jack。 今年可谓是 AI 元年&#xff0c;ChatGPT、AIGC、VITS 都火了一波。 我也先后发布了这几期视频&#xff1a; 这是一个大模型的时代&#xff0c;AI 能在文本、图像、音频等领域大放异彩&#xff0c;得益于大模型。而想要预训练大模型&#xff0c…

int *p = a、p = a、*p = a

int *p &a; //初始化一个int *类型指针&#xff0c;同时将变量a的地址存入p指针这里是一个特殊用法&#xff0c;仅在初始化变量的时候可以使用&#xff0c;应分为两个部分去进行理解。int *p; //初始化一个int * 类型指针pp &a; //将变量a的地址存入p指针&#xff0c…

基于深度学习的动物识别系统(YOLOv5清新界面版,Python代码)

摘要&#xff1a;动物识别系统用于识别和统计常见动物数量&#xff0c;通过深度学习技术检测日常几种动物图像识别&#xff0c;支持图片、视频和摄像头画面等形式。在介绍算法原理的同时&#xff0c;给出Python的实现代码、训练数据集以及PyQt的UI界面。动物识别系统主要用于常…

哈夫曼编码、哈夫曼树

已知一个文件中出现的各字符及其对应的率如下表所示。若采用定长编码&#xff0c;则该文件中字符的码长应为( )。若采用Huffman编码&#xff0c;则字符序列face的编码应为&#xff08; ) 字符abcdef频率4513121695码长决定了可以显示几位字符&#xff0c;题中一共有6位&#x…

【Linux】环境变量(基本概念 常见环境变量 测试PATH 环境变量相关命令)

文章目录环境变量基本概念常见环境变量测试PATH别的环境变量通过系统调用获取或设置环境变量环境变量相关命令export: 设置一个新的环境变量set: 显示本地定义的shell变量和环境变量unset: 清除环境变量通过代码如何获取环境变量环境变量 基本概念 环境变量(environment vari…

ThreadPool线程池源码解析

ThreadPool线程池源码解析 文章目录前言一、基本使用二、执行流程三、源码分析ThreadPoolExecutor 中重要属性ThreadPoolExecutor 内部类Workerexecute&#xff08;&#xff09;方法addWorker(command, true)方法runWorker(worker )方法getTask()方法shutdown和shutdownNow四、…

Java栈和队列·下

Java栈和队列下2. 队列(Queue)2.1 概念2.2 实现2.3 相似方法的区别2.4 循环队列3. 双端队列 (Deque)3.1 概念4.java中的栈和队列5. 栈和队列面试题大家好&#xff0c;我是晓星航。今天为大家带来的是 Java栈和队列下 的讲解&#xff01;&#x1f600; 继上一个讲完的栈后&…

1.9 日本蜡烛图技术之支撑和压力的其他含义

破低反涨和破高反跌形态 支撑和压力的研究不能局限于涨跌幅边界研究&#xff0c;用K线图来验证会注意到很多突破来临的信号破低反涨形态 一种移动和反向移动&#xff0c;价格跌破支撑&#xff0c;然后反弹重新上涨通常建立新的支撑线 破高反跌形态&#xff1a;突破压力后&…

hadoop理论基础(一)

1.hadoop的组成2 HDFS概述HDFS&#xff08;Hadoop Distributed File System&#xff09;是一个分布式文件系统&#xff08;1&#xff09;NameNode:存储文件的元数据;如文件名、文件目录结构、文件属性&#xff0c;以及每个文件的块列表和块所在的DataNode等。(2)DataNode:在本地…

【测试开发篇4】测试模型

目录 一、软件测试V模型 编码前 概要设计&#xff1a; 详细设计&#xff1a; 编码后&#xff1a; 单元测试&集成测试 系统测试 验收测试 V模型的特点 优点&#xff1a; 缺点&#xff1a; 二、软件测试W模型 编码之前&#xff1a; 编码的时候&#xff1a; 编…

Three.js——learn01

Three.js——learn01Three.js——learn01本地搭建文档通过parcel搭建Threejs环境1.初始化2.安装parcel设置打包位置3.设置目录结构QuickStart安装threejsindex.htmlindex.cssindex.js启动Three.js——learn01 本地搭建文档 登录GitHub搜索three.js git clone https://github…

KubeSphere All in one安装配置手册

KubeSphere All in one安装配置手册 1. 初始化 1.1 配置apt源 # vi /etc/apt/sources.list deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiversedeb…

【多线程】多线程案例

✨个人主页&#xff1a;bit me&#x1f447; ✨当前专栏&#xff1a;Java EE初阶&#x1f447; ✨每日一语&#xff1a;we can not judge the value of a moment until it becomes a memory. 目 录&#x1f35d;一. 单例模式&#x1f364;1. 饿汉模式实现&#x1f9aa;2. 懒汉模…

redhat httpd服务安装、访问浏览器及自定义访问页面

目录 1.查看本地仓库&#xff0c;如果没有配置&#xff0c;就在这目录创建一个 2.挂载 3.下载httpd服务 4.修改httpd配置文件 5.重启httpd服务 6.查看当前可用IP地址 7.随便用一个IP 看是否有东西 8.无法访问&#xff0c;原因是我们防火墙没有放行httpd服务 1.查看本地仓库&a…
最新文章