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. Layout alignment with fixed spacers is inconsistent (Qt 5.15)
QtWS25 Last Chance

Layout alignment with fixed spacers is inconsistent (Qt 5.15)

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 658 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.
  • R Offline
    R Offline
    rdowell
    wrote on last edited by rdowell
    #1

    When using fixed spacers in a layout, alignment is inconsistent. It seems that both in the Preview mode in Qt designer, and in the running application, fixed spacers actually just get converted into a flat space value between the elements on either side, which doesn't take into account the base spacing of the layout itself.

    Example: I have a horizontal layout with 2 QPushButtons, and I want one of them centered and the other on the far right. I have set the layout spacing to 20px and the following styleSheet to fix button sizes to exactly 100px to demonstrate the issue:

    QPushButton{
      width:100px; height:100px;
      min-width:100px; min-height:100px;
      max-width:100px; max-height:100px;
      border:none;
      background-color:red
    }
    
    • In Qt Designer, the layouts are aligned the same regardless of whether I use a fixed-size 100px spacer, or a 3rd QPushButton (which I would later set to disabled and make invisible to achieve the same effect)
      abf850c3-2c08-4d02-97ae-5665d86872e6-image.png

      However, when I go to Form->Preview and view the layout as it would appear at runtime, the center buttons are no longer aligned. This also happens in the running application:
      50f8e19f-5abb-458b-a26e-4a0ad06f9f45-image.png

    • If I change the fixed spacer size to 120px (100px button size + 20px layout spacing) then the alignment looks off in Designer, but correct in the preview:
      8c45b7dc-9c7a-4825-b989-ab29ac65b1b7-image.png
      01a88123-6d2c-47ed-b509-ee589a95f41c-image.png

    • The same happens if the spacers are not at the margins of the layout, for example if I wanted one button on far left, one on far right, one dead center, and one right of center:
      f844da2f-cdea-45b7-8f07-610350b9f67c-image.png
      1770e1fe-6718-4035-8f95-ff498e8b5e54-image.png

    I'm aware that the first scenario can also be solved by setting the layout margin to button size + layout spacing, but the 2nd scenario where the spacers are in the middle of the layout is the bigger issue, as we have a number of places in our application where a centered button is desired (typically one to confirm/exit a given page) with a variable number of additional controls on either side, things like the following (if X's are buttons and O's are spacers to achieve desired alignment)

    XXX------X------OOX
    XX------XXO------OX

    JoeCFDJ 1 Reply Last reply
    0
    • R rdowell

      When using fixed spacers in a layout, alignment is inconsistent. It seems that both in the Preview mode in Qt designer, and in the running application, fixed spacers actually just get converted into a flat space value between the elements on either side, which doesn't take into account the base spacing of the layout itself.

      Example: I have a horizontal layout with 2 QPushButtons, and I want one of them centered and the other on the far right. I have set the layout spacing to 20px and the following styleSheet to fix button sizes to exactly 100px to demonstrate the issue:

      QPushButton{
        width:100px; height:100px;
        min-width:100px; min-height:100px;
        max-width:100px; max-height:100px;
        border:none;
        background-color:red
      }
      
      • In Qt Designer, the layouts are aligned the same regardless of whether I use a fixed-size 100px spacer, or a 3rd QPushButton (which I would later set to disabled and make invisible to achieve the same effect)
        abf850c3-2c08-4d02-97ae-5665d86872e6-image.png

        However, when I go to Form->Preview and view the layout as it would appear at runtime, the center buttons are no longer aligned. This also happens in the running application:
        50f8e19f-5abb-458b-a26e-4a0ad06f9f45-image.png

      • If I change the fixed spacer size to 120px (100px button size + 20px layout spacing) then the alignment looks off in Designer, but correct in the preview:
        8c45b7dc-9c7a-4825-b989-ab29ac65b1b7-image.png
        01a88123-6d2c-47ed-b509-ee589a95f41c-image.png

      • The same happens if the spacers are not at the margins of the layout, for example if I wanted one button on far left, one on far right, one dead center, and one right of center:
        f844da2f-cdea-45b7-8f07-610350b9f67c-image.png
        1770e1fe-6718-4035-8f95-ff498e8b5e54-image.png

      I'm aware that the first scenario can also be solved by setting the layout margin to button size + layout spacing, but the 2nd scenario where the spacers are in the middle of the layout is the bigger issue, as we have a number of places in our application where a centered button is desired (typically one to confirm/exit a given page) with a variable number of additional controls on either side, things like the following (if X's are buttons and O's are spacers to achieve desired alignment)

      XXX------X------OOX
      XX------XXO------OX

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @rdowell

      if you need
      XXX-----X------X
      you group XXX to another widget and align this widget to the left. Align the other two to center and right respectively without using spacer.
      -------X-------X
      Align these two to center and right respectively without using spacer.

      R 1 Reply Last reply
      1
      • JoeCFDJ JoeCFD

        @rdowell

        if you need
        XXX-----X------X
        you group XXX to another widget and align this widget to the left. Align the other two to center and right respectively without using spacer.
        -------X-------X
        Align these two to center and right respectively without using spacer.

        R Offline
        R Offline
        rdowell
        wrote on last edited by rdowell
        #3

        @JoeCFD Thank you, that is actually the workaround I've already been looking at, I just would prefer not having to divide a widget into multiple layouts when it realistically could be a single layout if the spacing was predictable/consistent.

        The first example ([XXX]-----X------X with the first group combined into a sub-widget, and using layoutAlignment set to left/center/right, with no spacers) does work properly in the preview but it's inconsistent in Designer itself (the 'centered' widget seems to want to anchor to one side or the other in Designer, or center between the two widgets instead of the center of the layout) so it goes back to the lack of consistency between the two.

        Unfortunately, the second example you gave (i.e. the -------X---------X with layoutAlignment set to Center/Right respectively and no spacers) doesn't seem to work. Without a 3rd widget in the layout, I get this in Designer ('centered' element actually centered between right element and left margin):
        739dadce-ba4f-4863-a5b8-8a71625d800c-image.png
        And this in the Preview ('centered' element somewhere seemingly arbitrary):
        311e936e-9256-4869-8f37-b78fd06ada53-image.png

        JoeCFDJ 1 Reply Last reply
        0
        • R rdowell

          @JoeCFD Thank you, that is actually the workaround I've already been looking at, I just would prefer not having to divide a widget into multiple layouts when it realistically could be a single layout if the spacing was predictable/consistent.

          The first example ([XXX]-----X------X with the first group combined into a sub-widget, and using layoutAlignment set to left/center/right, with no spacers) does work properly in the preview but it's inconsistent in Designer itself (the 'centered' widget seems to want to anchor to one side or the other in Designer, or center between the two widgets instead of the center of the layout) so it goes back to the lack of consistency between the two.

          Unfortunately, the second example you gave (i.e. the -------X---------X with layoutAlignment set to Center/Right respectively and no spacers) doesn't seem to work. Without a 3rd widget in the layout, I get this in Designer ('centered' element actually centered between right element and left margin):
          739dadce-ba4f-4863-a5b8-8a71625d800c-image.png
          And this in the Preview ('centered' element somewhere seemingly arbitrary):
          311e936e-9256-4869-8f37-b78fd06ada53-image.png

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @rdowell I do not use designer to lay out widgets. Can you try to add two spacers to the left and right of the center button?

          JoeCFDJ 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @rdowell I do not use designer to lay out widgets. Can you try to add two spacers to the left and right of the center button?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #5

            @JoeCFD Just tested it. You can add an empty widget with the same size of your button and align it to the left of the layout. No spacers are needed. And other two buttons are aligned to the center and right.

            R 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @JoeCFD Just tested it. You can add an empty widget with the same size of your button and align it to the left of the layout. No spacers are needed. And other two buttons are aligned to the center and right.

              R Offline
              R Offline
              rdowell
              wrote on last edited by rdowell
              #6

              @JoeCFD You are correct and that's probably what I'll end up doing, but that goes back to the original issue - needing to add empty widgets to fix alignment when spacers should ideally be able to perform that function while keeping everything inside a single widget/layout

              JoeCFDJ 1 Reply Last reply
              0
              • R rdowell

                @JoeCFD You are correct and that's probably what I'll end up doing, but that goes back to the original issue - needing to add empty widgets to fix alignment when spacers should ideally be able to perform that function while keeping everything inside a single widget/layout

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @rdowell does not cost anything. Simply go for it.

                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