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


  • Qt Champions 2016



  • 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


  • Qt Champions 2016

    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.


  • Qt Champions 2016

    Hi
    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)
            link->trackNodes();
    }
    

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

    Node::Node()
    {
        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.