0
votes

All,

I have a QGraphicsEllipseItem with setFlags(Qt::ItemIsSelectable | Qt::ItemIsMovable). This allows me drag and move ellipses quickly in a QGraphicsView.

I decided to be fancy, and setCursor(Qt::OpenHandCursor) to let the user know they can move it by clicking. However, now it won't let go when I let go of the left mouse button? What am I doing wrong?


Example code: Custom QGraphicItem and Repaint Issues

Note: I removed the update() calls, and added prepareGeometryChange() calls. Now modify the MakeNewPoint function:

QGraphicsEllipseItem * InteractivePolygon::MakeNewPoint(QPointF & new_point)
{
   QGraphicsEllipseItem * result = 0;
   result = new QGraphicsEllipseItem();
   result->setPos(new_point);
   result->setRect(-4, -4, 8, 8);
   result->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable)
   result->setCursor(Qt::OpenHandCursor); //Setting this removes my ability to let go of an item. NOTE: result is parented by this.

   return result;
}

later:

 QGraphicsEllipseItem * new_item = MakeNewPoint(bla);
 new_item->setParent(this);
 //add it to my QList<QGraphicsEllipseItem *> m_points;

I would like to note that my QGraphicsEllipseItem is parented by a custom QGraphicsItem. I don't change the parents/Custom Item cursor, only the ellipse's. I do not experience this problem with non parented ellipses...


Interesting result: So my class custom QGraphicsItem class (the parent of the ellipses) is a QObject so I can filter incoming mouse events from the scene. I did a setCursor(Qt::ArrowCursor) in my custom class's constructor... and here's where it gets interesting:

The eventFilter now catches (event->type() == QEvent::GraphicsSceneMouseMove) even if a mouse button isn't pressed down. If I don't have the setCursor call, that event only fires while a mouse button is pressed... thoughts?

1
Could you provide some more details or code sample ? It's working fine for me - octal
"now it won't let go when I let go" The drag operation does not complete, or the cursor will not unset? - cmannett85
It also works fine for me. Are you subclassing QGraphicsEllipseItem? If so, are you reimplementing any of the mouse event handlers? - Anthony
See this question: stackoverflow.com/questions/13278281/… -- I subclass the QGraphicsItem and QObject class to make a polygon. I have indeed installed an event filter. - Constantin
@cmannett85 I am hesitant to say the drag operation does not complete as I do not use QDrag... the cursor will not unset. I let go of the mouse button and QGraphicsEllipseItem still moves with the cursor. - Constantin

1 Answers

2
votes

Okay got it, here's what's happening, it's intuitive once you realize it:

When you set that a QGraphicsItem to a unique cursor, the QGraphicsView has to setMouseTracking(true) otherwise the QGraphicsScene will never know when to change the cursor (ie when it's over the graphics item with the unique cursor.) The mouse move events were affecting my ellipse.

Normally, the QGraphicsScene only gets mouse move events when a button is held down.