博客
关于我
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导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1...
查看>>
mysql导入(ibd文件)
查看>>
Mysql工作笔记006---Mysql服务器磁盘爆满了_java.sql.SQLException: Error writing file ‘tmp/MYfXO41p‘
查看>>
MySQL工具1:mysqladmin
查看>>
mysql常用命令
查看>>
MySQL常用命令
查看>>
mysql常用命令
查看>>
MySQL常用指令集
查看>>
mysql常用操作
查看>>
MySQL常用日期格式转换函数、字符串函数、聚合函数详
查看>>
MySQL常见架构的应用
查看>>
MySQL常见的三种存储引擎(InnoDB、MyISAM、MEMORY)
查看>>
MySQL常见约束条件
查看>>
MySQL常见错误
查看>>
MySQL常见错误分析与解决方法总结
查看>>
mysql并发死锁案例
查看>>
MySQL底层概述—1.InnoDB内存结构
查看>>
MySQL底层概述—2.InnoDB磁盘结构
查看>>
MySQL底层概述—3.InnoDB线程模型
查看>>
MySQL底层概述—4.InnoDB数据文件
查看>>