PDA

View Full Version : Loading CSV file on start of application



akshaysulakhe
16th July 2013, 08:59
Hello friends,
I am importing my language CSV file in Table widget. I don't want to wait for the open action, what i want is to load the CSV file as soon as i start the main application. Here is my CSV file processing code. Kindly let me know what to do, or atleast a few pointers. Thank you very much.

QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("Language(*.csv)"));
QString data;
QFile importedCSV(fileName);
QStringList rowOfData;
QStringList rowData;
data.clear();
rowOfData.clear();
rowData.clear();

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

for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row
{
rowData = rowOfData.at(x).split(";"); //Number of columns
// int r=rowData.size();
for (int y = 0; y < rowData.size(); y++)
{
mUI.languageTable->item(x,y)->setText(rowData[y]);

}
}

Santosh Reddy
16th July 2013, 09:02
//QString fileName = QFileDialog::getOpenFileName(this, ("Open File"), NULL, ("Language(*.csv)"));
fileName = "English.csv"; //<<<<<<<<<<<<<<<<<
QString data;
QFile importedCSV(fileName);
QStringList rowOfData;
QStringList rowData;
data.clear();
rowOfData.clear();
rowData.clear();

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

for (int x = 0; x < rowOfData.size(); x++) //rowOfData.size() gives the number of row
{
rowData = rowOfData.at(x).split(";"); //Number of columns
// int r=rowData.size();
for (int y = 0; y < rowData.size(); y++)
{
mUI.languageTable->item(x,y)->setText(rowData[y]);

}
}

akshaysulakhe
16th July 2013, 09:14
Hello Santosh,
Thank you for the changes, but commenting the 1st line you mentioned, it gave me an error. So i declared the filename as QString. But it didnt work. My main question is how to call this function, so it can automatically look for the CSV file which is already in the qt project folder, from where i am working. Something similar to a qrc file. Thank you for your time.

Santosh Reddy
16th July 2013, 09:22
My main question is how to call this function, so it can automatically look for the CSV file which is already in the qt project folder, from where i am working. Something similar to a qrc file.
If you want to get the file name automatically you need not call QFileDialog::getOpenFileName(), this is needed only when operator/user has to select the file. If you automatically need the then use QDir::entryInfoList() get all the files in the directory and pick one from it.

akshaysulakhe
16th July 2013, 09:34
Thank you Santosh once again. Just one last question, when & where do i call the function which is handling all this code. I managed to get the entryInfoList operational, now it is only looking for csv files in my directory, but because i am not calling the function anywhere, its not executing. The function takes no parameters and does not return anything. Thank you so much for your time.

Santosh Reddy
16th July 2013, 09:44
It is trival to call instead of QFileDialog::getOpenFileName()