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. Centering in a layout

Centering in a layout

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 8.4k Views
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    I can get it close to that
    alt text

    using layoutStrech 6,1

    But text does move a little but scales ok

    alt text

    JonBJ 1 Reply Last reply
    1
    • T Offline
      T Offline
      Tink
      wrote on last edited by
      #3

      How about this: a spacer on the left side with sizehint preferred and the same width as the checkbox. And the label's sizepolicy expanding.

      JonBJ 1 Reply Last reply
      0
      • T Tink

        How about this: a spacer on the left side with sizehint preferred and the same width as the checkbox. And the label's sizepolicy expanding.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #4

        @Tink said in Centering in a layout:

        and the same width as the checkbox

        That's a width. I don't know what the width might or might not be.

        T 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Hi,

          Aren't you looking for QFormLayout ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          JonBJ 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            I can get it close to that
            alt text

            using layoutStrech 6,1

            But text does move a little but scales ok

            alt text

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #6

            @mrjj
            Thanks, I will try this tomorrow.

            But we both know you are achieving this via layoutStretch 6,1, so you're not really doing the right thing, are you? :) I'll try putting a very large integer on the left ;-)`

            mrjjM 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Aren't you looking for QFormLayout ?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #7

              @SGaist said in Centering in a layout:

              Aren't you looking for QFormLayout ?

              No, this really isn't a form. The two widgets have nothing to do with each other. I might just as well come back and ask for, say, a third widget on the left as well, and then the principle of two-column QForm wouldn't apply.

              1 Reply Last reply
              0
              • JonBJ JonB

                @mrjj
                Thanks, I will try this tomorrow.

                But we both know you are achieving this via layoutStretch 6,1, so you're not really doing the right thing, are you? :) I'll try putting a very large integer on the left ;-)`

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @JonB
                Hi
                Its not an absolute value. its relative. says 6/7 and 1/7 sharing of the space
                so it keeps the aspect when scaled but visually i was nto 100% satisfied with the text :)

                JonBJ 1 Reply Last reply
                0
                • mrjjM mrjj

                  @JonB
                  Hi
                  Its not an absolute value. its relative. says 6/7 and 1/7 sharing of the space
                  so it keeps the aspect when scaled but visually i was nto 100% satisfied with the text :)

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @mrjj
                  I know. If you think out the math/logic, this approach works best as the left-hand ratio tends to infinity (right-hand one stays at 1), because we're really try to center the first item across the whole width. I think/worry about these things... ;-)

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by Bonnie
                    #10

                    I would recommend using QGridLayout because you actually can add several widgets to the same grid.
                    But this cannot be done in the normal way, you will need to edit the ui file as plain text.

                    1. Add QLabel and QCheckBox to the QGridLayout
                    2. Save & Close Designer
                    3. Open the ui file with Plain Text Editor
                    4. Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.
                    5. Save, and reopen the ui file with Designer
                    6. Set the alignment of QLabel to Center Horizontally and QCheckBox to Right
                    JonBJ 1 Reply Last reply
                    1
                    • B Bonnie

                      I would recommend using QGridLayout because you actually can add several widgets to the same grid.
                      But this cannot be done in the normal way, you will need to edit the ui file as plain text.

                      1. Add QLabel and QCheckBox to the QGridLayout
                      2. Save & Close Designer
                      3. Open the ui file with Plain Text Editor
                      4. Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.
                      5. Save, and reopen the ui file with Designer
                      6. Set the alignment of QLabel to Center Horizontally and QCheckBox to Right
                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #11

                      @Bonnie
                      I will certainly try any suggestion from you!

                      Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.

                      So you want me to put both widgets in a single cell of a (presumably) 1x1 QGridLayout, right? And then set the alignments on each widget, and hey presto that will do it? The label will get centered over the whole width of the cell, and the checkbox will get put at the right?

                      Oh.... hang on... you want them kept in separate cells, but both cells to have the same row #0, column #0? So I have two separate cells at the same place? Surely that's cheating, or is it "super-imposition"...???

                      B 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Bonnie
                        I will certainly try any suggestion from you!

                        Find the grid layout and make the QLabel and QCheckBox both at the same grid, for example, row 0 and column 0.

                        So you want me to put both widgets in a single cell of a (presumably) 1x1 QGridLayout, right? And then set the alignments on each widget, and hey presto that will do it? The label will get centered over the whole width of the cell, and the checkbox will get put at the right?

                        Oh.... hang on... you want them kept in separate cells, but both cells to have the same row #0, column #0? So I have two separate cells at the same place? Surely that's cheating, or is it "super-imposition"...???

                        B Offline
                        B Offline
                        Bonnie
                        wrote on last edited by
                        #12

                        @JonB
                        I've cheated a lot by QGridLayout :)
                        So finally the ui content will be

                        ...
                           <layout class="QGridLayout" name="gridLayout">
                                <item row="0" column="0" alignment="Qt::AlignRight">
                                 <widget class="QCheckBox" name="checkBox">
                                  <property name="text">
                                   <string>CheckBox</string>
                                  </property>
                                 </widget>
                                </item>
                                <item row="0" column="0" alignment="Qt::AlignHCenter">
                                 <widget class="QLabel" name="label">
                                  <property name="text">
                                   <string>TextLabel</string>
                                  </property>
                                 </widget>
                                </item>
                               </layout>
                        ...
                        
                        JonBJ 1 Reply Last reply
                        2
                        • B Bonnie

                          @JonB
                          I've cheated a lot by QGridLayout :)
                          So finally the ui content will be

                          ...
                             <layout class="QGridLayout" name="gridLayout">
                                  <item row="0" column="0" alignment="Qt::AlignRight">
                                   <widget class="QCheckBox" name="checkBox">
                                    <property name="text">
                                     <string>CheckBox</string>
                                    </property>
                                   </widget>
                                  </item>
                                  <item row="0" column="0" alignment="Qt::AlignHCenter">
                                   <widget class="QLabel" name="label">
                                    <property name="text">
                                     <string>TextLabel</string>
                                    </property>
                                   </widget>
                                  </item>
                                 </layout>
                          ...
                          
                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #13

                          @Bonnie
                          Yeah got it.... I don't have Qt available now, could you just tell me, what code is it producing in the ui file for that? What is the runtime call to get multiple widgets into a QGridLayout row,col 0,0 cell? Is it some "appendWidget()` or what?

                          Designer has a way of renumbering rows & columns and I wouldn't want to rely on those matching numbers remaining over time. I'll look at coding in initialization to make sure they're in the right place, once I know how we're actually doing it in code.

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            Bonnie
                            wrote on last edited by
                            #14

                            Yes, better not to add / delete cells in the designer after doing that cheating thing.
                            This is from the generated ui header:

                            gridLayout->addWidget(checkBox, 0, 0, 1, 1, Qt::AlignRight);
                            ...
                            gridLayout->addWidget(label, 0, 0, 1, 1, Qt::AlignHCenter);
                            
                            JonBJ 1 Reply Last reply
                            2
                            • B Bonnie

                              Yes, better not to add / delete cells in the designer after doing that cheating thing.
                              This is from the generated ui header:

                              gridLayout->addWidget(checkBox, 0, 0, 1, 1, Qt::AlignRight);
                              ...
                              gridLayout->addWidget(label, 0, 0, 1, 1, Qt::AlignHCenter);
                              
                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by JonB
                              #15

                              @Bonnie
                              So..... a QGridLayout let's you addWidget() multiple times at same row, column.... Now, where do the docs mention this?

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Tink said in Centering in a layout:

                                and the same width as the checkbox

                                That's a width. I don't know what the width might or might not be.

                                T Offline
                                T Offline
                                Tink
                                wrote on last edited by
                                #16

                                @JonB, ok but there is QSpacerItem::changeSize() if you want to do it with code.

                                JonBJ 1 Reply Last reply
                                2
                                • T Tink

                                  @JonB, ok but there is QSpacerItem::changeSize() if you want to do it with code.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by
                                  #17

                                  @Tink
                                  Yes indeed. That will mean I'll have to add code for initialization & resizes. I think your answer is good, but when I have @mrjj or @Bonnie offering me non-code ways I prefer to pursue those :)

                                  1 Reply Last reply
                                  1
                                  • JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by JonB
                                    #18

                                    I have had a play this morning....

                                    @Bonnie
                                    I tried out your suggestion. It seems to work, perfectly! You get the bonus points for providing what seems like the correct solution. However, I cannot rely on not altering the UI in Designer, intentionally or unintentionally, over time, and finding it messes up the two-widgets-in-one-cell :(

                                    @mrjj
                                    I have gone for a solution related to yours. But changed to be a 3-column QGridLayout (didn't seem to work right with a QHboxLayout), with layoutColumnStretch 2,6,2, so 20%-60%-20%, like:

                                    <layout class="QGridLayout" name="gridLayout_2" columnstretch="2,6,2">
                                     <item row="0" column="0">
                                      <spacer name="horizontalSpacer">
                                       <property name="orientation">
                                        <enum>Qt::Horizontal</enum>
                                       </property>
                                      </spacer>
                                     </item>
                                     <item row="0" column="1" alignment="Qt::AlignHCenter">
                                      <widget class="QLabel" name="label">
                                      </widget>
                                     </item>
                                     <item row="0" column="2" alignment="Qt::AlignRight">
                                      <widget class="QCheckBox" name="cbLockMode">
                                      </widget>
                                     </item>
                                    </layout>
                                    

                                    It may be this just works given the widths of my particular label & checkbox, but I have tried stretching the container and things seem to resize maintaining the label at approximately in the center of the whole.

                                    Thank you both, this will have to do as a reasonable compromise.

                                    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