qt - QTreeView very slow sorting -
sorting qtreeview qsortfilterproxymodel extremely slow (compared basic qtreewidget). what's wrong code? how can speed-up?
# coding: utf-8 import sys pyqt5.qtwidgets import (qtreeview, qabstractitemview, qmainwindow, qapplication) pyqt5.qtsql import (qsqldatabase, qsqlquery, qsqlquerymodel) pyqt5.qtcore import qsortfilterproxymodel class database: def __init__(self): self.db = qsqldatabase.adddatabase("qodbc") self.db.setdatabasename( r'driver={sql server native client 11.0};server={(localdb)\v11.0};') self.db.open() class model(qsqlquerymodel): def __init__(self, parent=none): super().__init__(parent) query = qsqlquery() query.prepare('select * companies name ?') query.bindvalue(0, '%elektro%') query.exec_() self.setquery(query) class treeview(qtreeview): def edit(self, index, trigger, event): if trigger == qabstractitemview.doubleclicked: return false return qtreeview.edit(self, index, trigger, event) class window(qmainwindow): def __init__(self): super().__init__() self.db = database() self.model = model() sortfiltermodel = qsortfilterproxymodel() sortfiltermodel.setsourcemodel(self.model) treeview = treeview(self) treeview.setmodel(sortfiltermodel) treeview.hidecolumn(0) treeview.hidecolumn(9) treeview.setindentation(0) treeview.setselectionmode(qabstractitemview.multiselection) treeview.setallcolumnsshowfocus(true) treeview.setsortingenabled(true) treeview.clicked.connect(self.row_id) self.setcentralwidget(treeview) def row_id(self, index): if index.isvalid(): id_ = index.sibling(index.row(), 0).data() print(id_) if __name__ == '__main__': app = qapplication(sys.argv) window = window() window.show() app.exec_()
some tips (from c++ perspective, missing )
qtreeview known being slower qtableview , consume lot of memory , using plain table model anyways, so, try qtableview.
also, in real tree hierarchical model hiding column 0 hide child items.
try using qsqltablemodel instead of qsqlquerymodel, has convenience setsort, setfilter methods , fetchs visible rows. faster.
Comments
Post a Comment