博客
关于我
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笔记:InnoDB的锁机制
查看>>
MySQL简单查询
查看>>
mysql类型转换函数convert与cast的用法
查看>>
mysql系列一
查看>>
MySQL系列之数据类型(Date&Time)
查看>>
Mysql系列之锁机制
查看>>
Mysql系列九:使用zookeeper管理远程Mycat配置文件、Mycat监控、Mycat数据迁移(扩容)...
查看>>
mysql索引
查看>>
mysql索引
查看>>
Mysql索引,索引的优化,如何避免索引失效案例
查看>>
mysql索引、索引优化(这一篇包括所有)
查看>>
MySql索引为什么使用B+树
查看>>
WARNING!VisualDDK wizard was unable to find any DDK/WDK installed on your system.
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>