PDA

View Full Version : Not able to move QGraphicsEllipseItem on the scene



alex.agape94
5th September 2017, 09:42
Hi guys!
I've been trying to solve this problem for a couple of days now, and I can't seem to be able to find a solution for it.
Basically I have my own QGraphicsView, with my own QGraphicsScene and my own QGraphicsEllipseItem. I want to be able to move the Ellipse in the scene.
Until now I tried using the mouse events and the itemChange functions, but with no success.
In short, I want to be able to move the points in my scene only on the y-axis. Using the mouse events I was able to move the points, but they were all over the place. With the itemChange function they don't move at all.
Any ideas?


Here is some code:

The QGraphicsEllipseItem class implementation:


PunctGrafic::PunctGrafic(int x, int y, QGraphicsItem *parent)
{
// this->setParentItem(parent);
QPen pen;
pen.setBrush(QBrush(Qt::SolidPattern));
pen.setWidth(3);
pen.setColor(QColor(Qt::red));
this->setPen(pen);
this->setRect(x - 1.5 , y - 1.5 , 3 , 3);
setFlag(QGraphicsItem::ItemIsSelectable,true);
setFlag(QGraphicsItem::ItemIsMovable,true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges,tr ue);
setFlag(QGraphicsItem::ItemSendsScenePositionChang es,true);

}

QVariant PunctGrafic::itemChange(GraphicsItemChange change, const QVariant &value)
{

if (change == QGraphicsItem::ItemPositionChange)
return QPointF(pos().x(), value.toPointF().y());
return QGraphicsItem::itemChange(change, value);

}

and here is what i tried to do with the mouse events, but to no success:


void PunctGrafic::mousePressEvent(QGraphicsSceneMouseEv ent *event)
{
if(event->button() == Qt::LeftButton)
{
//moveStarted = true;
this->setCursor(QCursor(Qt::ClosedHandCursor));
QGraphicsItem::mousePressEvent(event);
event->accept();
return;
}
event->ignore();
}
////---------------------------------------------------------------------------------------------
void PunctGrafic::mouseMoveEvent(QGraphicsSceneMouseEve nt *event)
{
if(event->button() == Qt::LeftButton)
{/*
int g =
int h = mapToScene(event->pos()).y();*/
this->setPos(mapToItem(parentItem(),mapToParent(event->pos()).x(),mapToParent(event->pos()).y()));
QGraphicsItem::mouseMoveEvent(event);

event->accept();
return;
}
event->ignore();
}
////---------------------------------------------------------------------------------------------
void PunctGrafic::mouseReleaseEvent(QGraphicsSceneMouse Event *event)
{
if (event->button() == Qt::LeftButton)
{
//moveStarted = false;
this->setCursor(QCursor(Qt::ArrowCursor));
QGraphicsItem::mouseReleaseEvent(event);
event->accept();
return;
}
event->ignore();
}
//---------------------------------------------------------------------------------------------


Thank you!

Ginsengelf
7th September 2017, 08:36
Hi, if you want to move the item only in Y direction, why do you change the X position?
this->setPos(mapToItem(parentItem(),mapToParent(event->pos()).x(),mapToParent(event->pos()).y()));

Ginsengelf

alex.agape94
7th September 2017, 08:51
Thank you for responding!

The issue is that they don't move at all. I was thinking that it has to be a flag or something that I forgot or something.
I simply don't understand why they refuse to move. I tried debugging, and when I click an item, the itemChange() function is being called, but it doesn't enter the if statement with change == QGraphicsItem::ItemPositionChange . In the debugging window, change = ItemClickChange or something like that, so it just sees the click, not the move event. I don't know why...

Santosh Reddy
7th September 2017, 09:14
No need intercept mouse events.


class EllipseItem : public QGraphicsEllipseItem
{
public:
EllipseItem(const QRectF & rect, QGraphicsEllipseItem * parent = nullptr)
: QGraphicsEllipseItem(rect, parent)
{
setFlags(ItemIsMovable | ItemSendsGeometryChanges);
}

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant & value) override
{
if(change == ItemPositionChange)
return QPointF(scenePos().x(), value.toPointF().y());

return QGraphicsEllipseItem::itemChange(change, value);
}
};