c++qtc++11qcomboboxqstylesheet

Customizing QComboBox


I want to customize QComboBox. The behavior I'd like to see is a combo box where it's button (the down facing arrow), takes the whole area of the combo box when it's collapsed. I know that QComboBox is a QPushButton with a QListWidget (or some other widget). I'd like to change the QPushButton to take up the whole area. I'd like to also get rid of the area where the current selection is being displayed. Any tips will be appreciated!

Here's what I want :

enter image description here

I am able to control the width of the drop down list through QComboBox::view()->setMinimumWidth(). That way I can have a narrower combo box but a much wider drop down list. That part is done. Now I need to just have a button there.


Solution

  • Setting QComboBox button, actually down arrow, can be done by a style, using down-arrow style, however, since you require the selection to be not visible, you need to take care of other settings, the width of the combobox and the drop-down should probably to exactly match your image, example style to accomplish that:

    QComboBox::down-arrow {
        image: url(arrow.png); /* Set combobox button */
    }
    
    QComboBox::drop-down {
        width: 20px;  /* set the width of the drop down */
    }
    
    QComboBox {
        border: 0px;
        max-width: 20px; /* width of whole box */
    }