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. Force horizontal spacer in a grid layout to take exactly half the space (and a button other half)

Force horizontal spacer in a grid layout to take exactly half the space (and a button other half)

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttonhorizontalqt creator
5 Posts 3 Posters 4.7k 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
    fugreh
    wrote on 4 Sept 2018, 13:14 last edited by
    #1

    I have 3 groupboxes (highlighted green in the image) all use the grid layout. The first two groupboxes contain two buttons side by side so both buttons take exactly half the horizontal space.

    The third groupbox contains just one button and I would like this button to also take exactly half the horizontal space. When the button is by itself in the groupbox it just stretches to fill the entire space. If I insert a horizontal spacer it instead shrinks to minimum space. I would like the button to take exactly half the space so its size matches with that of the buttons in the other groupboxes.

    0_1536066795562_horizontal spacer.png

    A 1 Reply Last reply 5 Sept 2018, 05:00
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Sept 2018, 20:15 last edited by
      #2

      Hi,

      Can you show how you setup your QGroupBox ?

      It looks like it doesn't have a layout.

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

      1 Reply Last reply
      0
      • F fugreh
        4 Sept 2018, 13:14

        I have 3 groupboxes (highlighted green in the image) all use the grid layout. The first two groupboxes contain two buttons side by side so both buttons take exactly half the horizontal space.

        The third groupbox contains just one button and I would like this button to also take exactly half the horizontal space. When the button is by itself in the groupbox it just stretches to fill the entire space. If I insert a horizontal spacer it instead shrinks to minimum space. I would like the button to take exactly half the space so its size matches with that of the buttons in the other groupboxes.

        0_1536066795562_horizontal spacer.png

        A Offline
        A Offline
        ambershark
        wrote on 5 Sept 2018, 05:00 last edited by
        #3

        @fugreh This is kind of a weird thing to do in a gui, but if you really want to you're going to have to calculate and set the size yourself.

        Just set your button's minimumSize to be the same size as one of the buttons you want to mimic. Then keep your horizontal spacer as is.

        Like I said it's a bit weird though. That's not really how you want to work with layouts imo. If it were me I would have horizontal spacers in all the group boxes to compress the buttons a bit. Extra large white space on buttons always looked really weird to me.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fugreh
          wrote on 5 Sept 2018, 12:28 last edited by
          #4

          @ambershark Well the problem is that this whole panel can be stretched horizontally so the size of the other buttons might change so I can't just set this button's size statically

          @SGaist Hi, here is a screenshot from QtCreator. The groupbox has a grid layout. I tried other layouts, to no avail though.
          0_1536150488051_layout.png

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 Sept 2018, 21:20 last edited by SGaist 9 May 2018, 21:21
            #5

            Code wise using:

            QGroupBox *groupBox = new QGroupBox(tr("My Group Box"));
            QGridLayout *layout = new QGridLayout(groupBox);
            layout->addWidget(new QPushButton(tr("My Nice Button")), 0, 0);
            layout->setColumnStretch(0, 1);
            layout->setColumnStretch(1, 1);
            

            does what you want.

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

            1 Reply Last reply
            2

            1/5

            4 Sept 2018, 13:14

            • Login

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