It appears that you're hung on the syntax: you want to replace myQComboBox->itemText(i)
with myQComboBox[i]
. That can be rather easily done:
// implementation
class ModelAdapter {
QPointer<QAbstractItemModel> m_model;
public:
explicit ModelAdapter(QComboBox & box) : m_model(box.model()) {}
explicit ModelAdapter(QAbstractItemModel * model) : m_model(model) {}
QVariant operator[](int i) { return m_model->index(i, 0); }
};
// point of use
ModelAdapter model(myQComboBox);
for( auto i = 0; i < myQComboBox->count(); i++ )
{
result[i] = model[i];
}
With a good compiler, you can do the below and have it produce the same code as if you used combobox.model->index(i, 0)
directly. I don't see the point of it, but hey, it's possible :)
// implementation
class Adapter {
QAbstractItemModel* m_model;
public:
explicit Adapter(QComboBox & box) : m_model(box.model()) {}
explicit Adapter(QAbstractItemModel * model) : m_model(model) {}
QVariant operator[](int i) { return m_model->index(i, 0); }
};
// point of use
for( auto i = 0; i < myQComboBox->count(); i++ )
{
result[i] = Adapter(myQComboBox)[i];
}
A similar adapter could provide you with iterators.