【发布时间】:2014-12-12 11:25:19
【问题描述】:
我试图了解如何重新定义在QGraphicsScene 中选择和转换(一旦选择)项目的方式。例如改变一条线的长度,移动一条线,通过移动它的一个点来改变一个多边形。
我创建了QGraphicsView 的一个子级,并开始重载它的mousePressEvent,但似乎选择和移动动作被QGraphicsItem 捕获。我如何覆盖它,因为它们受到保护并且对QGraphicsView 的孩子不可见?
我可以想象我需要在myGraphicsItem 中重载QGraphicsItem::mousePressEvent,但这意味着我还必须重载QGraphicsScene 来处理myGraphicsItem?当它被移动时,我如何处理场景中的选定项目位置?
有什么可以看的例子吗?
我(显然)有点失落。
更新:根据反馈,我创建了一个QGraphicsItems 的孩子,如下所示:
class baseGraphicItem : public QGraphicsItem
{
public:
explicit baseGraphicItem(QVector<QPoint> data, operationType shape, QObject * parent = 0);
signals:
public slots:
public:
virtual QRectF boundingRect() const;
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
QPainterPath shape() const;
virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
private:
QPolygon vertex;
operationType shapeType;
};
baseGraphicItem::baseGraphicItem(QVector<QPoint> data, operationType shape, QObject *parent) :
QGraphicsItem(), vertex(data), shapeType(shape)
{
qDebug() << vertex;
this->setAcceptHoverEvents(false);
}
这些用于绘画、boundingRect 和形状。
void baseGraphicItem::paint(QPainter * painter, const QStyleOptionGraphicsItem*, QWidget*)
{
int i=0;
// Following needs better code for polygons
do painter->drawLine(vertex.at(i), vertex.at(i+1));
while (i++<vertex.size()-2);
}
QRectF baseGraphicItem::boundingRect() const
{
return vertex.boundingRect();
}
QPainterPath baseGraphicItem::shape() const
{
QPainterPath path;
path.addPolygon(vertex);
return path;
}
不幸的是,选择适用于一条线或多边形。但是当一条线在多边形内时,几乎总是选择多边形而不是线。这是因为boundingRect 还是形状?另外,如何获取存储在QPolygon 顶点中的新坐标?
谢谢
【问题讨论】:
-
如果我没看错的话,QGraphicsView 被子类化并用于捕获事件。我正在这样做,我无法捕获选择,因为它似乎是由 QGraphicsItem::mousepressevent 而不是由 QGraphicsView::mousepressevent 完成的。
标签: qt selection qgraphicsitem