python - spanning multiple columns, using QTreeView and QAbstractItemModel -


i want make treeview top level entries span columns (e.g. have 1 row), while children span multiple columns (e.g. have multiple rows.)

i trying accomplish qtreeview.setfirstcolumnspanned. however, can't figure out where/when call (and maybe i'm going things wrong begin with.)

i thought maybe make function in view called after view populated, check items need have spans updated.

however, couldn't find signals connect might me (i tried several, none of them ever seemed fire.)

i tried reimplementing insertrows in model and/or rowsinserted in view, never seem called.

here's extremely simplified version of i'm doing:

class mymodel(qtcore.qabstractitemmodel):     def __init__(self, top_level_nodes):         qtcore.qabstractitemmodel.__init__(self)         self.top_level_nodes = top_level_nodes         self.columns = 5      def columncount(self, parent):         return self.columns      def rowcount(self, parent):         total = len(self.top_level_nodes)         node in self.top_level_nodes:             total += len(node.subnodes)         return total      def data(self, index, role):          if not index.isvalid():             return qtcore.qvariant()          if role == qtcore.qt.displayrole:             obj = index.internalpointer()             return obj.name          return qtcore.qvariant()      def index(self, row, column, parent):         if not parent.isvalid():             if row > (len(self.top_level_nodes) - 1):                 return qtcore.qmodelindex()              return self.createindex(row, column, self.top_level_nodes[row])          return qtcore.qmodelindex()      def parent(self, index):         if not index.isvalid():             return qtcore.qmodelindex()          node = index.internalpointer()         if node.parent none:             return qtcore.qmodelindex()          else:             return self.createindex(node.parent.row, 0, node.parent)  class fakeentry(object):     def __init__(self, name, row, children=[]):         self.parent = none         self.row = row         self.name = 'foo'         self.subnodes = children  class mainwindow(qtgui.qwidget):     def __init__(self):         qtgui.qwidget.__init__(self)          self.resize(800, 600)          self.layout = qtgui.qvboxlayout(self)         self.tree_view = qtgui.qtreeview(self)         self.layout.addwidget(self.tree_view)          entry = fakeentry('foo', 0, children=[])         self.model = mymodel([entry])         self.tree_view.setmodel(self.model)  def main():     app = qtgui.qapplication(sys.argv)     frame = mainwindow()     frame.show()     sys.exit(app.exec_())  if __name__ == '__main__':     main() 

this code runs fine. need know is:

how can make entry created fakeentry span 5 columns instead of creating 5 identical cells?

other questions:

  • can reimplement qabstractitemmodel.insertrows or qtreeview.rowsinserted? there "trick" doing i'm missing, causing code pick default implementation?
  • what handles creating rows/columns in view, once model populated data , attached view?

this works me if add call setfirstcolumnspanned after setmodel() call in mainwindow.__init__().

class mainwindow(qtgui.qwidget):     def __init__(self):         qtgui.qwidget.__init__(self)          self.resize(800, 600)          self.layout = qtgui.qvboxlayout(self)         self.tree_view = qtgui.qtreeview(self)         self.layout.addwidget(self.tree_view)          entry = fakeentry('foo', 0, children=[])         self.model = mymodel([entry])         self.tree_view.setmodel(self.model)         self.tree_view.setfirstcolumnspanned(0, qtcore.qmodelindex(), true) 

i believe view keeps track of spanned columns internally, separate model. reset span information whenever setmodel() called. so, should call setfirstcolumnspanned() after calling setmodel() on view. if model doing lazy loading fetchmore(), you'll have work out way inform view should update spans after model has loaded new data.

wrt hadnles creating rows/columns in view, think partnership between model , view. once view has model set, begins asking various questions model. top-most parent item (an invalid index), how many columns there? cool, there's 5. how many rows there? ok, 1. information view's header can start configure itself. @ point, view iterates on rows, columns, , children in model, , asks each index it's data in turn, each data role. can start create delegates display data in appropriate spot in view.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -