Setting and clearing anchors from javascript



  • I am trying to figure out how to set and clear the anchors of components from javascript.
    I am doing this because I need to clear anchors when a component is pressed to allow dragging, and then reset the anchor to it's previous value after it is released. Could someone help me with this issue please?

    P.S. If anyone wonders why I need to do this, I am creating a transform control similar to the one in photoshop that allows you to scale, rotate, and move layers.



  • Hi,

    Setting an anchor to undefined will clear it.

    You might also want to play around with using the "AnchorChanges":http://doc.qt.nokia.com/4.7-snapshot/qml-anchorchanges.html element, as it will automatically handle restoring anchors for you.

    Regards,
    Michael



  • I hadn't come across the AnchorChanges element that seems like a better approach. I did, however, see the setting of the anchor to undefined, but how do I set the anchor to this value within a javascript file? undefined is not defined in js, no pun intended :).



  • Hi,

    undefined is actually a property of the global object (see e.g. http://www.diveintojavascript.com/core-javascript-reference/the-undefined-property), so you should be able to do the following, for example:

    @obj.anchors.left = undefined@

    (where obj is the Item you are removing the left anchor from).

    Regards,
    Michael



  • Oh ok I understand now. undefined is always available in javascript in the global scope. I seem to remember reading somewhere that you could not access variable in the global scope from js, but I guess that this is not true for the undefined variable. Thanks for your help, I got working!


Log in to reply
 

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