I am creating a subclass QAbstractItemModelto display in QTreeView.
My function index()also parent()creates QModelIndexwith the help of the QAbstractItemModelinherited function createIndexand provides it row, columnand data. Here, for testing purposes, the data is a Python string.
class TestModel(QAbstractItemModel):
def __init__(self):
QAbstractItemModel.__init__(self)
def index(self, row, column, parent):
if parent.isValid():
return self.createIndex(row, column, "bar")
return self.createIndex(row, column, "foo")
def parent(self, index):
if index.isValid():
if index.data().data() == "bar": <--- NEVER TRUE
return self.createIndex(0, 0, "foo")
return QModelIndex()
def rowCount(self, index):
if index.isValid():
if index.data().data() == "bar": <--- NEVER TRUE
return 0
return 1
def columnCount(self, index):
return 1
def data(self, index, role):
if index.isValid():
return index.data().data() <--- CANNOT DO ANYTHING WITH IT
return "<None>"
Inside the functions index(), parent()and data()I need to return my data. He comes like QVariant. How to return a Python object from QVariant?
source
share