Tworzę podklasę o numerze QAbstractItemModel
, która będzie wyświetlana w postaci QTreeView
.Jak odzyskać obiekt Pythona z QVariant w PyQt4?
My index()
i parent()
funkcja tworzy QModelIndex
pomocą QAbstractItemModel
odziedziczonej funkcji createIndex
i zapewniając, że jest to row
, column
i data
potrzebne. Tutaj, w celach testowych, dane są ciągiem w języku Python.
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>"
ciągu index()
, parent()
i data()
funkcji muszę dostać moje dane z powrotem. Występuje jako QVariant
. Jak mogę odzyskać mój obiekt Python z QVariant?
Ta odpowiedź jest bardziej zgodna z początkowym pytaniem. Pomogło mi to dzięki – Craig