[PyQt] Question about indexing a QAbstractItemModel

Darren Dale darren.dale at cornell.edu
Thu Dec 13 14:40:32 GMT 2007


On Thursday 13 December 2007 09:10:24 am Darren Dale wrote:
> I am trying to figure out how to append an item to a model derived from
> QAbstractItemModel, and have the appended item appear expanded by default,
> but I don't understand how to work with QModelIndex. Could someone suggest
> how simpletreemodel.py could be modified to expand the last parent item
> (Connection Editing Mode) before the widget is rendered? The following
> doesn't work:
>
> if __name__ == "__main__":
>     app = QtGui.QApplication(sys.argv)
>
>     f = QtCore.QFile(":/default.txt")
>     f.open(QtCore.QIODevice.ReadOnly)
>     model = TreeModel(QtCore.QString(f.readAll()))
>     f.close()
>
>     view = QtGui.QTreeView()
>     view.setModel(model)
>     view.setWindowTitle("Simple Tree Model")
>     index = model.index(model.rowCount(QtCore.QModelIndex()),
>                   0, QtCore.QModelIndex())
>     view.expand(index)
>     view.show()
>     sys.exit(app.exec_())

Apparently, I needed to work on this for 4 hours and 5 minutes, instead of 
posting after only 4 hours. Here is the correct code:

index = model.index(model.rowCount(QtCore.QModelIndex())-1,
                    0, QtCore.QModelIndex())

> Also, can anyone point me to an example of how to remove an item using the
> GUI, say by using the delete key or a context menu?

Any guidance on this second question would still be appreciated.

Thanks,
Darren


More information about the PyQt mailing list