c++ - Strange interaction of QAbstractItemModel with QSortFilterProxyModel -
what wrong interaction of qabstractitemmodel qsortfilterproxymodel? on left part of screen connected implementation of qabstractitemmodel , on right part part did qsortfilterproxymodel.
upd: upload code: git@bitbucket.org:h0x0d9/myfilterproxymodel.git
code:
listsregistermodel *model = new listsregistermodel(this); qsortfilterproxymodel *proxymodel = new qsortfilterproxymodel(this); proxymodel->setsourcemodel(model); qtreeview *view = new qtreeview(this); view->setmodel(model); qtreeview *view2 = new qtreeview(this); view2->setmodel(proxymodel);
headers:
class listsregistermodel : public qabstractitemmodel { q_object public: explicit listsregistermodel(qobject *parent = 0); ~listsregistermodel(); qvariant data(const qmodelindex &index, int role) const; qt::itemflags flags(const qmodelindex &index) const; qvariant headerdata(int section, qt::orientation orientation, int role = qt::displayrole) const; qmodelindex index(int row, int column, const qmodelindex &parent = qmodelindex()) const; qmodelindex parent(const qmodelindex &index) const; int rowcount(const qmodelindex &parent = qmodelindex()) const; int columncount(const qmodelindex &parent = qmodelindex()) const; bool setdata(const qmodelindex &index, const qvariant &value, int role = qt::checkstaterole); private: enum columns { ramificationcolumn, listnamecolumn = ramificationcolumn, listsubscribercolumn }; treeitem *rootitem; qmultimap<qstring, qstring> parselistsfile(qstring path); void setupmodeldata(qmultimap<qstring, qstring> lists, treeitem *parent); treeitem *getitem(const qmodelindex &index) const; }; listsregistermodel::listsregistermodel(qobject *parent) : qabstractitemmodel(parent) { qlist<qvariant> rootdata; rootdata << tr("list") << tr("subscribers"); rootitem = new treeitem(rootdata); qmultimap<qstring, qstring> listsreg = parselistsfile("etc/lists.reg"); setupmodeldata(listsreg, rootitem); } qvariant listsregistermodel::data(const qmodelindex &index, int role) const { if (!index.isvalid()) return qvariant(); treeitem *item = getitem(index); switch (role) { case qt::displayrole: if (item->parent() == rootitem) { return item->data(index.column()); } else { qdebug() << item->data(index.column() - 1); return item->data(index.column() - 1); } break; case qt::checkstaterole: if (index.column() == ramificationcolumn && item->parent() == rootitem) { return qvariant(item->checkstate()); } break; default: return qvariant(); break; } return qvariant(); } qmodelindex listsregistermodel::index(int row, int column, const qmodelindex &parent) const { treeitem * parentitem; if (row < 0 || column < 0) return qmodelindex(); if (!parent.isvalid()) parentitem = rootitem; else parentitem = getitem(parent); treeitem *childitem = parentitem->child(row); if (childitem) return createindex(row, column, childitem); return qmodelindex(); } qmodelindex listsregistermodel::parent(const qmodelindex &index) const { if (!index.isvalid()) return qmodelindex(); treeitem *childitem = getitem(index); treeitem *parentitem = childitem->parent(); if (parentitem == rootitem) return qmodelindex(); return createindex(parentitem->row(), 0, parentitem); }
i'm sprinkling ashes upon head. defined falsely columncount() function. correct version this:
int listsregistermodel::columncount(const qmodelindex &parent) const { return lastcolumn; // = 2 }
Comments
Post a Comment