c++ - cpp Qt Model not updating QML view -
i using custom model qml view , want able move items drag , drop. use list in cpp model set data , bind model qml view
.
however, when drop item in new position, the view ask model move item on new position. in purpose, update datalist function datalist.move(oldindexposition, newindexposition)
. my datalist correctly updated doesn't refresh qml view. tried use signal emit datachanged()
but still not refreshing view.
i don't understand should do, suggestion ?
here simple example of try do. notice there no drag , drop here, in order make easier understand:
public: q_invokable void move(int oldindex, int newindex) { datalist.move(oldindex,newindex); emit datachanged(this->index(oldindex),this->index(newindex)); } signals: void datachanged(const qmodelindex & topleft, const qmodelindex & bottomright);
model.h
int main(int argc, char ** argv) { qguiapplication app(argc, argv); model model; model.addsomedata("data1"); model.addsomedata("data2"); model.addsomedata("data3"); qquickview view; view.setresizemode(qquickview::sizerootobjecttoview); qqmlcontext *ctxt = view.rootcontext(); ctxt->setcontextproperty("mymodel", &model); view.setsource(qurl("qrc:view.qml")); view.show(); return app.exec(); }
main.cpp
listview { width: 200; height: 250 model: mymodel delegate: text { text: "data " + data } mousearea { anchors.fill: parent cursorshape: qt.pointinghandcursor onclicked: { mymodel.move(0,2) //just test } } }
myqml.qml
if have idea of doing wrong, kind of me! thank much.
i assume model
implementation of qabstractitemmodel
. in case, should call qabstractitemmodel::beginmoverows
before start moving data , qabstractitemmodel::endmoverows
finalise it.
q_invokable void move(int oldindex, int newindex) { qmodelindex parent; beginmoverows(parent, oldindex, oldindex, parent, newindex); datalist.move(oldindex,newindex); endmoverows(); }
Comments
Post a Comment