如何读取qtreeWidget中的列?

时间:2014-03-25 05:32:21

标签: qt qtreewidget

我在treewidget中有两列。文件和路径。现在我想将项目转储到xml文件中。但我面临的问题是我无法提取这些项目。

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);
        }
}

任何人都可以帮我解决如何处理需要读取多个列项的情况。评论1&amp; 2是整个故事的精髓。

1 个答案:

答案 0 :(得分:0)

我想,你可以这样写:

[..]
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);
}