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. Spacing in vertical layout
Forum Updated to NodeBB v4.3 + New Features

Spacing in vertical layout

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

    Hi
    I have a scrollable area containing a Vertical layout.
    I add items to the vertical layout but there appears to be a large amount of spacing between them as shown here

    https://www.dropbox.com/s/x718ndrmo123r1o/chart1.png

    How can I get rid of this?

    Here is the code for my main window
    @
    ui.setupUi(this);

    for (int s = 0; s < 3; s++)
    {
    QSplineSeries* series = new QSplineSeries();

    for (int i = 0; i < 100; ++i)
    {
    series->append(i,qSin(i));
    }
    QChart* chart = new QChart();
    chart->legend()->hide();
    chart->addSeries(series);

    QChartView* chartView = new QChartView(chart);

    chartView->setRenderHint(QPainter::Antialiasing);
    chartView->scale(1.0,0.5);
    QHBoxLayout* hoizontalLayout = new QHBoxLayout(this);
    QLabel *l1 = new QLabel(QString("%1").arg(s));
    hoizontalLayout->addWidget(l1);

    hoizontalLayout->addWidget(chartView);

    QLabel *l2 = new QLabel(QString("geo %1").arg(s));
    hoizontalLayout->addWidget(l2);

    ui.verticalLayout->addLayout(hoizontalLayout);
    }
    @

    Thanks

    1 Reply Last reply
    0
    • T Offline
      T Offline
      toptan
      wrote on last edited by
      #2

      Vertical and horizontal layouts always "spread" widgets evenly. If parent widget is 300px and you have 3 children widgets, all of them will end up with 100px. To "compact" just add horizontal or vertical spacer as last in layout. For vertical layout add vertical spacer, and for horizontal add horizontal spacer.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        Thanks for your reply
        I added this code to the end of the for loop -
        @
        QSpacerItem *spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

        ui.verticalLayout->addItem(spacer);
        @

        But the result is still the same?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If i'm not wrong, a simple addStretch(1) after your last widget should push them to the top.

          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
          1
          • G Offline
            G Offline
            GrahamL
            wrote on last edited by
            #5

            I have tried both of these but the result is still the same
            @
            ui.verticalLayout->addLayout(hoizontalLayout);
            }

            ui.verticalLayout->addStretch(1);
            // spacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
            //
            // ui.verticalLayout->addItem(spacer);
            @
            Is there anything else I can try?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toptan
              wrote on last edited by
              #6

              What is the minimum size of those widgets? Also, you can try to play with layout->setSpacing(), or try to set spacing directly in layout properties from the designer.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                b1gsnak3
                wrote on last edited by
                #7

                Well the spacer item should've worked if you would have set the vertical sizepolicy to expanding. AFAIK the QSpacerItem consists of: width, height, hData(which is horizontal data), and vData(which is vertical data). So you should have for the spacer item this:

                @spacer = new QSpacerItem(40, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);@

                Also you should use addSpacerItem instead of addItem.

                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