How do I keep the middle point of a widget always aligned vertically with that of its parent widget?
-
Hello there, I'm confused about the "size" of the Qt widgets:
- I have a subclass of QWidget which is a child of another subclass of QWidget. I want the two subclasses to always align their middle points vertically. I wrote some thing like this:
In the constructor of the parent widget:
{ ... w = new Widget(this); w->move(0, (height()-w->height())/2.0); qDebug() << "Size when initialization: " << size(); }And in the overridden resizeEvent() method of the parent widget:
resizeEvent(QResizeEvent *e) { QOpenGLWidget::resizeEvent(e); w->move(0, (height()-w->height())/2.0); qDebug() << "Size when resizeEvent happens: " << size(); }I ended up with:

(I used the example "hellogl2" as a quick demo)
Why didn't the child widget align its middle point with that of the parent widget vertically in the beginning?Anything I can do to achieve it?
- I have a subclass of QWidget which is a child of another subclass of QWidget. I want the two subclasses to always align their middle points vertically. I wrote some thing like this:
-
Hello @jsulm , thanks for your insight into the initialization of widgets. I didn't know that before. Thanks for the enlightment.
To answer your question: I'm working on getting a "view controller" with some buttons to be "on the top" of a 3D display widget (like the "GLWidget" class in the example of "hellogl2"). I figured that it might be easier to just get the "view controller" widget as child of GLWidget and set its position through "move(int x, int y)" method.Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place untill I dragged the parent widget "horizontally". Why didn't the child widget move to the right place during the first call of resizeEvent? Any hint about this?
@YTRoy You should override showEvent of your overlay widget the same way you have the resize event. As @jsulm mentioned this is the first time a widget gets correct dimensions.
Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place until I dragged the parent widget
You've implemented resizeEvent of the parent. The first time parent gets a resize event the child might not yet be shown and thus doesn't have correct dimensions.
-
Hello there, I'm confused about the "size" of the Qt widgets:
- I have a subclass of QWidget which is a child of another subclass of QWidget. I want the two subclasses to always align their middle points vertically. I wrote some thing like this:
In the constructor of the parent widget:
{ ... w = new Widget(this); w->move(0, (height()-w->height())/2.0); qDebug() << "Size when initialization: " << size(); }And in the overridden resizeEvent() method of the parent widget:
resizeEvent(QResizeEvent *e) { QOpenGLWidget::resizeEvent(e); w->move(0, (height()-w->height())/2.0); qDebug() << "Size when resizeEvent happens: " << size(); }I ended up with:

(I used the example "hellogl2" as a quick demo)
Why didn't the child widget align its middle point with that of the parent widget vertically in the beginning?Anything I can do to achieve it?
@YTRoy said in How do I keep the middle point of a widget always aligned vertically with that of its parent widget?:
In the constructor of the parent widget
In the constructor of a widget its dimensions are NOT yet known (they are set as soon as the widget is shown), so it can't work.
Why don't you use layouts to align widgets? - I have a subclass of QWidget which is a child of another subclass of QWidget. I want the two subclasses to always align their middle points vertically. I wrote some thing like this:
-
@YTRoy said in How do I keep the middle point of a widget always aligned vertically with that of its parent widget?:
In the constructor of the parent widget
In the constructor of a widget its dimensions are NOT yet known (they are set as soon as the widget is shown), so it can't work.
Why don't you use layouts to align widgets?Hello @jsulm , thanks for your insight into the initialization of widgets. I didn't know that before. Thanks for the enlightment.
To answer your question: I'm working on getting a "view controller" with some buttons to be "on the top" of a 3D display widget (like the "GLWidget" class in the example of "hellogl2"). I figured that it might be easier to just get the "view controller" widget as child of GLWidget and set its position through "move(int x, int y)" method.Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place untill I dragged the parent widget "horizontally". Why didn't the child widget move to the right place during the first call of resizeEvent? Any hint about this?
-
Hello @jsulm , thanks for your insight into the initialization of widgets. I didn't know that before. Thanks for the enlightment.
To answer your question: I'm working on getting a "view controller" with some buttons to be "on the top" of a 3D display widget (like the "GLWidget" class in the example of "hellogl2"). I figured that it might be easier to just get the "view controller" widget as child of GLWidget and set its position through "move(int x, int y)" method.Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place untill I dragged the parent widget "horizontally". Why didn't the child widget move to the right place during the first call of resizeEvent? Any hint about this?
@YTRoy You should override showEvent of your overlay widget the same way you have the resize event. As @jsulm mentioned this is the first time a widget gets correct dimensions.
Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place until I dragged the parent widget
You've implemented resizeEvent of the parent. The first time parent gets a resize event the child might not yet be shown and thus doesn't have correct dimensions.
-
@YTRoy said in How do I keep the middle point of a widget always aligned vertically with that of its parent widget?:
In the constructor of the parent widget
In the constructor of a widget its dimensions are NOT yet known (they are set as soon as the widget is shown), so it can't work.
Why don't you use layouts to align widgets?@jsulm I tried your advice using layouts, and it worked as I wanted. Thanks.
-
@YTRoy You should override showEvent of your overlay widget the same way you have the resize event. As @jsulm mentioned this is the first time a widget gets correct dimensions.
Also, I found that when initializing the parent widget, the resizeEvent would also be called once, but the child widget still didn't move to the right place until I dragged the parent widget
You've implemented resizeEvent of the parent. The first time parent gets a resize event the child might not yet be shown and thus doesn't have correct dimensions.
@Chris-Kawa I followed your advice and implemented both resizeEvent and showEvent. It worked as you said. Thanks for helping me understand Qt more.