1

I have several QHBoxLayout's that are all contained in a QVBoxLayout.

QHBoxLayout *hb1 = new QHBoxLayout;
// ...
QHBoxLayout *hb2 = new QHBoxLayout;
// ...

QWidget *hb1_layout = new QWidget;
hb1_layout->setLayout(hb1);

QWidget *hb2_layout = new QWidget;
hb2_layout->setLayout(hb2);

// ...

QVBoxLayout *ps = new QVBoxLayout;
ps->addWidget(hb1_layout);
ps->addWidget(hb2_layout);
ps->addWidget(hb3_layout);
ps->addWidget(hb4_layout);

What I'm trying to do is pack the widgets within each QHBoxLayout closer to the widgets in the QHBoxLayout next down. I.e., there's a lot of space between each row of widgets between the horizontal layouts.

When resizing the window, there needs to be a minimum size that the widgets won't go beyond. But when resizing larger, they evenly spread out.

If I resize the window, I can get it to look like what I want. But I don't know how to programmatically initialize the window to the right size in the first place. When I start my app, I have to minimize by resizing the window to get the widgets to appear nicely packed.

Thoughts?

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Ender
  • 1,652
  • 2
  • 25
  • 50
  • I don't use QGridLayout because the widgets are of different sizes and aren't evenly spaced, besides, they look too "gridy" when doing so. Will add detail to the question. – Ender Mar 30 '18 at 22:34
  • I don't see how this is particular. I just want widgets to appear closer together between the horizontal layouts. Your comments make me think this may be normal and maybe I need to reconsider my expectations. – Ender Mar 30 '18 at 22:37
  • Or maybe you can listen to me and provide a [mcve], many things have default values ​​so if you provide an example of your problem we could give you a better approach or a general solution, but as you do not we will not be able help him. Is it so hard to create a [mcve]?, I recommend reading [ask] and understand why we ask certain things. – eyllanesc Mar 30 '18 at 22:40
  • I'll work on the example. There's a lot that I can't show for NDA reasons so I'll need to remove some stuff. – Ender Mar 30 '18 at 22:41
  • Well aware of what it is, and no one mentioned publishing the project. Calm down. – Ender Mar 30 '18 at 22:44
  • I'm calming down, I only said it because you mentioned about NDA. – eyllanesc Mar 30 '18 at 22:45

1 Answers1

1

programmatically initialize the window to the right size in the first place.

use adjustSize(); to set window size fit your widgets.

What I'm trying to do is pack the widgets within each QHBoxLayout closer to the widgets in the QHBoxLayout next down.

QVBoxLayout won't do it straightforward .. , instead use a QFormLayout, the rows do not spread out on their own, the rows just expand to their contents. you can adjust the spacing between rows to a suitable int value, QFormLayout::setVerticalSpacing(40); .. and between columns QFormLayout::setHorizontalSpacing(40);

and, IMHO, no point manipulate sizes of widgets or spacing of rows everytime window gets a resize .. if your window has a handful of widgets .. it will look as you expect.

sample:

  QFormLayout * const formlayout = new QFormLayout(this->ui->centralWidget);
  QHBoxLayout *hb1 = new QHBoxLayout;
  QHBoxLayout *hb2 = new QHBoxLayout;
  QHBoxLayout *hb3 = new QHBoxLayout;
  QLabel *hb1_layout = new QLabel("Short Text1");
  hb1_layout->setStyleSheet("background-color: rgb(212, 192, 255)");
  hb1_layout->setFixedSize(100,100);
  hb1->addWidget(hb1_layout);
  formlayout->addRow(hb1);
  //
  QLabel *hb2_layout = new QLabel("Long Long Long Text");
  hb2_layout->setFixedSize(200,200);
  hb2_layout->setStyleSheet("background-color: rgb(25, 255, 192)");
  hb2->addWidget(hb2_layout);
  formlayout->addRow(hb2);
  //
  QLabel *hb3_layout = new QLabel("Long Long Long Text too \n Long Long Long Text too \n Long Long Long Text too");
  hb3_layout->setStyleSheet("background-color: rgb(100, 25, 25)");
  hb3_layout->setFixedSize(300,300);
  hb3->addWidget(hb3_layout);
  formlayout->addRow(hb3);
  //
  formlayout->setHorizontalSpacing(40); // between columns
  formlayout->setVerticalSpacing(10); // between rows
  adjustSize(); // Window to contents
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47