博客
关于我
QT自定义QGraphicsRectItem
阅读量:511 次
发布时间:2019-03-07

本文共 1286 字,大约阅读时间需要 4 分钟。

PS:添加自定义类时需要继承QObject,来获得信号与槽功能。

//myrectitem.h#ifndef MYRECTITEM_H#define MYRECTITEM_H#include 
#include
#include
#include
#include
#include
//自定义QGraphicsRectItem,使其1.被单击时,状态栏显示标签名;2.右键显示删除菜单class MyRectItem : public QObject, public QGraphicsRectItem{ Q_OBJECTpublic: explicit MyRectItem(const QRectF & rect, QGraphicsItem *parent = nullptr);protected: void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);signals: void sendDelete(MyRectItem*); void show(MyRectItem*);public slots: void slotRemoveItem();};#endif // MYRECTITEM_H
//myrectitem.cpp#include "myrectitem.h"MyRectItem::MyRectItem(const QRectF & rect, QGraphicsItem *parent) : QGraphicsRectItem(rect, parent){   }void MyRectItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event){       QMenu menu;    QAction *removeAction = menu.addAction("删除");    connect(removeAction,SIGNAL(triggered()),this,SLOT(slotRemoveItem()));    menu.exec(event->screenPos());}void MyRectItem::slotRemoveItem(){       emit sendDelete(this);}void MyRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event){       emit show(this);}

PS:父组件.cpp文件需要在创建MyRectItem时connect两个信号与槽,在删除时disconnect。

转载地址:http://fvknz.baihongyu.com/

你可能感兴趣的文章
MySQL常用日期格式转换函数、字符串函数、聚合函数详
查看>>
MySQL常见错误分析与解决方法总结
查看>>
MySQL底层概述—2.InnoDB磁盘结构
查看>>
MySQL底层概述—3.InnoDB线程模型
查看>>
MySQL底层概述—5.InnoDB参数优化
查看>>
MySQL底层概述—6.索引原理
查看>>
MySQL底层概述—7.优化原则及慢查询
查看>>
MySQL底层概述—8.JOIN排序索引优化
查看>>
MySQL底层概述—9.ACID与事务
查看>>
Mysql建立中英文全文索引(mysql5.7以上)
查看>>
mysql建立索引的几大原则
查看>>
Mysql建表中的 “FEDERATED 引擎连接失败 - Server Name Doesn‘t Exist“ 解决方法
查看>>
MySQL开源工具推荐,有了它我卸了珍藏多年Nactive!
查看>>
MySQL异步操作在C++中的应用
查看>>
Mysql当前列的值等于上一行的值累加前一列的值
查看>>
MySQL当查询的时候有多个结果,但需要返回一条的情况用GROUP_CONCAT拼接
查看>>
MySQL必知必会(组合Where子句,Not和In操作符)
查看>>
MySQL必知必会总结笔记
查看>>
MySQL快速入门
查看>>
MySQL快速入门——库的操作
查看>>