Qt acquired execution file path (the lower window and linux general), reading CSV data

System 1: ThinkPad T570, Windows10, QT5.12.2 (Qt Creater 4.8.2)
System 2: NVIDIA Tegra X2, Ubuntu 16.04LTS , QT5.5.1 (Qt Creater 3.5.1)

Qt Widget Application a new application program to realize the functions of: reading csvdata / CapRas_016_B.csv file (which is stored in the luminance data of type double) at the level of the path where the program execution path, and read QString to take the type of data converted to double
in mainwindow.cpp code as follows:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QList>
#include <QStringList>
#include <QTextStream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString     wstrFilePath;
    wstrFilePath = qApp->applicationDirPath() + "/../../csvdata/CapRas_016_B.csv" ;
    //qApp->applicationDirPath()获取到当前执行程序所在路径,   ../表示返回到上一个文件夹,在window下的路径比在linux下多一个debug文件夹(debug模式下)
    QString FILE_PATH(wstrFilePath);
    QFile csvFile(FILE_PATH);
    QStringList CSVList;
    CSVList.clear();
    if (csvFile.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&csvFile);
        while (!stream.atEnd())
        {
            CSVList.push_back(stream.readLine());
        }
        csvFile.close();
        int ncolumn,nrow;
        nrow = CSVList.size();
        ncolumn = nrow;
        if(!CSVList.empty())
        {
            QString strtemp = CSVList.first();
            QStringList qslisttemp = strtemp.split(",");
            ncolumn = qslisttemp.size();
            double *dCsvData = new double[ncolumn*nrow];
            //由于ncolumn*nrow个double达到12M,不能放在栈上面,所以不能通过double dCsvData[ncolumn*nrow];的方式申请内存,只能通过new的方式在堆上面申请
            for(int i = 0;i<nrow;i++)
            {
                QString str1 = CSVList.at(i);
                QStringList qslist = str1.split(",");
                for(int j = 0;j<ncolumn;j++)
                {
                    dCsvData[i*ncolumn+j] = qslist.at(j).toDouble();
                }
                qDebug()<<dCsvData[i*ncolumn];//输出每一行的第一个数据
            }
        }
            qDebug() <<"ncolumn = "<<ncolumn<< "nrow = "<<nrow<<"\n";//输出csv文件的列和行数
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

Guess you like

Origin blog.csdn.net/weixin_43935474/article/details/89003348