Nominate our 2022 Qt Champions!

PyQt: Combobox with checkable items (no external libs)

  • I'm trying to make a combobox with checkable items. So far I've made a model that inherits from QAbstractTableModel, returns the Qt.ItemIsUserCheckable flag and handles the Qt.CheckStateRole in data(). Everything works fine when using a tableview on the model (checkboxes with labels in the first column). However when I point a QComboBox to the model, only the labels are displayed in the list i.e. no checkboxes. Can anyone offer suggestions on how to get this to work?

    Something like what is described in this post is what I'm after:

    External libraries (e.g. Qxt) aren't an option. Also I need to use a Model/View approach rather than manually adding items, so the method of using a QMenu with checkable QActions is less than ideal.

  • If using libQxt isn't an option (why not? It's licence is very liberal), then you can at least use it for inspiration. It is open source, and it has a working example of exactly what you are after.

    Sure, it's API is based on that of QComboBox, but I am sure you can easily figure out how you can use a normal item view from those sources.

  • Thanks Andre, the main reason is that QxT bindings for Python aren't readily available. All I'm after is a widget that allows items from a model to be selected and that uses a minimum of screen real-estate (hence why a combobox would be good!) I will look into the Qxt source and possibly an alternate widget to do the same thing.

  • I solved the problem - the custom model with checkstate role supported was working all along. It turns out checkboxes were just not being rendered when using Gnome under Ubuntu (10.04). Checkboxes are rendered correctly when using KDE or Windows XP. This must be a bug in Qt.

Log in to reply