Question

I have two columns in treewidget viz. Files and path. Now I want to dump the items into an xml file. But the issue I'm facing is that I'm unable to extract the items.

QDomDocument document;
QDomElement root = document.createElement("Playlist");
document.appendChild(root);

QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Save", "Do you want to save the playlist?", MessageBox::Yes | QMessageBox::No);

if(reply == QMessageBox::Yes)
{
    //Add some elements
        QDomElement playlist = document.createElement("MyPlaylist");
        playlist.setAttribute("Name", "Playlist1");
        root.appendChild(playlist);

        for(int h = 0; h < ui->treeWidget->topLevelItemCount(); h++)
        {
            QDomElement file = document.createElement("File");
            file.setAttribute("ID", ui->treeWidget->columnAt(0));     //1
            file.setAttribute("Path", ui->treeWidget->columnAt(1));   //2
            playlist.appendChild(file);
        }
}

Can anyone help me out how to handle such situation wherein multiple column items are to be read. Comments 1 & 2 are the essence of whole story.

Was it helpful?

Solution

I think, you can write it like:

[..]
for(int h = 0; h < ui->treeWidget->topLevelItemCount(); h++)
{
    QDomElement file = document.createElement("File");
    QTreeWidgetItem *item = ui->treeWidget->topLevelItem(h);
    file.setAttribute("ID", item->text(0));     //1
    file.setAttribute("Path", item->text(1));   //2
    playlist.appendChild(file);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top