博客
关于我
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基础系列—SQL分类之一
查看>>
MySQL处理千万级数据分页查询的优化方案
查看>>
mysql备份
查看>>
mysql备份与恢复
查看>>
mysql备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>