How to reset anchors?



  • I read in http://doc.qt.nokia.com/4.7-snapshot/qml-mousearea.html#drag.active-prop that in order to drag an object which is anchored to its parent, we can undefined the anchors in the onPressed signal. I tried it but I have the compile error

    @
    Expected token `;'
    anchors.bottom: undefined
    @

    Below is my code, icon is under a rectangle
    @
    Image {
    id: icon
    width: 64
    height: 64
    source: "icon.gif"
    anchors.bottom: parent.bottom

           MouseArea {
               id: iconMouseArea
               anchors.fill: parent
               width: 64
               height: 64
    
               drag.target: parent
               drag.axis: Drag.XandYAxis
    
               onPressed: {
                   anchors.bottom: undefined
               }
    
           }
       }
    

    @

    Can someone help me out? Thanks!



  • I think you need to explicitly state which item you are changing anchors on with something like this in a state.

    @
    AnchorChanges {
    target: icon
    anchors.bottom: undefined
    }
    @

    You onPressed: handler can then simply change to that state. Does that work for you?



  • Thank you very much for your help ZapB, I implemented a state and use AnchorChanges, it works now.

    Is it possible to do anchor changes in c++, instead of qml?



  • Hmmm, good question. I'm not sure. It's not something I have ever tried. In theory I would think yes as the anchors are just properties like everything else. Have a play around with it and see if it is possible. I would be interested to hear the outcome of your experiments.



  • I tried the following slot, which is called when I move an object (e.g. icon) into a slide (e.g. rectangle). Once the object is in the slide, I want to set the object as a child of the slide, so that when I drag the slide, the object will move with it.

    In this function, both slide and object are cast as QGraphicsWidgets and then I try to use QGraphicsAnchorLayout to anchor the object inside slide, but it does not work.

    @
    void cc::putObjectIntoSlide(QGraphicsObject * slide, QGraphicsObject * object)
    {
    if (object != NULL)
    {
    qDebug("got object in slide");
    object->setParent(slide);
    QGraphicsWidget *a = new QGraphicsWidget(slide);
    QGraphicsWidget *b = new QGraphicsWidget(object);
    QGraphicsAnchorLayout *la = new QGraphicsAnchorLayout;
    QGraphicsAnchorLayout *lb = new QGraphicsAnchorLayout;
    a->setLayout(la);
    b->setLayout(lb);
    la->addAnchor(la, Qt::AnchorBottom, lb, Qt::AnchorBottom);
    }
    else
    {
    qDebug("no object");
    }
    }
    @



  • QGraphicsAnchorLayout has nothing to do with anchors in QML. I'm not sure if the QML anchors are exported in any useable way in C++ other than perhaps by way of the setProperty() and property() functions of QObject.



  • Ok, I tried using setProperty as follows, but it doesn't work

    @
    object->setProperty("anchors.bottom","parent.bottom");
    @



  • I'd think that anchors is actually an object (or some other collection of properties) of its own, so I am not surprised that that doesn't work. No idea how it would work though.



  • If I drag an object A into object B, and make object A a child of object B, how do I anchor A to B?


Log in to reply
 

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