qt读取csv数据画图_通过Qt中的csv文件解析

bd96500e110b49cbb3cd949968f18be7.png

Is anyone familiar with how to parse through a csv file and put it inside a string list. Right now I am taking the entire csv file and putting into the string list. I am trying to figure out if there is a way to get only the first column.

#include "searchwindow.h"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

QWidget *widget = new QWidget();

QHBoxLayout *layout = new QHBoxLayout();

QStringList wordList;

QFile f("FlightParam.csv");

if (f.open(QIODevice::ReadOnly))

{

//file opened successfully

QString data;

data = f.readAll();

wordList = data.split(',');

f.close();

}

QLabel *label = new QLabel("Select");

QLineEdit *lineEdit = new QLineEdit;

label->setBuddy(lineEdit);

QCompleter *completer = new QCompleter(wordList);

completer->setCaseSensitivity(Qt::CaseInsensitive); //Make caseInsensitive selection

lineEdit->setCompleter(completer);

layout->addWidget(label);

layout->addWidget(lineEdit);

widget->setLayout(layout);

widget->showMaximized();

return a.exec();

}

解决方案

There you go:

FlightParam.csv

1,2,3,

4,5,6,

7,8,9,

main.cpp

#include

#include

#include

int main()

{

QFile file("FlightParam.csv");

if (!file.open(QIODevice::ReadOnly)) {

qDebug() << file.errorString();

return 1;

}

QStringList wordList;

while (!file.atEnd()) {

QByteArray line = file.readLine();

wordList.append(line.split(',').first());

}

qDebug() << wordList;

return 0;

}

main.pro

TEMPLATE = app

TARGET = main

QT = core

SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

("1", "4", "7")

Qt打开CSV文件读取解析数据并通过串口发送,一般需要完成以下步骤: 1. **打开CSV文件**: 使用Qt文件操作类QFile来打开CSV文件。首先需要构建文件的路径,然后创建并打开QFile对象。如果文件打开成功,则可以继续读取数据。 2. **读取解析数据**: CSV文件通常以逗号、分号或其他特定字符分隔数据。可以使用QTextStream或QFile配合QString的split函数来读取解析数据行。逐行读取CSV文件,并将每行按照分隔符分割成多个数据项。 3. **通过串口发送数据**: 使用Qt的串口通信类QSerialPort进行数据的发送。首先需要实例化一个QSerialPort对象,并设置好串口的相关参数(如波特率、数据位、停止位、校验位等)。然后打开串口,将解析后的数据转换为字节数据(例如使用QByteArray),通过QSerialPort的write函数发送出去。 以下是一个简化的代码示例来说明这个过程: ```cpp #include <QFile> #include <QTextStream> #include <QSerialPort> #include <QSerialPortInfo> // 打开CSV文件读取数据 bool openAndReadCsv(const QString &filePath) { QFile file(filePath); if (!file.open(QIODevice::ReadOnly)) { return false; // 打开失败 } QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); QStringList dataItems = line.split(','); // 假设以逗号分隔 // 处理每一行的数据 } return true; } // 通过串口发送数据 bool sendDataViaSerialPort(QByteArray data) { QSerialPort serialPort; serialPort.setPortName("COM1"); // 设置串口名称 serialPort.setBaudRate(QSerialPort::Baud9600); // 设置波特率 // ... 设置其他串口参数 ... if (!serialPort.open(QIODevice::WriteOnly)) { return false; // 打开串口失败 } serialPort.write(data); // 发送数据 serialPort.close(); // 关闭串口 return true; } int main() { QString csvFilePath = "/path/to/your/csvfile.csv"; if (!openAndReadCsv(csvFilePath)) { return -1; // 读取CSV文件失败 } // 假设已解析好的数据 QByteArray dataToSend = "Hello, World!"; if (!sendDataViaSerialPort(dataToSend)) { return -1; // 通过串口发送数据失败 } return 0; } ``` 注意:上述代码仅为示例,实际应用需要根据具体需求进行错误处理和异常管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值