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. Widgets alignment within layout

Widgets alignment within layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 965 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.
  • L Offline
    L Offline
    lukutis222
    wrote on last edited by
    #1

    Hello. I have issues with my widgets not being properly aligned and I cannot fully figure out how it works..

    Take a look at my widget.ui
    c9edb1cf-36e8-4063-9b83-911d34a152bd-image.png

    As you can see, I have placed some widgets within vertical layout and I placed horizontal spacers hoping that they will be automatically aligned but that is not the case. When I run my application:

    With default font:

    3c2876a3-1c9e-4a13-98ee-3e736037be6e-image.png

    If I try to change font with:

        //QFont font("Courier New");
        //QApplication::setFont(font);
    

    It is even worse:

    18020ad1-bd84-40f1-8d4f-693c8fcaeb8e-image.png

    My questions:

    1. How can I ensure that comboboxes are aligned nicely?
    2. How can I manually modify the width of horizontal spacers? For example, I would like to move the spacer to the position that I have marked:
      2bc184ee-a3b2-4693-a825-d52a3e42cff7-image.png

    I cant do that by simply moving the spacer as it permamently "stuck" to the position

    jsulmJ Chris KawaC 2 Replies Last reply
    0
    • L lukutis222

      Hello. I have issues with my widgets not being properly aligned and I cannot fully figure out how it works..

      Take a look at my widget.ui
      c9edb1cf-36e8-4063-9b83-911d34a152bd-image.png

      As you can see, I have placed some widgets within vertical layout and I placed horizontal spacers hoping that they will be automatically aligned but that is not the case. When I run my application:

      With default font:

      3c2876a3-1c9e-4a13-98ee-3e736037be6e-image.png

      If I try to change font with:

          //QFont font("Courier New");
          //QApplication::setFont(font);
      

      It is even worse:

      18020ad1-bd84-40f1-8d4f-693c8fcaeb8e-image.png

      My questions:

      1. How can I ensure that comboboxes are aligned nicely?
      2. How can I manually modify the width of horizontal spacers? For example, I would like to move the spacer to the position that I have marked:
        2bc184ee-a3b2-4693-a825-d52a3e42cff7-image.png

      I cant do that by simply moving the spacer as it permamently "stuck" to the position

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

      @lukutis222 I think for your use case https://doc.qt.io/qt-6/qformlayout.html is a better choice

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

      1 Reply Last reply
      3
      • L lukutis222

        Hello. I have issues with my widgets not being properly aligned and I cannot fully figure out how it works..

        Take a look at my widget.ui
        c9edb1cf-36e8-4063-9b83-911d34a152bd-image.png

        As you can see, I have placed some widgets within vertical layout and I placed horizontal spacers hoping that they will be automatically aligned but that is not the case. When I run my application:

        With default font:

        3c2876a3-1c9e-4a13-98ee-3e736037be6e-image.png

        If I try to change font with:

            //QFont font("Courier New");
            //QApplication::setFont(font);
        

        It is even worse:

        18020ad1-bd84-40f1-8d4f-693c8fcaeb8e-image.png

        My questions:

        1. How can I ensure that comboboxes are aligned nicely?
        2. How can I manually modify the width of horizontal spacers? For example, I would like to move the spacer to the position that I have marked:
          2bc184ee-a3b2-4693-a825-d52a3e42cff7-image.png

        I cant do that by simply moving the spacer as it permamently "stuck" to the position

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

        @lukutis222 said:

        1. How can I ensure that comboboxes are aligned nicely?

        Use a layout that has columns. Like @jsulm said QFormLayout is best suited for two column label+widget layouts. In cases like yours, where you have more columns and you want a spacer at the end, a QGridLayout would do nicely.

        1. How can I manually modify the width of horizontal spacers?

        You don't, or rather shouldn't. Layouts resize elements according to their policies. The default policy for spacers is expanding, meaning the widgets in the layout take the minimum space they can get by with, and the rest is filled by the spacer. Think of it as a spring that expands as much as it can. If you want smaller spacer increase the minimum sizes of the widgets.

        You can set a fixed size of an element by setting min and max to the same values, but that would defeat the purpose of a layout or a spacer.

        L 1 Reply Last reply
        2
        • Chris KawaC Chris Kawa

          @lukutis222 said:

          1. How can I ensure that comboboxes are aligned nicely?

          Use a layout that has columns. Like @jsulm said QFormLayout is best suited for two column label+widget layouts. In cases like yours, where you have more columns and you want a spacer at the end, a QGridLayout would do nicely.

          1. How can I manually modify the width of horizontal spacers?

          You don't, or rather shouldn't. Layouts resize elements according to their policies. The default policy for spacers is expanding, meaning the widgets in the layout take the minimum space they can get by with, and the rest is filled by the spacer. Think of it as a spring that expands as much as it can. If you want smaller spacer increase the minimum sizes of the widgets.

          You can set a fixed size of an element by setting min and max to the same values, but that would defeat the purpose of a layout or a spacer.

          L Offline
          L Offline
          lukutis222
          wrote on last edited by
          #4

          @jsulm @Chris-Kawa
          Thanks both. I have easily managed to sort my widget by using grid layout as you have suggested

          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