QAbstractItemModel/TreeView zeigt nichts an
Verfasst: 4. Juli 2010 12:37
Hallo,
versuche mich gerade an einer ersten Modelklasse in Qt, ich will einen Dokumentenbaum darstellen, alles eigentlich relativ einfach gestrickt.
Orientieren tue ich mich hierbei an diesem Beispiel:
http://doc.qt.nokia.com/4.6/itemviews-s ... model.html
Jedoch, jetzt wo alles implementiert ist, bleibt der TreeView leer.
Mein Code:
Da gibt es keine großen Unterschiede zu dem Qt Beispiel, getText gibt ein QString zurück, ich weiss nicht, ob QVariant(p->getText()); Evtl. fehl schlägt oder hier nicht korrekte Daten liefert.
Wo anders fülle ich dies dann mit Daten:
Insgesamt sieht das alles für mich korrekt aus, aber ich verstehe nicht, warum nichts angezeigt wird...
versuche mich gerade an einer ersten Modelklasse in Qt, ich will einen Dokumentenbaum darstellen, alles eigentlich relativ einfach gestrickt.
Orientieren tue ich mich hierbei an diesem Beispiel:
http://doc.qt.nokia.com/4.6/itemviews-s ... model.html
Jedoch, jetzt wo alles implementiert ist, bleibt der TreeView leer.
Mein Code:
Code: Alles auswählen
DocumentTreeModel::DocumentTreeModel(QObject *parent) :
QAbstractItemModel(parent)
{
//doc ist die Wurzel des Baumes
doc = new DocRoot("Root");
}
QModelIndex DocumentTreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
DocRoot* p = 0;
if(parent.isValid())
p = static_cast<DocRoot*>(parent.internalPointer());
else
p = doc;
DocRoot* childItem= p->getByIndex(row);// entweder 0 oder ein DocRoot aus dem Container
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex DocumentTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
DocRoot* p = static_cast<DocRoot*>(index.internalPointer());
DocRoot* par = p->getParent();
if(!par || par ==doc)
return QModelIndex();
return createIndex(par->row(), 0, par);
}
int DocumentTreeModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
if (!parent.isValid())
return doc->count();
return 2;
}
int DocumentTreeModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant DocumentTreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
DocRoot* p = static_cast<DocRoot*>(index.internalPointer());
if(p && p != doc)
return QVariant(p->getText());
return QVariant();
}
Qt::ItemFlags DocumentTreeModel::flags(const QModelIndex &index) const
void DocumentTreeModel::update()
{
emit dataChanged(createIndex(0,0,doc->getByIndex(0)),createIndex(doc->count()-1,0,doc->getByIndex(doc->count()-1)));
}Wo anders fülle ich dies dann mit Daten:
Code: Alles auswählen
Document::t_form* form = new Document::t_form;
form->first = str;
DocRoot* root = new TreeModelProxy<Document::t_form>(form,str);
root->addNode(new TreeModelProxy<DiagramScene>(form->second.second,"Landscape"));
root->addNode(new TreeModelProxy<DiagramScene>(form->second.first,"Portrait"));
//DocRoot* p = root->getByIndex(0)->getParent();
doctree.addNode(root);
doctree.update();
//ui->treeView->setModel(&doctree);