如何在Qt中将树视图保存为.csv文件?

时间:2016-01-25 12:25:01

标签: c++ qt file csv treeview

我需要将QTreeView(QSortFilterProxyModel和QStandardItemModel)的当前视图保存为.csv文件。不幸的是,我不知道从哪里开始......

我想我应该使用QSortFilterProxyModel,因为我需要保存当前视图,但我不确定......有什么想法吗?有没有办法知道一行是否是父亲?

树视图是层次结构,有5列:

enter image description here

.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

1 个答案:

答案 0 :(得分:1)

要迭代QTreeWidget子项,请使用QTreeWidget :: topLevelItem()访问顶级项,然后递归使用QTreeWidgetItem :: child()迭代较低级别的子级及其子级。

你自己加载这个dicom文件吗?您还可以查看gdcmdump的源代码。修改该代码以输出csv而不是其标准输出。那很简单。