Seite 1 von 1

XML / TreeView

Verfasst: 14. März 2013 18:35
von usta
Hallo,

ich möchte eine Xml Datei einlesen und die NodeNamen im Treeview anzeigen (ohne Attribute) -das funktioniert auch so weit. Nun möchte ich, wenn ich auf ein Element klicke, die Attribute von diesem Element haben. Wie bekomme ich as am esten hin?

Hier mein Code:

Code: Alles auswählen


void Widget::openFileDialog(void)
{
    QFileDialog dialog;
    QString fileName = NULL;
    QDomDocument doc;
    QDomElement xmlRoot;
    QFile file;
    QStandardItem *root = new QStandardItem("ROOT");
    root->setEditable(false);

    model->appendRow(root);

    fileName = dialog.getOpenFileName(this, tr("Open Document"), QDir::currentPath(), tr("XML files (*.xml);;All files (*.*)") );
    if(fileName != NULL)
    {
        qDebug()<<"OpenFileName = " << fileName;
    }

    file.setFileName(fileName);
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"error: file.open(QIODevice::ReadOnly) = ";
        return;
    }
    if (!doc.setContent(&file))
    {
        qDebug()<<"error: doc.setContent(&file) = ";
        file.close();
        return;
    }
    file.close();

    xmlRoot = doc.firstChildElement();

    fillTree(xmlRoot.childNodes(), root );

    ui->treeView->setModel(model);

    connect(ui->treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(viewClicked(QModelIndex)));

}

void Widget::viewClicked(const QModelIndex &index)
 {
     QStandardItem *item = model->itemFromIndex(index);
     //qDebug()<< item->text();
 }

void Widget::fillTree(QDomNodeList nodeList, QStandardItem *parentItem)
{
    for(int i = 0; i<nodeList.count(); i++)
    {
        QDomElement element = nodeList.at(i).toElement();
        QStandardItem *item = new QStandardItem(element.nodeName());
        item->setEditable(false);
        parentItem->appendRow(item);
        fillTree(element.childNodes(), item );


    }
}

Vielen Dank im Voraus.