Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I keep the middle point of a widget always aligned vertically with that of its parent widget?
Forum Updated to NodeBB v4.3 + New Features

How do I keep the middle point of a widget always aligned vertically with that of its parent widget?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 474 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by Chris Kawa
    #1

    Hello there, I'm confused about the "size" of the Qt widgets:

    1. 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:
    alt text

    (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?

    jsulmJ 1 Reply Last reply
    0
    • ? A Former User

      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?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #4

      @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.

      ? 1 Reply Last reply
      2
      • ? A Former User

        Hello there, I'm confused about the "size" of the Qt widgets:

        1. 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:
        alt text

        (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?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        ? 2 Replies Last reply
        3
        • jsulmJ jsulm

          @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?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #3

          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?

          Chris KawaC 1 Reply Last reply
          0
          • ? A Former User

            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?

            Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #4

            @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.

            ? 1 Reply Last reply
            2
            • jsulmJ jsulm

              @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?

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #5

              @jsulm I tried your advice using layouts, and it worked as I wanted. Thanks.

              1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                @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.

                ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #6

                @Chris-Kawa I followed your advice and implemented both resizeEvent and showEvent. It worked as you said. Thanks for helping me understand Qt more.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved