1

I am trying to set the minimum height of the rows in the QComboBox dropdown menu without changing their width or the size of the QComboBox itself.

By default the width and height of the row items are calculated by its data. To make the dropdown list resize to the width of the data I am calling

view()->setMinimumWidth(view()->sizeHintForColumn(0);

in the combobox's ::showPopup().

However, since I have different font sizes in rows, I would like to enforce a minimum height on each row.

I tried to use setSizeHint on each QStandardItem that is added in the first column to the rows but without success. I tried:

item->setSizeHint(QSize(item->sizeHint().width(), minHeight));

(https://stackoverflow.com/a/10749345/1981832)

But this lead to the dropdown menu not resizing to the data's width.

So, I tried to leave the width "unset". However, both

item->setSizeHint(QSize(QSize().width(), minHeight));

and

item->setSizeHint(QSize(0, minHeight));

did not work. Does anyone have an idea how I can enforce a minimum height on the rows of the QComboBox without messing up the automatic width calculation?

Daniel
  • 3,383
  • 4
  • 30
  • 61
  • Your question is confusing, from what you point out I can understand that you want to establish a fixed height, am I correct? I do not understand what you mean with a minimum height, minimum with respect to what? – eyllanesc Jul 31 '20 at 06:20
  • @eyllanesc Each item in the dropdown list has a certain font set to it (think wordprocessor style chooser: heading 1, heading 2, etc.). Now, some of the font sizes in the list can be tiny. To make it possible to still easily choose them, I want to set the minimum height of a row to the standard row height (with the default UI font). So, yes, I want to fix the row height for these items. But I still want the items with larger fonts to have a greater height. That's why I was saying that I want each row to have a minimum height. – Daniel Jul 31 '20 at 06:27
  • @eyllanesc Currently, I just manually check whether a font size of an item is greater than the default font size and then fix the minimum height only if it is smaller. I am not sure that's the best way to achieve this. But the main problem is that I cannot set a fixed height without messing up the automatic width calculation. – Daniel Jul 31 '20 at 06:31
  • From what I understand: 1) Do you want to set a predefined fixed height for each row? That is, each row could have different heights. 2) That minimum height depends on the fonts, am I correct? – eyllanesc Jul 31 '20 at 06:36
  • @eyllanesc Sounds right. – Daniel Jul 31 '20 at 06:45

1 Answers1

1

Just use this lines:

QComboBox combo;
QListView *view = new QListView(&combo);
view->setStyleSheet("QListView::item{height: 100px}");
combo.setView(view);

Or write this code in qss file:

QListView::item {
    height: 30px;
}

After that use:

QComboBox::setView(QAbstractItemView *itemView)
bogdyname
  • 358
  • 2
  • 10
  • Thanks for your answer. I am not using a `QListView` but just a layout model set via `setModel`. Should the solution work there as well? Also, is there a particular point at which the stylesheet has to be set? – Daniel Jul 31 '20 at 17:02
  • Okay, got it to work with a style sheet. The important ingredient for my specific situation was to use `QStyledItemDelegate` instead if `QStyledItemDelegate` and then `QComboBox QAbstractItemView::item {min-height: 30px;}`. – Daniel Aug 09 '20 at 06:11