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. QBoxLayout and "stretching"

QBoxLayout and "stretching"

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 14.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.
  • R Offline
    R Offline
    ronM71
    wrote on last edited by
    #1

    Trying to implement something similar to a toolbar where the elements are fixed in size and align to the left.

    When i implement QBoxLayout and add a few widgets there, they seem to "spread" evenly across the expanse of the layout object. I'd like them to align to the left and have no spacing between them at all, which also means, large empty area to the right of the last widget as the sum width of all widgets is smaller than the QBoxLayout object.

    For some reason, none of the following produces the desired effect. What am I forgetting?

    @this->buttonLayout->setDirection(QBoxLayout::LeftToRight);
    this->buttonLayout->setAlignment(Qt::AlignLeft);
    this->buttonLayout->setSpacing(0);
    this->buttonLayout->setContentsMargins(0, 0, 0, 0);@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Insert a "QSpacerItem":http://doc.trolltech.com/4.7/qspaceritem.html into your layout.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ronM71
        wrote on last edited by
        #3

        I added:

        @this->buttonLayout->addSpacerItem(new QSpacerItem(0,0, QSizePolicy::Expanding,QSizePolicy::Expanding ));@

        but there is no change in the dynamically growing spaces between the widgets I add. It keeps the relative size of the width as I grow and shrink the window. Are you sure this should be enough?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dakron
          wrote on last edited by
          #4

          yes, mlong is right. Try to insert the QSpacerItem as the first element from the right side and make it expandable.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ronM71
            wrote on last edited by
            #5

            It makes no difference if I put the "addWidget" x 5 times, before or after the addSpacerItem call. It's always the same. The widgets just spread out evenly. I must be doing something wrong. This is very strange. I'll try doing this graphically in the 'Designer' and take a look at the generated code.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dakron
              wrote on last edited by
              #6

              For example:
              @
              QHBoxLayout *layout = new QHBoxLayout;
              layout->setSpacing(0);
              layout->setContentsMargins(0, 0, 0, 0);
              layout->addWidget(new QPushButton("Button1"));
              layout->addWidget(new QPushButton("Button2"));
              layout->addWidget(new QPushButton("Button3"));
              layout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding));
              @

              1 Reply Last reply
              0
              • R Offline
                R Offline
                ronM71
                wrote on last edited by
                #7

                I was able to do it like that, however I have a stranger problem. Spacing is twisted.

                my widgets are of variable widths (same height). I implemented the sizeHint method in the class of my widgets. If I set spacing to be "0", there is an overlap and ALL widgets are spaced such that they look even in width. very strange. If I do "-1" in the spacing, they will be okay with the variable widths, but will have a ~2px spacing between them.

                I've checked and the sizeHint returns the correct size any time...

                I can't figure this weirdness out. Trying more things...

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  ronM71
                  wrote on last edited by
                  #8

                  Why does spacing "0" cause an overlap of my widgets? it takes the width of the last widget and applies it to all the prior siblings which are wider. Very strange behavior. A spacing of "-1" spaces the variable width widgets okay, but with a 2 pixel spacing between widgets. I just don't get it.

                  My widgets set "setMinimumSize" which is correct, and also implement hintSize which I checked gives out a correct value. Why is the layout messing things up with spacing "0"? is this a Qt bug?

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    ronM71
                    wrote on last edited by
                    #9

                    A more "scientific" observation.
                    I have 3 widgets in the layout.

                    when spacing is set to 0, the first two (all are of variable width) are "chopped" by 9 pixels in the width and become overlapping. the third one remains its regular size.

                    when spacing is "-1", the width are ok, but there's a 2 pixel spacing between widgets.

                    help!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      silver47
                      wrote on last edited by
                      #10

                      What about addStretch?
                      @
                      QHBoxLayout *layout = new QHBoxLayout;
                      layout->setSpacing(0);
                      layout->setContentsMargins(0, 0, 0, 0);
                      layout->addWidget(new QPushButton("Button1"));
                      layout->addWidget(new QPushButton("Button2"));
                      layout->addWidget(new QPushButton("Button3"));
                      //layout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding));
                      layout->addStretch(9);
                      @

                      sorry for my english :(

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        ronM71
                        wrote on last edited by
                        #11

                        Adding "addStretch(9)" for some reason makes everything invisible. no buttons are visible at all. and this is the wrong approach. I can hack this by adding the "setSpacing(9)"... but I need to understand why I need to do that. there is another problem on Qt Bug lurking here. Why should I add "9" and not 3.14? why not 2,432,432.12? that's so special about "9"? this needs to be investigated ideally, not hacked around.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          ronM71
                          wrote on last edited by
                          #12

                          Solution:

                          This appears to be a knows Qt problem under OSX. Note the following bug report: http://bugreports.qt.nokia.com/browse/QTBUG-14591

                          Resolution:

                          by setting the Qt::WA_LayoutUsesWidgetRect attribute for each button.

                          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