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

QTComboBox model returning fewer items that reported by rowCount(..)



  • using a model derived from QAbstractListModel and after expanding the list box once QComboBox requests fewer data items than what is returned by rowCount(...).


  • Moderators

    @duckman What do you mean by "expanding the list box once"? Also can we see the code for creating the model and qcombobox? Are you removing items from the model perchance?

    Definitely going to need to see some code to help with this. AFAIK there is no bug that mysteriously drops items from the model when shown. :)



  • "expanding the list box once" means that once items have been added to the to the model, I click on the ComboBox to reveal the list box contents then hit escape to list the list box collapse. I then add a couple of items to the model then expand the list box again. the model returns the correct count but the ComboBox does not as for all of the model's list entries.

    Note that if I keep adding items to the model without expanding the list box and then expand the list box all of the items are visible but again after the list box expansion the model returns the correct count and the ComboBox only asks for a subset of the item.

    The model is derived from QAbstractListModel, the ComboBox is not editable and the only 2 functions the model implements are rowCount(...) and data(...)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you correctly implementing the notification part of your model modifications ?



  • It is my understanding that whenever the model is modified it should emit a signal notifying the ui to update itself which causes rowCount(...) to be called followed by a data(...) call rowCount(...) number of times. Are there additional signals that should be emitted by my model or UI?


  • Lifetime Qt Champion

    Take a look at the Model View Programming chapter of Qt's documentation, especially the Inserting and removing rows part.



  • Thanks for the tip, I was unaware that it was necessary to override insertRows(...) and removeRows(...).


  • Lifetime Qt Champion

    It usually is when using custom types. Depending on your use case a QStringListModel might be enough for you.


Log in to reply