AeroSun
AeroSun

Reputation: 2561

How to change QComboBox items' height

I want change only the height, I need it larger.

Upvotes: 6

Views: 8479

Answers (4)

falero80s
falero80s

Reputation: 398

Following is what worked for me. I'm on Qt 6.7. You need to enable setEditable.

qComboBox->setEditable(true);
qComboBox->setMaxVisibleItems(whatever-number-of-items);

Upvotes: 0

Jadamec
Jadamec

Reputation: 923

I had this problem on macOS - list items were large, but the QComboBox itself preserved its default height.

No luck setting minimumSize, etc.

I managed to solve this by setting the combo box style to Fusion. Then the control height respects the setMinimumHeight(...)

combo.setStyle(QStyleFactory::create("Fusion"));

Upvotes: 1

eyllanesc
eyllanesc

Reputation: 243897

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();
}

Upvotes: 5

Degang Guo
Degang Guo

Reputation: 505

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_())

Upvotes: 2

Related Questions