I want to populate a QComboBox defined in QML from my C++ code. I have seen two possible ways to do this:
- Define a list (as a
QStringList
for example) from the C++ code, and expose it asQ_ELEMENT
. Then access that list from the C++, by sayingmodel: backend.qlist
assuming the list is defined inbackend
. Or - Find the QComboBox in the C++ code by using
view.rootObject()->findChild()
. Then useaddItem()
to populate the list.
What is best practice?
By far the first option!
QML stands for Qt Modeling Language, following the model-view architecture, in which the model (here C++) should not know anything about the view (QML).