python - Selecting many rows in Qt table -
i trying create qtableview
in qt efficient large tables. i've managed make display of data efficient defining own abstract table model:
from pyqt4 import qtcore, qtgui pyqt4.qtcore import qt class datatablemodel(qtcore.qabstracttablemodel): def columncount(self, index=none): return 3 def rowcount(self, index=none): return 10000 def headerdata(self, section, orientation, role): if role != qt.displayrole: return none if orientation == qt.horizontal: return 'c' elif orientation == qt.vertical: return 'r' def data(self, index, role): if not index.isvalid(): return none if role == qt.displayrole: return "({0},{1})".format(index.row(), index.column()) app = qtgui.qapplication([""]) viewer = qtgui.qtableview() model = datatablemodel() viewer.setmodel(model) viewer.show()
this works fine, because data
method called cells appear in field of view of table.
i want display existing selection of fraction of rows:
import numpy np selected_rows = np.where(np.random.random(10000) > 0.5)[0]
i can tell table widget selection doing e.g.:
smodel = viewer.selectionmodel() row in selected_rows: model_index = model.createindex(row, 0) smodel.select(model_index, qtgui.qitemselectionmodel.select | qtgui.qitemselectionmodel.rows)
however, inefficient. typically takes second select 1000-2000 rows, when in practice have tables millions of rows. there may ways of speeding loop, away loop altogether, , instead have qt ask me (similarly data itself) information selections within visible cells. possible, , if so, best way achieve this?
you should use second overloaded version of select
, 1 accepts qitemselection
instead of single index.
the qitemselection
able select ranges of rows providing 2 argument constructor:
qitemselection(start_index, stop_index)
moreover can merge
items become single selection:
selection.merge(other_selection, flags)
this suggest to:
- sort indices of rows want select
- use
itertools.groupby
group consecutive rows - use
createindex
qmodelindex
of start-end indices of these groups - create
qitemselection
objects each group of rows - merge
qitemselection
s singleqitemselection
- perform selection on model.
note want sort rows by index, not values.
Comments
Post a Comment