4

I have a dialog set up like this:

enter image description here

Every components' properties are set to their defaults aside from the captions. I want the row containing label_3 to be the same height as the other three rows (and that bottom row to be expanded to take up the remaining space, as shown, to be clear). The problem is the checkbox isn't the same height as the text boxes, so the row has a different height. According to Designer, in the image above label_1 and friends have height 20, and label_3 has height 13. None of the following attempts worked quite right:

  • Messing with the layoutRowStretch property of the QGridLayout has no effect.
  • I don't want to set a hard-coded fixed height because this may be run on other platforms or with other themes, and so I want to defer component sizes to system defaults.
  • Setting the height to match one of the other label heights in the constructor, e.g.:

    ui->label_3->setFixedHeight(ui->label_2->height());
    

    Yields incorrect results:

    enter image description here

    Debugging shows that label_2->height() in the constructor is 30, not 20 as reported in designer, and so label_3's row ends up too large. Presumably this is because the dialog hasn't been displayed yet at that point and so the components have not been laid out.

I'm out of ideas. What's the cleanest way to do this that also does not involve hard coding sizes?

A Qt5 project for the example above is here.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • i think you should set checkbox size – xinyi May 02 '17 at 03:49
  • Sure that's another totally valid approach, but set it to what? It presents the same issues as setting `label_3`'s size. – Jason C May 02 '17 at 03:50
  • Looks like you have Expanding policy or flex on the check box? Also can you confirm all your labels don't have height=30? – spinkus May 02 '17 at 09:20
  • 2
    Are you programming on Windows 95? :D – deW1 May 02 '17 at 11:30
  • @deW1 Heh, this machine is Windows 7, classic theme. – Jason C May 02 '17 at 15:10
  • @spinkus I can confirm the labels do not have their heights set in designer. All values are their defaults, label height policies are `Preferred`, text and checkbox height policies are `Fixed`. Just to avoid ambiguity for anybody else wondering about the properties: The UI file is [here](https://github.com/JC3/SO43728165/blob/master/mainwindow.ui) and a portion of the generated code is [here](https://pastebin.com/hGxpbtBS). The reason for the layout looking the way it does makes sense and is not unexpected, I'm looking for a way to make the rows consistent but still tight. – Jason C May 02 '17 at 15:23

1 Answers1

1

I don't see any clean way to do this without manually setting the size.

However, from what I just tried in Qt5.5 the minimumSizeHint of the QLineEdit is set before being painted (with the default size you see in QtDesigner), so I was able to set the minimum-size of the QCheckBox in the Dialog constructor.

ui->checkBox->setMinimumHeight(ui->lineEdit->minimumSizeHint().height());

This gets wrong if I manually set a minimumHeight on the QLineEdit, as the minimumSizeHint is not changed, but using a max of the two is working :

ui->checkBox->setMinimumHeight(qMax(ui->lineEdit->minimumSizeHint().height(),
                                    ui->lineEdit->minimumSize().height()));

Edit: in case of an OS-theme where a checkbox would have a greater height than the line-edit, you could use the max of both minimumSizeHint().height() and set it as minimum-height on both widgets.

ymoreau
  • 3,402
  • 1
  • 22
  • 60
  • Nailed it, thanks. I always forget about the size hints. And can confirm, choosing the max of the two hint heights and applying to all widgets works correctly when line edits are smaller than checkboxes (tested by setting very small font sizes). – Jason C May 11 '17 at 13:26