2014-03-25 82 views
0

我在treewidget中有兩列。文件和路徑。現在我想將這些項目轉儲到一個xml文件中。但是我面臨的問題是我無法提取物品。如何閱讀qtreeWidget中的列?

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 & 2是整個故事的精髓。

回答

0

我想,你可以寫這樣的:@vahancho :)它的正常工作

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

謝謝... –

+0

我目前面臨的下一個問題是如何更名單追加到XML文件,更新相同。 –