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. Space Buttons equally in Layout, but with an empty slot
Forum Updated to NodeBB v4.3 + New Features

Space Buttons equally in Layout, but with an empty slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 5.5k Views 1 Watching
  • 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 mrjj

    @JonB
    as it would distribute the space evenly ? Or did i miss the point ?
    alt text

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #10

    @mrjj
    I think you absolutely did not miss the point, and that looks very pretty :)

    mrjjM 1 Reply Last reply
    0
    • JonBJ JonB

      @mrjj
      I think you absolutely did not miss the point, and that looks very pretty :)

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

      @JonB
      Ok, thats good. very hot here and brain a bit fried :)

      1 Reply Last reply
      0
      • mrjjM mrjj

        @JonB
        as it would distribute the space evenly ? Or did i miss the point ?
        alt text

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #12

        @mrjj
        I don‘t know why the op says a Qqspacer didn‘t work.
        This is exactly why they exist.

        A Qwidget will do it, as you‘ve shown, but a spacer will too, with much less overhang ☺️


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        JonBJ 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @mrjj
          I don‘t know why the op says a Qqspacer didn‘t work.
          This is exactly why they exist.

          A Qwidget will do it, as you‘ve shown, but a spacer will too, with much less overhang ☺️

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #13

          @J.Hilk
          The OP wrote/claimed:

          I have tried to use Spacers, but it seems like those require a specific height and weight

          So that should not have been the case?

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #14

            Hi
            well spacer could do the same but requires more fiddling as its greedy and compresses the
            the ends so dont work the same "out of the box" but im sure it can be adjusted to do the same.
            alt text

            J.HilkJ 1 Reply Last reply
            4
            • mrjjM mrjj

              Hi
              well spacer could do the same but requires more fiddling as its greedy and compresses the
              the ends so dont work the same "out of the box" but im sure it can be adjusted to do the same.
              alt text

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #15

              @mrjj

              mmh, not to tricky in my opinion x):

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  QVBoxLayout *vLayout = new QVBoxLayout(ui->centralWidget);
                  for(int i(0); i < 5; i++){
                      QPushButton *btn(new QPushButton(QString("Button %1").arg(i)));
                      btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                      vLayout->addWidget(btn,1);
                  }
                  QSpacerItem *spacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Expanding);
                  vLayout->addItem(spacer);
                  vLayout->setStretch(5,1);
                  QPushButton *btn(new QPushButton(QString("Last Button")));
                  btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  vLayout->addWidget(btn,1);
              }
              

              sry, don't know of an easy way to make a gif on the fly. X)

              0_1533155117534_da2d388a-623f-4f0e-9ce6-134dd0b3ea1c-image.png

              0_1533155138761_3de11ba4-6b14-42cb-93dc-26e97a5d16bc-image.png


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              VRoninV 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @mrjj

                mmh, not to tricky in my opinion x):

                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                
                    QVBoxLayout *vLayout = new QVBoxLayout(ui->centralWidget);
                    for(int i(0); i < 5; i++){
                        QPushButton *btn(new QPushButton(QString("Button %1").arg(i)));
                        btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                        vLayout->addWidget(btn,1);
                    }
                    QSpacerItem *spacer = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Expanding);
                    vLayout->addItem(spacer);
                    vLayout->setStretch(5,1);
                    QPushButton *btn(new QPushButton(QString("Last Button")));
                    btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                    vLayout->addWidget(btn,1);
                }
                

                sry, don't know of an easy way to make a gif on the fly. X)

                0_1533155117534_da2d388a-623f-4f0e-9ce6-134dd0b3ea1c-image.png

                0_1533155138761_3de11ba4-6b14-42cb-93dc-26e97a5d16bc-image.png

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #16

                @J.Hilk said in Space Buttons equally in Layout, but with an empty slot:

                mmh, not to tricky in my opinion x):

                Just to clarify, the central point here is that the buttons and the spacer have the same vertical size policy (doesn't have to be "expanding").

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                J.HilkJ 1 Reply Last reply
                1
                • VRoninV VRonin

                  @J.Hilk said in Space Buttons equally in Layout, but with an empty slot:

                  mmh, not to tricky in my opinion x):

                  Just to clarify, the central point here is that the buttons and the spacer have the same vertical size policy (doesn't have to be "expanding").

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #17

                  @VRonin
                  true,
                  but if it's not set to expanding, the sizeHint would also have to be the same for Spacer and Button, right?


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @JonB
                    as it would distribute the space evenly ? Or did i miss the point ?
                    alt text

                    S Offline
                    S Offline
                    Smeeth
                    wrote on last edited by
                    #18

                    @mrjj Is your first example using a GridLayout? Or a vertical layout? When I place the buttons and a widget in the form and combine into a QVBoxLayout or a QGridLayout, shrinking and expanding the layout only changes the height of the widget, not the buttons.

                    mrjjM 1 Reply Last reply
                    0
                    • S Smeeth

                      @mrjj Is your first example using a GridLayout? Or a vertical layout? When I place the buttons and a widget in the form and combine into a QVBoxLayout or a QGridLayout, shrinking and expanding the layout only changes the height of the widget, not the buttons.

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

                      @Smeeth
                      Hi noticed the buttons come default with Fixed as policy. I changed that for that sample.

                      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