4

How to change QComboBox items height size?

I want change only height - I need it larger.

Its strange that there are no any functions for this purposes.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
AeroSun
  • 2,401
  • 2
  • 23
  • 46

2 Answers2

3

A first option is to set a new popup, for example a QListView and change the size using Qt Style Sheet:

#include <QApplication>
#include <QComboBox>
#include <QListView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBox combo;
    QListView *view = new QListView(&combo);
    view->setStyleSheet("QListView::item{height: 100px}");
    combo.setView(view);
    combo.addItems({"A", "B", "C", "D", "E", "F"});
    combo.show();
    return a.exec();
}

Another option is to set a delegate to the popup that resizes:

#include <QApplication>
#include <QComboBox>
#include <QStyledItemDelegate>
#include <QAbstractItemView>

class PopupItemDelegate: public QStyledItemDelegate
{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QSize s = QStyledItemDelegate::sizeHint(option, index);
        s.setHeight(60);
        return s;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QComboBox combo;
    combo.view()->setItemDelegate(new PopupItemDelegate(&combo));
    combo.addItems({"A", "B", "C", "D", "E", "F"});
    combo.show();
    return a.exec();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
1

You can control the height through the setView method and QSS.

self.comboBox.setView(QtWidgets.QListView())

QSS

QListView::item {
    height: 30px;
}

The sample code:

import sys
from PyQt5 import QtWidgets, QtCore, QtGui


class MainWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.__ui__()
        self.__style__()

    def __ui__(self):
        self.layout = QtWidgets.QVBoxLayout()
        self.comboBox = QtWidgets.QComboBox()
        self.comboBox.setView(QtWidgets.QListView())
        self.comboBox.addItems(["one", "too", "three", "four", "five", "six"])
        self.layout.addWidget(self.comboBox)
        self.setLayout(self.layout)

    def __style__(self):
        self.comboBox.setStyleSheet("QListView::item {height:30px;}")

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    widget = MainWidget()
    widget.show()
    sys.exit(app.exec_())
Degang Guo
  • 475
  • 8
  • 18