1
votes

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?

2
change nothing to undefinedeyllanesc
@eyllanesc: You're kidding me, right, it's that simple? :-)paxdiablo
Did it work? I have not tested iteyllanesc
@eyllanesc: it surely does work. Thanks heaps. If you'd like to post that as an answer, I'll be happy to accept, and hopefully future searchers will be helped as well.paxdiablo

2 Answers

1
votes

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

0
votes

You can use the States mechanism and use AnchorChanges, see https://doc.qt.io/archives/qt-4.8/qml-anchorchanges.html for a good example