Qt4 Book Example Malfunctions in Qt5
-
I have taken the Diagram example from chapter 4 of the Qt4 book on C++ GUI programming with Qt4 and got it to compile and run with Qt5.7 using Qt Creator 4.1.0. The example code suggests that the links between the nodes should be updated whenever the nodes are repositioned on the canvas. Unfortunately, this does not happen when I try this. Has anybody else tried this example? I am eager to know how to make it work...
-
Hi
That sounds like this example
http://doc.qt.io/qt-5/qtwidgets-graphicsview-diagramscene-example.html -
I am referring to a different one. It looks like (showing also the malfunction).
I believe that I downloaded the code from http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165
-
Yes, it looks like 99% the same example :)
Except the newer sample using different shapes than just nodes and
the arrows moves with the shapes. -
Hi
Had a quick look at the old sample
Seems it never tell when node is updated inQVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
qDebug() << "Node::itemChange:" << change;if (change == ItemPositionHasChanged) { qDebug() << "Node::itemChange: Pos Changed"; <<<<<<<<<<<<< this code is never run foreach (Link *link, myLinks) link->trackNodes(); }
and it only gives
Node::itemChange: ItemSelectedHasChanged
Node::itemChange: ItemSelectedChangeSo its never detected its move and links not adjusted via trackNodes
So it turns out the sample needs
setFlag( QGraphicsItem::ItemSendsScenePositionChanges, true );in Node constructor and then it works ;)
Node::Node() { myTextColor = Qt::darkGreen; myOutlineColor = Qt::darkBlue; myBackgroundColor = Qt::white; setFlags(ItemIsMovable | ItemIsSelectable); setFlag( QGraphicsItem::ItemSendsScenePositionChanges, true ); <<<< needed }