c++ - 如何在 Qt 中将 TreeView 保存为 .csv 文件?

标签 c++ qt file csv treeview

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

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

TreeView 是分层的,有 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

最佳答案

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

您是否自己加载此 dicom 文件?您还可以查看 gdcmdump 的源代码。修改该代码以输出 csv 而不是其标准输出。那将是直截了当的。

关于c++ - 如何在 Qt 中将 TreeView 保存为 .csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34992581/

相关文章:

c++ - 对大字符串进行更快的操作

c++ - 无法正确推回结构 C++ 中的 vector

c++ - Qt 使用 QNetworkReply 获取网络请求,将数据下载到临时文件

c - 如何从输入文件中提取信息并将它们放入 C 数组中?

c++ - 是 clrscr(); C++ 中的函数?

c++ - ODBC with oracle,绑定(bind)数字字段

c++ - 生成MITK教程错误

c++ - Qt - 禁用 QDialog 的 "?"按钮

java - 在 Java 中将文件从一个目录复制到另一个目录

python - python多久刷新一次文件?