How to make C++ qml model example editable?
-
Hello, I am working on this example http://doc.qt.io/qt-5/qtquick-models-abstractitemmodel-example.html. I added setData method to edit animal type using its role. But I see in debug mode, the program never breaks in setData method even though i added breakpoint. Nor, could I edit the animal type.
bool AnimalModel ::setData(const QModelIndex &index, const QVariant &value, int role) { if (role == TypeRole ) { qDebug() << "Break here"; } return true; }
Please help.
-
Did you add any code on QML that would trigger a model change? Like, a TextInput or ComboBox that would allow the user to modify animal type?
-
Well then you don't modify any model data, so setData() is never called.
You need to modify
type
orsize
somewhere for setData() to be invoked. -
Well then you don't modify any model data, so setData() is never called.
You need to modify
type
orsize
somewhere for setData() to be invoked. -
You have probably modified
text
property of TextInput, but nottype
property of the model.Sth. like:
TextInput { text: type onEditingFinished: type = text }
-
You have probably modified
text
property of TextInput, but nottype
property of the model.Sth. like:
TextInput { text: type onEditingFinished: type = text }
@sierdzio said in How to make C++ qml model example editable?:
onEditingFinished: type = text
Thanks a lot. This triggered the setData. I also need to trigger the setData using checkRole i have added. So, there is checkbox in qml which is a boolean in C++ model. I would like this to trigger the setData method too.
-
@sierdzio . I now replaced Text with TextInput, and I edited the text in the application. But it is still not triggered.
@milan As @sierdzio said, you need to modify
type
orsize
. It will much simpler if you use two separated input control for each of the data to set.Something like this maybe:
delegate: Row{ spacing: 0 Text{ text: "Animal: "} TextInput { text: type onEditingFinished: type = text } Text { text: ", " } TextInput { text: size onEditingFinished: size = text } }
-
Happy coding :-)