15

Column documentation explains how to achieve this:

enter image description here

However, I would like to achieve this:

enter image description here

That is, all items inside the Column should be horizontally aligned to the center. anchors.horizontalCenter: parent.Center does not work. How can I obtain the desired result?

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
avb
  • 1,701
  • 5
  • 22
  • 37
  • I'm a bit confused, the answer was accepted and now the post is edited that it does not work. So does it or does it not? – Rudolfs Bundulis Jun 17 '15 at 09:26
  • I've had already tried `anchors.horizontalCenter: parent.Center` even before I posted. But that was wrong. What you wrote was right: `anchors.horizontalCenter: parent.horizontalCenter`. The difference is to use `parent.horizontalCenter` and not `parent.Center`. – avb Jun 17 '15 at 09:30
  • yeah, after posting the comment I read it the second time and understood the difference, damn I must pay more attention to reading:D Sorry for the pointless comment:) – Rudolfs Bundulis Jun 17 '15 at 09:31
  • No problem. Thank YOU for your help :) – avb Jun 17 '15 at 09:32

3 Answers3

20

Try to put this line to every child:

anchors.horizontalCenter: parent.horizontalCenter
fat
  • 6,435
  • 5
  • 44
  • 70
12

I suppose you can use anchors.horizontalCenter for all the child items to align them with the horizontalCenter of the column given that the column has an id you can refer to.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • You should not used anchors inside layouts: "Detected anchors on an item that is managed by a layout." – Patrick José Pereira Apr 29 '19 at 13:08
  • 3
    `Column` is not a layout. – MarPan Jun 25 '19 at 12:00
  • 1
    The [Detailed Description of Column](https://doc.qt.io/qt-5/qml-qtquick-column.html#details) is quite specific about what properties of `anchors` shall not be used. `horizontalCenter` is not mentioned. So I think the comments by @patrick-josé-pereira @marpan are obsolete. – marsl Aug 05 '20 at 09:31
4

If it's a matter of laying out your items, also take a look at the ColumnLayout component and the attached property Layout.alignment (you can set it as Qt.AlignHCenter).

I find layouts far easier to use.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • The downside of layouts is that they don't have transitions eg for move. Or at least I don't know how to apply them. – avb Jun 21 '15 at 08:29
  • If you want to move items around, it's not only a matter of laying out them. :-) – skypjack Jun 21 '15 at 08:31
  • I did it this way as the doc said, but Qt sets the left side of the items in the center position, instead of centering the whole item with equal margins left and right. Am I doing something wrong? – Savvas Parastatidis Aug 29 '16 at 19:41
  • @Savvas, the `Layout` is an [attached object](http://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html) that can be used in children of the `ColumnLayout`. You can set the attached property `Layout.alignment` in each child that you want to center, as you would have set `anchors.horizontalCenter` in each child of a `Column`. – SR_ May 25 '18 at 08:39