qtqmlanchorpoint

How to anchor different things based on a property?


I desire a QML layout that looks something like this:

+--------+            +--------+
|        |            |        |
| Text 1 |            | Text 3 |
+--------+            +--------+
----^^---------vv---------^^----
           +--------+
           | Text 2 |
           |        |
           +--------+

In other words, alternate objects (a QML Item in this case) are placed above or below some central line and the things they contain (QML Text) within those objects need to go to either the top or the bottom of the object.

Now I know I can align specific things with anchors on the items:

anchors.bottom: isAbove ? parent.bottom : parent.top;

but that will set the item bottom anchor regardless of whether it's needed (such as with the second object above containig Text 2).

What I'm actually looking for is a way to set only one anchor depending on the position. In psuedo-QML, that would be:

isAbove? (anchors.bottom: parent.bottom) : (anchors.top: parent.top);

In other words, I only want to anchor the item bottom to parent bottom if it's above the line. Otherwise I want to anchor the item top to the parent top.

I thought it may be something like:

anchors.bottom: isAbove ? parent.bottom : nothing   ;
anchors.top:    isAbove ? nothing       : parent.top;

but nothing does not appear to be an anchoring option :-)

Is there a way to do this with QML?


Solution

  • If you want to remove an anchor you must use undefined as indicated by the docs