c++ - sorting a QAbstractTableModel and expose it to qml -
i need create integration between qt
, qml
, qt
ask info server, create tablemodel
qt
, expose info qml
. i've reached point, i've sort info. i'm using qsortfilterproxymodel
, of examples i've found created widgets, , create tableview
qt
, when tried create qml
, there's not option "setsortingenabled"
, in qml
i've many objects no tableview
. have used order model->sort(1, qt:ascendingorder)
there not change in qml
screen
simmainwindow.cpp
#include "simmainwindow.h" #include "turndisplaytable.h" #include "turndisplayitem.h" #include <qtqml> #include <qgraphicsobject> #include <qdebug> #include <qdeclarativeview> #include <qquickview> #include <qqmlapplicationengine> #include <qtwidgets> #include <qmainwindow> #include <tablemodel.h> #include <qsortfilterproxymodel> simmainwindow::simmainwindow(int argc, char **argv): qguiapplication(argc, argv), view() { qdebug() << "begin"; /////////////////////////////// qmlregistertype<turndisplaytable>("turndisplaytable", 1,0, "turndisplaytable"); filesmodel= new turndisplaytable(); view.rootcontext()->setcontextproperty("idfilesmodel", filesmodel); proxymodel = new qsortfilterproxymodel(this); proxymodel->setsourcemodel(filesmodel); proxymodel->sort(1, qt::ascendingorder); view.rootcontext()->setcontextproperty("pmodel", proxymodel); view.setsource( qurl::fromlocalfile("./ui/main.qml") ); qobject::connect((qobject*)view.rootobject(), signal(refreshmsg()), this, slot(refreshhome())); qobject::connect((qobject*)view.rootobject(), signal(changemsg()), this, slot(changehome())); qobject::connect((qobject*)view.rootobject(), signal(sortmsg()), this, slot(sorthome())); qobject::connect((qobject*)view.rootobject(), signal(infomsg(qstring, qstring)), this, slot(infohome(qstring, qstring))); view.show(); qdebug() << "end"; /////////////////////////////// } simmainwindow::~simmainwindow() {} void simmainwindow::refreshhome() { qdebug() << "refresh"; qlist<turndisplayitem> list; qstring turn = "2"; qstring date = "septiembre"; turndisplayitem item(turn, date); list << item; turn = "4"; date = "mayo"; turndisplayitem item2(turn, date); list << item2; turn = "1"; date = "julio"; turndisplayitem item3(turn, date); list << item3; turn = "3"; date = "abril"; turndisplayitem item4(turn, date); list << item4; turn = "2"; date = "mayo"; turndisplayitem item5(turn, date); list << item5; filesmodel->setcurrencymap(list); proxymodel->setsourcemodel(filesmodel); proxymodel->sort(1, qt::ascendingorder); } void simmainwindow::changehome() { qdebug() << "cambio"; qlist<turndisplayitem> list; qstring turn = "3"; qstring date = "sep"; turndisplayitem item(turn, date); list << item; turn = "1"; date = "may"; turndisplayitem item2(turn, date); list << item2; turn = "2"; date = "jul"; turndisplayitem item3(turn, date); list << item3; turn = "6"; date = "abr"; turndisplayitem item4(turn, date); list << item4; turn = "9"; date = "oct"; turndisplayitem item5(turn, date); list << item5; filesmodel->setcurrencymap(list); } void simmainwindow::sorthome() { proxymodel->sort(0, qt::ascendingorder); view.rootcontext()->setcontextproperty("pmodel", proxymodel); qdebug()<< "sort..."; view.show(); } void simmainwindow::infohome(const qstring &turnh, const qstring &dateh) { qdebug() << "info"; qlist<turndisplayitem> list; qstring turn = turnh; qstring date = dateh; turndisplayitem item(turn, date); list << item; filesmodel->setcurrencymap(list); }
try this, works in case:
model model; qsortfilterproxymodel proxymodel; proxymodel.setsortrole(model::yourcustom_role_here); proxymodel.setsourcemodel(&model); proxymodel.setdynamicsortfilter(true); proxymodel.sort(0);
i suspect model not sorting because have set target role setting setsortrole(int role)
, appling void qsortfilterproxymodel::sort(int column, qt::sortorder order = qt::ascendingorder) [virtual]
.
i hope solve problem.
Comments
Post a Comment