QML Anchors Strokes Tutorial _qt

Source: Internet
Author: User
QT documentation QT 5.10 qt Quick positioning with anchors positioning with anchors

In addition to the most traditional Grid, Row, and Column, Qt Quick also provides a way to layout items using the concept of anchors. Each item can is thought of as has a set of 7 invisible "anchor lines": Left, Horizontalcenter, right, top, Verticalcen ter, Baseline, and bottom.

The baseline (not pictured above) corresponds to the imaginary line on which text would sit. For items with no text it's the same as top.

The Qt Quick anchoring system allows you to define relationships between the anchor lines of different items. For example, can write:

Rectangle {id:rect1;}
Rectangle {id:rect2; anchors.left:rect1.right;}

In this case, the left edge of the rect2 was bound to the right edge of rect1, producing the following:

You can specify multiple anchors. For example:

Rectangle {id:rect1;}
Rectangle {id:rect2; anchors.left:rect1.right; anchors.top:rect1.bottom;}

By specifying multiple horizontal or vertical anchors your can control the size of a item. Below, Rect2 is anchored to the right of Rect1 and the left of RECT3. If either of the blue rectangles are moved, rect2 'll stretch and shrink as necessary:

Rectangle {id:rect1; x:0;}
Rectangle {id:rect2; anchors.left:rect1.right; anchors.right:rect3.left;}
Rectangle {id:rect3; x:150;}

There are also some convenience. Anchors is a anchors.fill this is the convenience as same and bottom anchors to the left,right,top and bottom of the target item. Anchors.centerin is another convenience anchor, and is the same as setting the Verticalcenter and Horizontalcenter To the Verticalcenter and horizontalcenter of the target item. Anchor margins and offsets

The anchoring system also allows margins and offsets to is specified for an item ' s anchors. Margins specify the amount of empty spaces to leave to the outside of a item ' s anchor, while offsets allow positioning to Be manipulated using the center anchor lines. An item can specify it anchor margins individually through LeftMargin, RightMargin, TopMargin and BottomMargin, or use a Chors.margins to specify the same margin value for all four edges. Anchor offsets are specified using Horizontalcenteroffset, Verticalcenteroffset and BaselineOffset.

The following example specifies a left margin:

Rectangle {id:rect1;}
Rectangle {id:rect2; anchors.left:rect1.right; anchors.leftmargin:5;}

In this case, a margin of 5 pixels are reserved to the left of RECT2, producing the following:

Note:anchor margins only apply to anchors; They are not a generic means of applying margins to a Item. If an anchor margin are specified for a edge but the item is isn't anchored to any item on this edge, the margin is not APPL Ied. changing anchors

Qt Quick provides the anchorchanges type for specifying the anchors in a state.

State {
    Name: "Anchorright"
    anchorchanges {
        target:rect2
        anchors.right:parent.right
        anchors.left:undefined  //remove the left anchor
    }
}

Anchorchanges can be animated using the Anchoranimation type.

Transition {
    anchoranimation {}  //animates any anchorchanges in the corresponding state change
}

Anchors can also be changed imperatively within JavaScript. However, these changes should are carefully ordered, or they may produce unexpected. The following example illustrates the issue:

Bad code
Rectangle {
    width:50
    anchors.left:parent.left

    function reanchortoright () {
        Anchors.right = parent.right
        anchors.left = undefined
    }
}

When Reanchortoright is called, the function-the right anchor. At "Point," both left and right anchors are set, and the item would be stretched horizontally to fill its parent. When the left anchor is unset, the new width would remain. Thus when updating anchors within JavaScript, you should a unset the any anchors of that are no longer required N Set any new anchors that are required, as shown below:

Rectangle {
    width:50
    anchors.left:parent.left

    function reanchortoright () {
        anchors.left = undefined
        anchors.right = Parent.right
    }
}

Because the evaluation order of bindings is not defined, it isn't recommended to change anchors via conditional bindings, As this can leads to the ordering issue described above. In the following example the Rectangle would eventually grow to the full width of it parent, because both left and right a Nchors'll be simultaneously set during binding update.

Bad code
Rectangle {
    width:50; height:50
    anchors.left:state = = "right"? Undefined:parent.left;
    Anchors.right:state = "Right"? parent.right:undefined;
}

This should is rewritten to use Anchorchanges instead, as anchorchanges would automatically handle ordering-issues internal Ly. Restrictions

For performance reasons, your can only anchor a item to its siblings and direct parent. For example, the following anchor is invalid and would produce a warning:

Bad code
Item {
    id:group1
    Rectangle {id:rect1;.}
}
Item {
    id:group2
    Rectangle {id:rect2; anchors.left:rect1.right;}    Invalid anchor!
}

Also, anchor-based layouts cannot is mixed with absolute positioning. If an item specifies its x position and also sets anchors.left, or anchors it left and right edges but add Itionally sets A width, the result are undefined, as it would not being clear whether the item should use anchoring or AB Solute positioning. The same can is said for setting an item ' s y and height with anchors.top and  Anchors.bottom, or Setting anchors.fillas the as width or height. The same applies when using positioners such as Row and Grid, which may set the item ' S x and y propert ies. If you are wish to change from the using anchor-based to absolute positioning, you can clear a anchor value by setting it to  ; undefined.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.