1 Attachment(s)
Not able to move QGraphicsEllipseItem on the scene
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:
Code:
{
// this->setParentItem(parent);
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);
}
QVariant PunctGrafic
::itemChange(GraphicsItemChange change,
const QVariant &value
) {
return QPointF(pos
().
x(), value.
toPointF().
y());
}
and here is what i tried to do with the mouse events, but to no success:
Code:
{
if(event->button() == Qt::LeftButton)
{
//moveStarted = true;
this
->setCursor
(QCursor(Qt
::ClosedHandCursor));
event->accept();
return;
}
event->ignore();
}
////---------------------------------------------------------------------------------------------
{
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()));
event->accept();
return;
}
event->ignore();
}
////---------------------------------------------------------------------------------------------
{
if (event->button() == Qt::LeftButton)
{
//moveStarted = false;
this
->setCursor
(QCursor(Qt
::ArrowCursor));
event->accept();
return;
}
event->ignore();
}
//---------------------------------------------------------------------------------------------
Thank you!
Re: Not able to move QGraphicsEllipseItem on the scene
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
Re: Not able to move QGraphicsEllipseItem on the scene
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...
Re: Not able to move QGraphicsEllipseItem on the scene
No need intercept mouse events.
Code:
{
public:
{
setFlags(ItemIsMovable | ItemSendsGeometryChanges);
}
protected:
{
if(change == ItemPositionChange)
return QPointF(scenePos
().
x(), value.
toPointF().
y());
}
};