PDA

View Full Version : QGraphicsItemChange not working for ItemPositionChange



Narada
4th April 2016, 04:18
I am trying out an example from
http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165

Chap08/diagram.pro

The program compiles, but does not work as advertised. When you move the nodes, the links are supposed to move with the nodes. I used the debugger and found out that it never hits the if condition in


QVariant Node::itemChange(GraphicsItemChange change,
const QVariant &value)
{
if (change == ItemPositionChange) {
foreach (Link *link, myLinks)
link->trackNodes();
}
return QGraphicsItem::itemChange(change, value);
}


If I take the if condition out, I sort of get to move the links, but after clicking on the graphics scene.

Any insight would be great into what the problem is.

d_stranz
4th April 2016, 16:46
Have you set the QGraphicsItem::ItemSendsGeometryChanges and maybe QGraphicsItem::ItemSendsScenePositionChanges flags on your Node instances? These flags are both off by default, which means you will not get itemChange events for ItemPositionChange and other transform changes.

Narada
5th April 2016, 04:46
That was it. Didn't know there was such a flag. Thank you very much.

anda_skoa
5th April 2016, 09:18
That was it. Didn't know there was such a flag. Thank you very much.
The example in the book had code for the position change handling but not for setting the flags?

Cheers,
_

Narada
11th April 2016, 15:57
That's correct. However, at the time the book was written the flag was set automatically. Later Qt changed the default values ( around 4.7).