Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QComboBox Items spacing



  • Hi,

    I have a problem with a QComboBox. The list is 9 items long and some of them are 2 lines long.

    So it gets difficult to read the list. I would like to add some more space between the items.

    I tried using stylesheets, but it did not work. Do you have any ideas on how to do this?

    Thanks



  • QComboBox::view() will give a QAbstractItemView pointer. Since the view is a list view, you can cast it safely to QListView. Use QListView::setSpacing() to something of your choice. Default value is 0.
    @
    QAbstractItemView *abVw = comboInst->view();
    QListView listVw = qobject_cast<QListView>(abVw);
    if (listVw) {
    listVw->setSpacing(2);
    }
    @



  • THANK YOU!

    Just What I needed!



  • Here is a bonus for anyone having the same question as i did.

    here is what I did

    @QAbstractItemView *abVw = SetMode->view();
    QListView listVw = qobject_cast<QListView>(abVw);
    if (listVw) {
    listVw->setSpacing(2);
    listVw->setAlternatingRowColors(1);
    QPalette p= listVw->palette();
    p.setColor(QPalette::AlternateBase, Qt::lightGray);
    listVw->setPalette(p);
    }@

    where SetMode is a pointer to my ComboBox. It sets an alternating color for the comboBox at the same time giving it some nice spacing


Log in to reply