PDA

View Full Version : Read XML into QtreeWidget



SpanchBob
24th July 2012, 06:51
Hello!

I have a next code:


void MainWindow::openFile()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"),
xmlPath, tr("XML files (*.xml);;HTML files (*.html);;"
"SVG files (*.svg);;User Interface files (*.ui)"));

QStandardItem *root = new QStandardItem ("Main");
root->setIcon(QIcon("D:/note.png"));
model->appendRow(root);


QDomDocument document;
if (!filePath.isEmpty()) {
QFile file(filePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{

document.setContent(&file);
file.close();
}

//read root element
QDomElement xmlroot = document.firstChildElement();

//считываем childitems
QDomNodeList books = xmlroot.elementsByTagName("entry");

for (int i = 0; i < 1; i++)
{
QDomElement book = books.at(i).toElement();
QStandardItem *bookitem = new QStandardItem(book.attribute("term"));
bookitem->setIcon(QIcon("pic.png"));




QDomNodeList ne_chapters = book.elementsByTagName("entry");
for (int h = 0; h < 1; h++)
{
QDomElement ne_chapter = ne_chapters.at(h).toElement();
QStandardItem *ne_chapteritem = new QStandardItem(ne_chapter.attribute("term"));
ne_chapteritem->setIcon(QIcon("D:/pic2.png"));
bookitem->appendRow(ne_chapteritem);

}

//считываем подпункты
QDomNodeList chapters = book.elementsByTagName("entry");
for (int h = 1; h < chapters.count()-1; h++)//select all only end item
{
QDomElement chapter = chapters.at(h).toElement();
QStandardItem *chapteritem = new QStandardItem(chapter.attribute("term"));
chapteritem->setIcon(QIcon("D:/pic3.png"));

bookitem->appendRow(chapteritem);

}

for (int s = chapters.count()-1; s < chapters.count(); s++)//end item
{
QDomElement chapter = chapters.at(s).toElement();
QStandardItem *chapteritem = new QStandardItem(chapter.attribute("term"));
chapteritem->setIcon(QIcon("pic.png"));
bookitem->appendRow(chapteritem);

}

root->appendRow(bookitem);

}

}

}


in start code:


model = new QStandardItemModel (0,1,this);//0 - row; 1 column;

ui->treeView->setModel(model);


This is working so good, but i want used QtreeWidget instead TreeView. How can i do it in this code or i must rewriting this?
Sorry for my bad english :(
Thanks

MrShahi
24th July 2012, 07:08
hi ,
you can use member function of QStandardItemModel , i.e QModelIndex QModelIndex::sibling ( int row, int column ) const ,to get QModelIndex and extract data from that model index and create QtreeWidgetItem and add it to QTreeWidget.
I hope it will helpful to you.

wysota
24th July 2012, 07:23
Instead of creating standard items, create treewidget items. However think twice if you really want to do this, since the model approach gives much more flexibility than the item approach and with QStandardItemModel the API for accessing data is almost the same as in QTreeWidget.

SpanchBob
25th July 2012, 10:54
hi ,
you can use member function of QStandardItemModel , i.e QModelIndex QModelIndex::sibling ( int row, int column ) const ,to get QModelIndex and extract data from that model index and create QtreeWidgetItem and add it to QTreeWidget.
I hope it will helpful to you.

Thank you, but my knowledge QT only two month. I will be grateful if you writing litle code for example with ModelIndex and TreeWidget.