[PyQt] Question about indexing a QAbstractItemModel

Darren Dale darren.dale at cornell.edu
Thu Dec 13 14:10:24 GMT 2007


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_())


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?


Thanks,
Darren



More information about the PyQt mailing list