若该文为原创文章,未经允许不得转载
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/88869308
各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)
Qt开发专栏:实用技巧(点击传送门)
需求
做白板中遇到图元,图元画直线返回也是矩形,让点击直线的线才选中否则不选择(虽然在boundrect内)
解决方法1(不行):画直线只能点击最中间,点击到了又变成整体了
最开始设置图元选择属性为:
setFlags(ItemIsSelectable | ItemIsMovable);
改为
setFlags(ItemIsSelectable | ItemIsMovable | ItemClipsToShape);
解决方法2(推荐)
重载QGraphicsItem::share() const;
修改前
#include "LineItem.h" #include <QPainter> LineItem::LineItem(QObject *parent) : XGraphicsItem(parent) { } QRectF LineItem::boundingRect() const { return QRectF(_x, _y, _width, _height); } void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setPen(QPen(Qt::red, 10)); painter->drawLine(_x, _y, _x+_width, _y+_height); }
画图形后,选择boundrect局域选择(非红色直线)可移动(录制的看不到鼠标)
修改后
#include "LineItem.h" #include <QPainter> LineItem::LineItem(QObject *parent) : XGraphicsItem(parent) { } QRectF LineItem::boundingRect() const { return QRectF(_x, _y, _width, _height); } QPainterPath LineItem::shape() const { QPainterPath path; QPainterPathStroker painterPathStroker; // 特别要注意笔宽度(其他图形类似于边框宽度) painterPathStroker.setWidth(10); path.moveTo(_x, _y); path.lineTo(_x+_width, _y+_height); return painterPathStroker.createStroke(path); } void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setPen(QPen(Qt::red, 10)); painter->drawLine(_x, _y, _x+_width, _y+_height); }
只有点击到红线上才可以移动(录制的看不到鼠标),重叠的时候,哪个图元Z轴在前就选择的是哪个
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客导航:https://blog.csdn.net/qq21497936/article/details/102478062
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/88869308