c++qtqmlqcomboboxqstringlist

What's the accepted way to fill a QComboBox defined in QtQuick QML, from C++?


I want to populate a QComboBox defined in QML from my C++ code. I have seen two possible ways to do this:

  1. Define a list (as a QStringList for example) from the C++ code, and expose it as Q_ELEMENT. Then access that list from the C++, by saying model: backend.qlist assuming the list is defined in backend. Or
  2. Find the QComboBox in the C++ code by using view.rootObject()->findChild(). Then use addItem() to populate the list.

What is best practice?


Solution

  • 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).