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...

  • Lifetime Qt Champion

  • I am referring to a different one. It looks like (showing also the malfunction).

    Diagram Application

    I believe that I downloaded the code from http://www.informit.com/store/c-plus-plus-gui-programming-with-qt4-9780132354165

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

    Had a quick look at the old sample
    Seems it never tell when node is updated in

    QVariant 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)

    and it only gives
    Node::itemChange: ItemSelectedHasChanged
    Node::itemChange: ItemSelectedChange

    So 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 ;)

        myTextColor = Qt::darkGreen;
        myOutlineColor = Qt::darkBlue;
        myBackgroundColor = Qt::white;
        setFlags(ItemIsMovable | ItemIsSelectable);
        setFlag( QGraphicsItem::ItemSendsScenePositionChanges, true ); <<<< needed 

  • Great! That extra flag indeed solves it :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.