giblit
24th March 2013, 06:06
First of all, I couldnt figure out how to map or get an array of variable names basically I was trying like QFile file[i] where I would be like 1-3 and i wanted it to be QFile file1, QFile2, QFile3 same with QTextStream because I think if I had that working it would be a lot better.
Secondly with my Code below I can read in two files, but if I try and read in a 3rd I get an application failure and it wont even open the exe get this error message:
ASSERT failure in QLIST<T>::operator[]: "index out of range", file C:\QtSDK\Desktop\Qt\4.8.1\mingw\include/Qtcore/qlist.h, line 477. and two mesages saying Invalid parameter passed to C runtime.
my code is here PS: if i make my two loops i < 2 and comment out the other 3 files it works but I want to read all 5 =/
#include <QtGui>
#include "datelistdialog.h"
#include "abitem.h"
#include <sstream>
#include <iostream>
using namespace std;
DateListDialog::DateListDialog()
{
QStringList foods;
QStringList dates;
QStringList totalProteins;
QStringList totalFats;
QStringList totalCarbs;
QStringList labels;
labels << tr("Date") << tr("Food") << tr("Total Protein") << tr("Total Carb") << tr("Total Fat");
treeWidget = new QTreeWidget;
treeWidget->setColumnCount(5);
treeWidget->setHeaderLabels(labels);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(treeWidget);
setLayout(mainLayout);
for(int i = 0; i < 3; i++){
string filename = "/";
string type [] = {"date","food","totalProtein","totalCarb","totalFat"};
string number;
stringstream convert;
filename.append(type[i]);
convert << i;
number = convert.str();
filename.append(".txt");
QString filename2 = QString::fromStdString(filename);
QFile i(QDir::homePath() + filename2);
if (i.open(QIODevice::ReadOnly)) {
QTextStream input(&i);
input.setCodec("UTF-8");
while(!input.atEnd()){
if(filename == "/date.txt"){
dates << input.readLine();
} else if(filename == "/food.txt"){
foods << input.readLine();
} else if(filename == "/totalProtein.txt"){
totalProteins << input.readLine();
} else if(filename == "/totalCarb.txt"){
totalCarbs << in.readLine();
in.readLine();
} else if(filename == "/totalFat.txt"){
totalFats << in.readLine();
in.readLine();
}
}
}
}
for (int i = 0; i < 5; ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem;
//item->setText(0,"HELLO");
item->setText(0, dates[i]);
item->setText(1, foods[i]);
item->setText(2, totalProteins[i]);
item->setText(3, totalCarbs[i]);
item->setText(4, totalFats[i]);
treeWidget->addTopLevelItem(item);
}
}
Secondly with my Code below I can read in two files, but if I try and read in a 3rd I get an application failure and it wont even open the exe get this error message:
ASSERT failure in QLIST<T>::operator[]: "index out of range", file C:\QtSDK\Desktop\Qt\4.8.1\mingw\include/Qtcore/qlist.h, line 477. and two mesages saying Invalid parameter passed to C runtime.
my code is here PS: if i make my two loops i < 2 and comment out the other 3 files it works but I want to read all 5 =/
#include <QtGui>
#include "datelistdialog.h"
#include "abitem.h"
#include <sstream>
#include <iostream>
using namespace std;
DateListDialog::DateListDialog()
{
QStringList foods;
QStringList dates;
QStringList totalProteins;
QStringList totalFats;
QStringList totalCarbs;
QStringList labels;
labels << tr("Date") << tr("Food") << tr("Total Protein") << tr("Total Carb") << tr("Total Fat");
treeWidget = new QTreeWidget;
treeWidget->setColumnCount(5);
treeWidget->setHeaderLabels(labels);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(treeWidget);
setLayout(mainLayout);
for(int i = 0; i < 3; i++){
string filename = "/";
string type [] = {"date","food","totalProtein","totalCarb","totalFat"};
string number;
stringstream convert;
filename.append(type[i]);
convert << i;
number = convert.str();
filename.append(".txt");
QString filename2 = QString::fromStdString(filename);
QFile i(QDir::homePath() + filename2);
if (i.open(QIODevice::ReadOnly)) {
QTextStream input(&i);
input.setCodec("UTF-8");
while(!input.atEnd()){
if(filename == "/date.txt"){
dates << input.readLine();
} else if(filename == "/food.txt"){
foods << input.readLine();
} else if(filename == "/totalProtein.txt"){
totalProteins << input.readLine();
} else if(filename == "/totalCarb.txt"){
totalCarbs << in.readLine();
in.readLine();
} else if(filename == "/totalFat.txt"){
totalFats << in.readLine();
in.readLine();
}
}
}
}
for (int i = 0; i < 5; ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem;
//item->setText(0,"HELLO");
item->setText(0, dates[i]);
item->setText(1, foods[i]);
item->setText(2, totalProteins[i]);
item->setText(3, totalCarbs[i]);
item->setText(4, totalFats[i]);
treeWidget->addTopLevelItem(item);
}
}