Solved Items in QGraphicsScene are not movable
-
Hi, I am inheriting QGraphicsScene for the class Canvas as follows,
'class Canvas : public QGraphicsScene'
This is how I'm instantiating object of Canvas in the constructor of MainWindow
scene = new Canvas(this);
ui->graphicsView->setScene(scene);In the constructor, I am not not doing anything.
This is how I am adding object to the scene.
ellipse = scene->addEllipse(100,100,100,100,blackpen,bluebrush);
ellipse->setFlags(QGraphicsItem::ItemIsMovable);
rect = scene->addRect(-10,50,100,100,blackpen,redbrush);
rect->setFlags(QGraphicsItem::ItemIsMovable);My observation is that the items I have added are not movable instead of setting the flag as above seen.
Previously when I had not inherited QGraphicsScene but only made an object of QGraphicsScene, the above two items were movable in the scene.
Is there something I am missing out on.
Please Help!
Thanks! -
Try adding the following statement also,
ellipse->setAcceptHoverEvents( true ); -
Tried ellipse->setAcceptHoverEvents( true );
Doesn't work.
-
Hi,
ellipse = scene->addEllipse(100,100,100,100,blackpen,bluebrush);
ellipse->setFlags(QGraphicsItem::ItemIsMovable);
rect = scene->addRect(-10,50,100,100,blackpen,redbrush);
rect->setFlags(QGraphicsItem::ItemIsMovable);I added these items to a custom graphics scene and the move operation works perfectly for me.
Could you please share more info/code ?
Also are you overriding the mouse events for the graphics scene class?Regards,
San -
Hi, Yes I am overriding the mousePressEvent as follows. Whereas the constructor is empty.
void Canvas::mousePressEvent(QGraphicsSceneMouseEvent *gevent)
{/* Get the position of the mouse clicked */ QPointF pressedPosition(gevent->scenePos());
#ifdef DEBUG
qDebug("Mouse has been pressed");qreal temp_x = gevent->scenePos().x(); qDebug("The value of the Xcordinate for the mouse clicked is"); qDebug()<<temp_x; qreal temp_y = gevent->scenePos().y(); qDebug("The value of the Ycordinate for the mouse clicked is"); qDebug()<<temp_y;
#endif
/* Create the state here */ srstate = new StateItem(pressedPosition); mainWindowPtr->scene->addItem(srstate); srstate->setFlags(QGraphicsItem::ItemIsMovable);
}
Thanks!
-
Try adding the statement QGraphicsScene::mousePressEvent( event ); before the end of the function
-
@sanojsubran I tried that but it results in a compilation error.
-
@faiszalkhan
Hi
Can you list that error ?
Since its the base class that do the actual moving,
its critical to call it so lets find out what was wrong ? -
@mrjj @sanojsubran It was my bad. The solution worked. However I'm not very sure as to what we did there :(
-
The problem is not completely solved. StateItem is a custom graphics item which inherits QGraphicsItem.
The constructor of StateItem is as shown below,StateItem::StateItem(QPointF pos)
{
/* Get the position from the caller */
drawPos = new QPointF(pos.x(), pos.y());//TODO: Set item as movable
setFlags(QGraphicsItem::ItemIsMovable);}
This graphics Item is displayed on the scene but still is not movable.
Please help!
Thanks! -
Hi,
Your original problem was that by not calling the base class method implementation, you don't get the behaviour from the base class.
-
Since I am inheriting the QGraphicsItem in StateItem to create custom shape. Am I missing out something for setFlags(QGraphicsItem::ItemIsMovable); in the constructor to not work ?
Thanks
-
Hi
You might need
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);If we look at
http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.htmlIt all it does to flag the items.
-
@mrjj said in Items in QGraphicsScene are not movable:
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);Hi @mrjj , tried the same but still not working.
-
Ok. cant tell what is wrong then.
You might have overridden a virtual and forgot to call base class.Have a look at the diagram sample and see if can spot anything you
did different.Or try the StateItem in a default scene/view.
-
@mrjj I just have the constructor, paint function and bounding rect. The file is as follows,
StateItem::StateItem(QPointF pos)
{/* Get the position from the caller */ drawPos = new QPointF(pos.x(), pos.y()); //TODO: Set item as movable
// setFlags(QGraphicsItem::ItemIsFocusable);
// setFlags(QGraphicsItem::ItemIsSelectable);
setFlags(QGraphicsItem::ItemIsMovable);// QGraphicsItem::QGraphicsItem();
}QRectF StateItem::boundingRect() const
{
//TODO: Return a rectangle of calculated size
qreal penWidth = 1;
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth);}
/* Explicit call to this function is not required */
void StateItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget widget)
{
/ Set the pen */
QPen blackpen(Qt::black);
blackpen.setWidth(2);
painter->setPen(blackpen);/* Set colour of the state */ QColor statecolor(229,231,233); QBrush statebrush(statecolor); painter->setBrush(statebrush); /* Draw state item */ painter->drawRoundedRect(drawPos->x(),drawPos->y(),100,50,8,8); //x,y,width,height,xradius,yradius QPoint p(drawPos->x(),(drawPos->y() + 10)); QPoint q((drawPos->x() + 100), (drawPos->y() + 10)); QLine stateLine(p,q); painter->drawLine(stateLine);
// QGraphicsItem::paint(painter,option,widget);
}
-
Hi
Dont it need to be selectable to be moveable ?
It all seems fine
try use the DiagramTextItem from the diagram sample (its available directly in Creator)If that cant move, it means its your view or scene.
//! [0] DiagramTextItem::DiagramTextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent) { setFlag(QGraphicsItem::ItemIsMovable); // BOTH setFlag(QGraphicsItem::ItemIsSelectable); } //! [0] //! [1] QVariant DiagramTextItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == QGraphicsItem::ItemSelectedHasChanged) emit selectedChange(this); return value; } //! [1] //! [2] void DiagramTextItem::focusOutEvent(QFocusEvent *event) { setTextInteractionFlags(Qt::NoTextInteraction); emit lostFocus(this); QGraphicsTextItem::focusOutEvent(event); } //! [2] //! [5] void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { if (textInteractionFlags() == Qt::NoTextInteraction) setTextInteractionFlags(Qt::TextEditorInteraction); QGraphicsTextItem::mouseDoubleClickEvent(event); }
-
@mrjj Dont it need to be selectable to be moveable ?
No, Only setting it as moveable also works. I have standard QGraphicsRectItem and QGraphicsEllipseItem also in the same scene which are moving. So I don't think its the view or the scene.
:'(
-
@faiszalkhan
Ok, there is something i dont see then. :)
if you provide both StateItem.h and cpp
i can try run it and see. -
Hi,
Comment your overriden paint method for StateItem and check whether it is working.
Also please tell how you are updating the variable "drawPos".
Regards,
San