PDA

View Full Version : Make QGraphicsItem not moveable in QGraphicsScene



Noroxus
18th March 2015, 02:28
I'm trying to make the qgraphicsitem not moveable. I tried setting the flag when its being clicked and tried setting it when the item is created etc. I can call flags(); and it shows that ItemIsMovable is not in the flags list yet it is still able to be moved. I have noticed when I pop up a modeless dialog in my mainwindow.cpp then go back to the mainwindow without closing the dialog and try to move an item it then recognizes the flag and I can't move the item until I close the dialog. I'm not sure why qgraphicsscene is ignoring the flag. Can anyone please explain how I can get this to work?

Here is some of my code stripped down to the relevant stuff.


//GraphScene.h
class GraphScene : public QGraphicsScene
{
private:
QGraphicsItem* x;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};

//GraphScene.cpp
void GraphScene::mousePressEvent(QGraphicsSceneMouseEve nt *event)
{

x = qgraphicsitem_cast<QGraphicsItem*>(itemAt(event->scenePos(), QTransform()));

if (x)
{
x->setFlag(QGraphicsItem::ItemIsMovable,false);
}

QGraphicsScene::mousePressEvent(event);
}

//y.h
class y : public QGraphicsObject
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
};

//y.cpp
y::y()
{
setFlags(ItemIsSelectable | ItemIsFocusable);
this->setFlag(QGraphicsItem::ItemIsMovable,false);
setAcceptHoverEvents(true);
}
void y::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
update();
}

wysota
18th March 2015, 06:25
Please provide a minimum compilable example reproducing the problem.