如何将 CSV 文件导入到 QTableWidget
我是一名学生程序员,我正在使用 Qt 开发一个 GUI 界面来工作,但我无法弄清楚应该如何使这个导入函数工作。如果你愿意的话,有些程序员作家会阻止。到目前为止,我有这样的代码:
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[]);
}
}
}
这里的问题是我不知道如何获取表中每个项目的设置文本以引用 rowData QStringList 中的下一个项目。我需要增加一个位置,但不能使用 int++,因为这最终会在 QStringList 中查找不存在的项目并导致分段错误。由于当前的 for 循环结构,我也无法使用 for 循环来限制此处位置字段中所需的数量。我只是想不出一个好的策略来解决这个问题。请仅留下建设性意见,因为我只对学习或完成这项任务感兴趣。感谢您的阅读!
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[]);
}
}
}
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要处理行结尾。
这意味着如果您仅检查 CSV 文件中的“\n”,您的代码可能不独立于平台。
如果我明白你想要做什么,你就必须在 for 循环中设置 rowData 。
那么,我会做什么:
我想这应该可以做到。
此致。
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.
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 :
I guess this should do it.
Best regards.