Is there a direct function that would allow "locking" a widgets' positions to another widget?
-
Hi. I have two QWidgets, one a QFrame built to be a vertical bar, the other a QLabel, as shown here.

I was wondering whether there was a way to directly "lock" the positions of the two widgets together, such that the QLabel would move with the QFrame when the QFrame is repositioned, without having to also manually reposition the QLabel with a move function. Parenting the QLabel to the QFrame just puts the QLabel within the QFrame, which basically hides the QFrame.
Please let me know if more information is required, I hope I have explained my issue adequately.
-
Hi. I have two QWidgets, one a QFrame built to be a vertical bar, the other a QLabel, as shown here.

I was wondering whether there was a way to directly "lock" the positions of the two widgets together, such that the QLabel would move with the QFrame when the QFrame is repositioned, without having to also manually reposition the QLabel with a move function. Parenting the QLabel to the QFrame just puts the QLabel within the QFrame, which basically hides the QFrame.
Please let me know if more information is required, I hope I have explained my issue adequately.
@Dummie1138
So far as I know, the easiest way to do this is to define aQWidgetas the parent and put theQFrameand theQLabelon it. Then you move the parentQWidgetand it takes its children with it. -
Hi. I have two QWidgets, one a QFrame built to be a vertical bar, the other a QLabel, as shown here.

I was wondering whether there was a way to directly "lock" the positions of the two widgets together, such that the QLabel would move with the QFrame when the QFrame is repositioned, without having to also manually reposition the QLabel with a move function. Parenting the QLabel to the QFrame just puts the QLabel within the QFrame, which basically hides the QFrame.
Please let me know if more information is required, I hope I have explained my issue adequately.
@Dummie1138 usually parenting works, as the position of the child is always relative to the parent.
but since that "doesn't work" let me introduce you to c++ property bindings
https://doc-snapshots.qt.io/qt6-dev/bindableproperties.htmlhell of a lot more complicated/complex but it should enable you to "lock" siblings position together without doing it manually
-
@Dummie1138 usually parenting works, as the position of the child is always relative to the parent.
but since that "doesn't work" let me introduce you to c++ property bindings
https://doc-snapshots.qt.io/qt6-dev/bindableproperties.htmlhell of a lot more complicated/complex but it should enable you to "lock" siblings position together without doing it manually
@J-Hilk
Ah, so I read thereBindable properties allow to achieve the same not only in QML code, but also in C++.
So you can utilise the QML ability to monitor changes and change things for you this way, right? In this solution you would "bind" the (x,y) coordinates of the label to those of the frame, so if the frame is moved the label moves with it? Would the frame be bound to the label, so if the label moves the frame does too, or would that create a "binding loop" problem?
-
@J-Hilk
Ah, so I read thereBindable properties allow to achieve the same not only in QML code, but also in C++.
So you can utilise the QML ability to monitor changes and change things for you this way, right? In this solution you would "bind" the (x,y) coordinates of the label to those of the frame, so if the frame is moved the label moves with it? Would the frame be bound to the label, so if the label moves the frame does too, or would that create a "binding loop" problem?
@JonB said in Is there a direct function that would allow "locking" a widgets' positions to another widget?:
or would that create a "binding loop" problem?
it would! You should only bind one to an other, not both to each other :D