why position always 0 ?
I try to get widget position. But always I get 0. I tried
widget->parentWidget().mapToGlobal( widget->pos() )
widget->mapToGlobal( widget->pos() )
But always Qt says position is 0,0. But the widget is in middle of layout. It can not be 0.
How can I get real widget position ?
Well, the short answer is because your widget is into layout. Just try to make form with two buttons, and get its positions, than organize them into layout, and try again get positions. In first case you will get some numbers, in second case 0,0.
Thanks Harb. So how can I get position of widget in layout. Is there a way for it ?
Well, this is a question : ). The only way, that I know is just to calculate it manually, by adding spacing, margins, and other widgets width/height. It is quite tedious and very ugly. But sorry, for me this Layout system is also still a black box, which works in very a peculiar and non-obvious magic way, so I can't offer anything better : ).
OK @Harb. Thank you again
Well, the short answer is because your widget is into layout
That's incorrect. Layouts have nothing to do with how
You should just use
pos(). It returns position relative to the parent widget. It will do exactly the same no matter if there's a layout or not.
One of the situations
pos()returns 0 is when you haven't shown your widget yet (e.g. in the constructor). The geometries of children are calculated when the widget is first shown, or when you explicitly call
setGeometryor similar first..
Can you share your ui setup code?
Hi! Yes, you are right, tnx for your remark!. I was sure about that, because I have made thous two experiments, which I described above. Interesting thing that without layout returning value was correct, this made be sure about my assumption. I moved code from constructor to separate method, and now no it shows corrects values for both cases. So, as I understand, the problem was that when u just placing widgets onto parent widget, no code is running, the geometry of widgets stays permanent. But when widgets are managed by layout, there geometry calculated each time, when any resize event happens. In my particular case it was show() method.
There's no need for assumptions ;) It's all in the code and pretty simple.
When you place a widget without a layout in the designer the uic generates a code that places it where you designed it by calling
setGeometry. You can confirm that by inspecting the generated
ui_yourclass.hfile. This code is then (usually) called at the top of the constructor with
ui->setupUi. Since it calls
setGeometryyou can then read it back later in the constructor and it's what you set in the designer (obviously).
If you place a widget in a layout no such code is generated. The
ui->setupUicall simply puts the widget in a layout and that's it. It doesn't touch its geometry. So in the constructor after the call to
ui->setupUithe layout didn't trigger yet, as the widget is not yet shown. So since nothing touched its geometry yet it will have the default (0,0) position and size. When the widget is shown or resized the layout triggers the calculation of its geometry and sets it via
Really straightforward and no trickery whatsoever.
@Chris-Kawa Tnx! Now everything is clear.