簡體   English   中英

如何在Qt中將Treeview保存為.csv文件?

[英]How to save a treeview as .csv file in Qt?

我需要將QTreeView的當前視圖(QSortFilterProxyModel和QStandardItemModel)另存為.csv文件。 不幸的是,我不知道從哪里開始。

我想我應該使用QSortFilterProxyModel,因為我需要保存當前視圖,但是我不確定...有什么想法嗎? 有沒有辦法知道某行是否是父母?

樹視圖是分層的,有5列:

在此處輸入圖片說明

.CSV輸出,我需要:

"(0028,2110)",LossyImageCo,CS,2,00
"(00028,3000)",ModalityLUTse,SQ,,
 ,"(0028,3002)",LUTDesciptoi,US,6,4096\0\12
etc...

到目前為止,我的代碼:

for (int i = 0; i < proxyModel->rowCount(); i++)
    {
        for (int j = 0; j < proxyModel->columnCount(); j++)
        {
            QModelIndex index = proxyModel->index(i, j);
            qDebug() << "Data: " << proxyModel->data(index).toString();
            // How do I know when to start a child row?
        }
    }

更新1:

我創建了一個包含所有父行的文件。 如何獲得子行?

QFile file(filePath);
            if (file.open(QFile::WriteOnly))
            {
                QTextStream stream(&file);              
                for (int i = 0; i < proxyModel->rowCount(); i++)
                {
                    QModelIndex index0 = proxyModel->index(i, 0);
                    QModelIndex index1 = proxyModel->index(i, 1);
                    QModelIndex index2 = proxyModel->index(i, 2);
                    QModelIndex index3 = proxyModel->index(i, 3);
                    QModelIndex index4 = proxyModel->index(i, 4);
                    stream << proxyModel->data(index0).toString() << "," <<  proxyModel->data(index1).toString() << "," << proxyModel->data(index2).toString() << "," << proxyModel->data(index3).toString() << "," << proxyModel->data(index4).toString();
                    stream << "\n";                 
                }
                file.close();
            }

輸出:

(0008,0005),SpecificCharacterSet,CS,10,ISO_IR 100
(0008,0008),ImageType,CS,36,ORIGINAL\PRIMARY\M\HEADER_CORRECTED
(0008,0016),SOPClassUID,UI,26,1.2.840.10008.5.1.4.1.1.4
(0008,0018),SOPInstanceUID,UI,46,1.3.12.2.1107.5.2.5.11090.5.0.5823661031981777
(0008,0020),StudyDate,DA,8,20040305
(0008,0021),SeriesDate,DA,8,20040305
(0008,0022),AcquisitionDate,DA,8,20040305
(0008,0023),ContentDate,DA,8,20040305
(0008,0030),StudyTime,TM,14,085922.859000
(0008,0031),SeriesTime,TM,14,090019.359000
(0008,0032),AcquisitionTime,TM,14,085939.762492
(0008,0033),ContentTime,TM,14,090021.062000
(0008,0050),AccessionNumber,SH,2,0
(0008,0060),Modality,CS,2,MR
(0008,0070),Manufacturer,LO,8,SIEMENS
(0008,0080),InstitutionName,LO,18,cJf7JCqV84P^te1az
(0008,0090),ReferringPhysicianName,PN,20,FLp8xklEDWOqavQWiJ9
(0008,1010),StationName,SH,8,unknown
(0008,1030),StudyDescription,LO,12,WRIST^RIGHT
(0008,103e),SeriesDescription,LO,18,SCOUT 3-PLANE RT.
(0008,1070),OperatorsName,PN,14,RIORDAN, JAMES

要迭代QTreeWidget子項,請使用QTreeWidget :: topLevelItem()訪問頂級項,然后遞歸使用QTreeWidgetItem :: child()遍歷低級子項及其子項。

您是自己加載這個dicom文件嗎? 您也可以查看gdcmdump的源代碼。 修改該代碼以輸出csv而不是其標准輸出。 那很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM