如何将CSV文件导入QTableWidget [英] How to import a CSV file to a QTableWidget

查看:726
本文介绍了如何将CSV文件导入QTableWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是学生程序员,我使用Qt开发一个GUI界面的工作和Im有麻烦了解如何使这个导入功能工作。有一点程序员写作者阻止如果你愿意。到目前为止我有这个代码:

I am student programmer and I am using Qt to develop a GUI interface for work and Im having trouble figuring out how I should make this this import function work. A bit of programmers writers block if you will. SO far I have this for code:

void InjectionLocationsDialogExpanded::importCSVFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), "/home", ("csv File(*.csv)"));
    QString data;
    QFile importedCSV(fileName);
    QStringList rowOfData;
    QStringList rowData;
    int tempint = 0;
    data.clear();
    rowOfData.clear();
    rowData.clear();
    if (importedCSV.open(QFile::ReadOnly))
    {
        data = importedCSV.readAll();
        rowOfData = data.split("\n");
        rowData = data.split(";");
        importedCSV.close();
    }
    qDebug() << data;
    for (int x = 0; x < rowOfData.size(); x++)
    {
        for (int y = 0; y < ui->tableWidgetInjectionLocationsExpandedDialog->columnCount(); y++)
        {
            ui->tableWidgetInjectionLocationsExpandedDialog->item(x,y)->setText(rowData[]);
        }
    }
}

不知道如何获取表中每个项的settext来引用rowData QStringList中的下一个项。我需要增加一个位置,但我不能使用一个int ++,因为这将最终寻找一个项目在QStringList中不存在,并导致分段错误。我也不能使用for循环来覆盖我需要的位置字段在这里,因为电流for循环结构。我只是不能想到一个好的策略,以解决这个问题。请只留下建设性的意见,因为我只对学习或完成这项任务感兴趣。感谢您阅读!

The issue here is that I don't know how to get the settext of each item in the table to reference the next item in the rowData QStringList. I need to increment by one location but I cant use a int++ because that will eventually look for a item in the QStringList that doesn't exist and cause a segmentation fault. I also cant use a for loop to cap the number I need in the location field here because of the current for loops structure. I just cant think of a good strategy for to approach this on. Please only leave constructive comments as I am only interested in learning or accomplishing this task. Thanks for reading!

推荐答案

首先,你需要照顾行尾。
这意味着如果你只是检查你的CSV文件中的\\\
,你的代码可能不会与平台无关。

First of all, you would need to take care of line endings. This means that your code might not be platform independent if you just check for "\n" in your CSV file.

重试尝试,你必须在for-loop中设置rowData。
那么,我会这样做:

If I get what you're trying to do, you would have to set the rowData within the for-loop. So, what I would do then :

rowOfData.clear();
rowData.clear();

if (importedCSV.open(QFile::ReadOnly))
{
    data = importedCSV.readAll();
    rowOfData = data.split("\n");
    importedCSV.close();
}

for (int x = 0; x < rowOfData.size(); x++)
{
    rowData = rowOfData.at(x).split(";");
    for (int y = 0; y < rowData.size(); y++)
    {
        ui->tableWidgetInjectionLocationsExpandedDialog->item(x,y)->setText(rowData[y]);
    }
}



我想这应该做。
最好的问候。

I guess this should do it. Best regards.

这篇关于如何将CSV文件导入QTableWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆