c++qtqt5qcombobox

How to change QComboBox items' height


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


Solution

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