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:
{
// 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());
}
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,true);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges,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);
}
To copy to clipboard, switch view to plain text mode
and here is what i tried to do with the mouse events, but to no success:
{
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();
}
//---------------------------------------------------------------------------------------------
void PunctGrafic::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
//moveStarted = true;
this->setCursor(QCursor(Qt::ClosedHandCursor));
QGraphicsItem::mousePressEvent(event);
event->accept();
return;
}
event->ignore();
}
////---------------------------------------------------------------------------------------------
void PunctGrafic::mouseMoveEvent(QGraphicsSceneMouseEvent *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(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
//moveStarted = false;
this->setCursor(QCursor(Qt::ArrowCursor));
QGraphicsItem::mouseReleaseEvent(event);
event->accept();
return;
}
event->ignore();
}
//---------------------------------------------------------------------------------------------
To copy to clipboard, switch view to plain text mode
Thank you!
Bookmarks