How to fill QML Combobox on C++ side by ListElement
-
Hi Everybody. I write a code which fill combobox from C++ side. My code is Ok by using QStringList
class ComboboxUpdate:public QObject { Q_OBJECT Q_PROPERTY(QStringList comboList READ comboList) public: ComboboxUpdate(QObject *parent = 0); const QStringList comboList(); void setComboList( const QStringList &comboList); ......
Qml part is:
ComboBox { model:combomodel.comboList ......
I don't want to put details here.I can fill it from c++ side with code blocks above.But I want to fill with ListElement not only text For Exaplem qml:
ComboBox { ListModel { ListElement {text:"Orange" ;sqlid:1;} ListElement {text:"Apple" ;sqlid:2;} ListElement {text:"Greeps";sqlid:3;} }
How to pass stuff like that to combobox from c++ side.Because sqlid is necessary for me.I use them on OnCurrrentTextChange event that put the sqlid in Javascript code.Any ideas ? Thank you ....
-
So assuming your QObject subclassed class has a property say for eg. text then to populate ComboBox with it you will have to set textRole to 'text'
ComboBox { model: combomodel.comboList textRole: "text" }
after that you can get your sqlid attribute from model
-
You need to change QStringList to QList<QObject*>
Fromclass ComboboxUpdate:public QObject { Q_OBJECT Q_PROPERTY(QStringList comboList READ comboList) ... }
to
class ComboboxUpdate:public QObject { Q_OBJECT Q_PROPERTY(QList<QObject*> comboList READ comboList) }
for example you could change your code in this way or add a subclass
class ComboboxUpdate:public QObject { Q_OBJECT Q_PROPERTY(QStringList comboList READ comboList) Q_PROPERTY(int sqlid READ sqlid WRITE setSqlid NOTIFY sqlidChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) }
and in ComboBox you can use
ComboBox { id: typeComboBox model:combomodel.comboList textRole: "text" onCurrentIndexChanged: { var sqlid = typeComboBox.model[typeComboBox.currentIndex].sqlid; console.log(sqlid); } }
I hope that my answer will be helpful for you.