I want to use QCombobox
as a the Combobox
of Swing
in Java
. So i need to use Model
for holding my object. How can i hold my object in QCombobox. (I think that I should hold data in Model because QCombobox
was designed according to MVC Pattern ... )
Any help will be appreciated.

- 3,402
- 1
- 22
- 60

- 18,331
- 13
- 61
- 80
3 Answers
Depending on what you want to display with your QComboBox
, you'll need to write your own model, inheriting QAbstractListModel
, reimplementing rowCount()
and data()
.
Then, use QComboBox::setModel()
to make the QComboBox
display it.
If you just want to display strings, you can use a QStringListModel
, provided with Qt.

- 17
- 4

- 26,567
- 29
- 98
- 120
-
Where placed the data (not text) in subclassed model? `QComboBox::model()->index(row, 1)`? – Tomilov Anatoliy Oct 16 '14 at 09:39
You can add a model to your QCombobox
by using the setModel
function. You can use a predefined model or create your own by inheriting from QAbstractItemModel
.
Your model will contain your object to separate display from data.

- 14,136
- 6
- 46
- 59
Qt uses a simplified version of MVC which only has the Model / View parts.
You can use one of the provided subclasses of QAbstractItemModel if you don't need any specialized behaviour, which one to use depends on whether you keep your data in a file system or a data structure in memory.
You should read up the whole section on model/view programming in the Qt documentation.

- 686
- 3
- 14

- 4,267
- 1
- 26
- 35