Display a QAbstractTableModel in a QComboBox

761 views Asked by At

Good evening StackOverflow. This is my first question, so I hope it will not be too dumb.

I have a model sublassing QAbstractTableModel that stores items inheriting QObject. I want to display the list of the items in a QComboBox, which is updated when the model changes.

The model implements the four needed functions rowCount(), columnCount(), data() and headerData().

My question is : how is used a QAbstractItemModel in a QComboBox when using QComboBox::setModel() ? (in other words, does column count must be equal to 1, for instance ?).

And how do I choose the text diplayed for each item in the QComboBox when using a QAbstractModel ? I have found a objectName() method in QObject but I'm not really sure on the way it is used.

Thank you !

1

There are 1 answers

0
Rustam Safin On BEST ANSWER

You can subclass your QAbstractTableModel providing columnCount() that return 1.

Then, when you will override

QVariant QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole)

you must react to role. If it's DisplayRole you will return text that you get from your model's objects