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. Horizontal Spacer expanding incorrectly
Forum Update on Monday, May 27th 2025

Horizontal Spacer expanding incorrectly

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.6k 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.
  • F Offline
    F Offline
    fclushio
    wrote on last edited by
    #1

    I want to achieve specific behavior:

    1. A button with the text expands depending on its text
    2. Below this button locates another button with fixed size (max 20 pxls)
    3. First button does not expands more than it needs, the rest of content goes to QLineEdit on the right
    4. Second button should be located on the right side, so it has to be somehow shifted (Spacer e.g.)

    So I assume:

    1. First button horizontal policy must be 'Maximum' so its width stays constant depending on its content
    2. Second button should be 'fixed' horizontally to indicate that it has a constant size at a compile time
    3. Slider values should be 0x0 so it goes lower as much as it needs
    4. QLineEdit should be 'Expanding' so it expands

    I added a change name to 'a' on click to the first button for demo

    The first button and QLineEdit get along just fine (first line on pic)
    The problem arises when we need to shift second button. Within designer it behaves as I expect (second line on pic).
    But when compiled a empty space appears on the left of fixed-size button
    Untitled.png

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your spacer must be 'Minimum' - then all works fine for me

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • F Offline
        F Offline
        fclushio
        wrote on last edited by
        #3

        On last picture it is 'Minimum'. You might have some default size set. If you set it 0x0 I think you will get the same situation.
        Button 'G' - Fixed
        Spacer - Minimum, 0x0
        'AAAAAAAAAAAAAAA' - Maximum
        LineEdit - Expanding

        Untitled.png

        https://pastebin.com/kS3jBecJ
        UI code
        https://drive.google.com/file/d/1Dm4IpgNZFO2OggFXlpKkQiqHdYE6xrdb/view?usp=sharing
        App code

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Use a QGridLayout instead a QHBox & QVBoxLayout

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fclushio
            wrote on last edited by
            #5

            Same results
            qtcreator_ic6mz2zaot.png

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              No, put the spacer and the two buttons in a grid layout. Otherwise the spacer + button and the button don't have any relation to each other and therefore the spacer expands larger than the button.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • F Offline
                F Offline
                fclushio
                wrote on last edited by
                #7

                Worked perfectly. Many thanks.
                qtcreator_OwiPGOdHV7.png

                1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  You can also try to align G label to left when it is added to the layout.
                  something like:
                  layout->addWidget( g_label, 0 , Qt::AlignLeft );
                  In this case, spacer may not be needed at all.

                  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