Qt 拖动QGraphicsItem时的奇怪行为
我试图在Qt中创建一个箭头项,因此我基于以下示例: 唯一的区别是我想将箭头连接到一个项目,而不是两个项目。 除了我尝试拖动我的物品时,它会留下一条模糊的轨迹,就好像它在画整个拖动运动,如果我按住alt tab键,它就会消失 在拖动之前,请先执行以下操作: 接下来是: 只有箭头的行为是这样的,我相信它的绘画事件可能有问题:Qt 拖动QGraphicsItem时的奇怪行为,qt,draggable,qgraphicsitem,Qt,Draggable,Qgraphicsitem,我试图在Qt中创建一个箭头项,因此我基于以下示例: 唯一的区别是我想将箭头连接到一个项目,而不是两个项目。 除了我尝试拖动我的物品时,它会留下一条模糊的轨迹,就好像它在画整个拖动运动,如果我按住alt tab键,它就会消失 在拖动之前,请先执行以下操作: 接下来是: 只有箭头的行为是这样的,我相信它的绘画事件可能有问题: void myArrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget
void myArrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
qreal arrowSize = 20;
double angle = std::atan2(-line().dy(), line().dx());
QPointF arrowP1 = line().p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line().p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
arrowHead.clear();
arrowHead << line().p1() << arrowP1 << arrowP2;
painter->drawLine(line());
painter->drawPolygon(arrowHead);
}
void myArrow::paint(QPainter*painter,const QStyleOptionGraphicsItem*选项,QWidget*小部件){
qreal arrowSize=20;
双角度=std::atan2(-line().dy(),line().dx());
QPointF箭头p1=直线().p1()+QPointF(sin(角度+M_π/3)*箭头尺寸,
cos(角度+M_PI/3)*箭头尺寸);
QPointF箭头P2=直线().p1()+QPointF(sin(角度+M_π-M_π/3)*箭头尺寸,
cos(角度+M_PI-M_PI/3)*箭头大小);
箭头。清除();
箭头drawPolygon(箭头)
线条本身工作正常
以下是整个源项目:
有什么提示吗?问题是
MyArrow
的boundingRect
只将QLineF
作为基础,而不是产生不希望的效果的箭头
我的解决方案建议使用QGraphicsPathItem
而不是QGraphicsLineItem
,此外,我不理解您在moveLineToCenter
中实现的逻辑,我认为它是不必要的,我已修改它以更新其位置:
myarrow.h
#ifndef MYARROW_H
#define MYARROW_H
class CustomRect;
#include <QGraphicsPathItem>
class MyArrow : public QGraphicsPathItem
{
public:
MyArrow(CustomRect *rect);
void updatePosition(QPointF p1, QPointF p2);
private:
CustomRect *myrect;
};
#endif // MYARROW_H
#ifndef CUSTOMRECT_H
#define CUSTOMRECT_H
#include <QGraphicsPixmapItem>
class MyArrow;
class CustomRect : public QGraphicsRectItem
{
public:
CustomRect (const QRectF& rect);
void addLine(MyArrow *line);
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void moveLineToCenter(QPointF newPos);
private:
QList<MyArrow *> arrows;
};
#endif // CUSTOMRECT_H
\ifndef MYARROW\u H
#定义我的箭头
类CustomRect;
#包括
类MyArrow:公共QGraphicsPathItem
{
公众:
MyArrow(CustomRect*rect);
无效更新位置(QPointF p1、QPointF p2);
私人:
CustomRect*myrect;
};
#endif//MYARROW_H
myarrow.cpp
#include "customrect.h"
#include "myarrow.h"
#include <QPainter>
#include <cmath>
MyArrow::MyArrow(CustomRect *rect){
myrect = rect;
QPointF p = rect->boundingRect().center();
updatePosition(p - QPointF(0, 50), p - QPointF(0, 250));
setFlag(QGraphicsLineItem::ItemIsSelectable);
}
void MyArrow::updatePosition(QPointF p1, QPointF p2)
{
const qreal arrowSize = 20;
QPainterPath path;
path.moveTo(p1);
path.lineTo(p2);
QPointF diff = p2-p1;
double angle = std::atan2(-diff.y(), diff.x());
QPointF arrowP1 = p1 + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = p1 + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF arrowHead;
arrowHead << p1 << arrowP1 << arrowP2 << p1;
path.moveTo(p1);
path.addPolygon(arrowHead);
setPath(path);
}
#include "customrect.h"
#include "myarrow.h"
CustomRect::CustomRect(const QRectF &rect) : QGraphicsRectItem(rect){
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
}
void CustomRect::addLine(MyArrow *line) {
arrows << line;
}
QVariant CustomRect::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPointF newPos = value.toPointF();
moveLineToCenter(newPos);
}
return QGraphicsItem::itemChange(change, value);
}
void CustomRect::moveLineToCenter(QPointF newPos) {
for(MyArrow *arrow: arrows) {
arrow->setPos(newPos);
}
}
#包括“customrect.h”
#包括“myarrow.h”
#包括
#包括
MyArrow::MyArrow(CustomRect*rect){
myrect=rect;
QPointF p=rect->boundingRect().center();
更新位置(p-QPointF(0,50),p-QPointF(0,250));
setFlag(QGraphicsLineItem::ItemIsSelectable);
}
void MyArrow::updatePosition(QPointF p1、QPointF p2)
{
常数qreal arrowSize=20;
QPainterPath路径;
路径移动到(p1);
线路图(p2);
QPointF diff=p2-p1;
双角度=标准::atan2(-diff.y(),diff.x());
QPointF箭头p1=p1+QPointF(sin(角度+M_π/3)*箭头尺寸,
cos(角度+M_PI/3)*箭头尺寸);
QPointF箭头P2=p1+QPointF(sin(角度+M_π-M_π/3)*箭头尺寸,
cos(角度+M_PI-M_PI/3)*箭头大小);
QPolygonF箭头;
arrowHead很好,谢谢你的帮助。你能更详细地解释是什么导致了我的问题,以及使用QGraphicsPathItem是如何解决的吗?我很感激,因为我还在学习Qt。