PDA

View Full Version : Problem replacing a value in a QList.



bbad68
28th January 2010, 21:54
Hi,

I've been trying to implement a function into my program that loads values from a .txt file. In the program that I've made there is a QList<QList<double>> (ie a QList containing QLists' of double values). Each QList<double> represents a 'layer', while the double values contained within the each seperate list represent layer properties (ie layer area). When the user closes the program after manipulating these properties (through QDoubleSpinBoxes that I've included on my form), the values are saved into a .txt file. When the user starts up the program again, the previous values are all reloaded. Here is the code:


void ISocGenerator::getOldValues()
{
//inform user if old values file cannot be read from
QFile oldValuesFile("lastSettings");
if (!oldValuesFile.open(QIODevice::ReadOnly))
{
QMessageBox::information(this, tr("Unable to load previous values"),
oldValuesFile.errorString());
return;
}

//create a text stream to read with
QTextStream in(&oldValuesFile);

double theArray[4];

//get old number of layers
int listSize;
in >> listSize;

//get old X and Y dimensions
double xDim;
in >> xDim;
ui.xSpinBox->setValue(xDim);

double yDim;
in >> yDim;
ui.ySpinBox->setValue(yDim);

//load old values
for(int i = 0; i < listSize; i++)
{
//creates new lists for each loaded layer,
//(except for the first layer, since it was already created)
if(i != 0)
{
QList<double> newLayerData;
addNewLayer();
getNewLayerData(newLayerData);
}
//inserts the old values into the layer's list
for(int j = 0; j < 4; j++)
{
in >> theArray[j];

(_allData.value(i)).replace(j, theArray[j]);
}

//displayes values of first layer
if(i == 0)
{
ui.doubleSpinBox->setValue(theArray[0]);
ui.doubleSpinBox_2->setValue(theArray[1]);
ui.doubleSpinBox_3->setValue(theArray[2]);
ui.spinBox_4->setValue(theArray[3]);
}
}
}

An example of the .txt file from which they are loaded would be:

2 50 100 5000 4 1 7 3000 8 2 4

Where the first value is the list size, the next two can be ignored, the next 4 are the values of the first layer, and the last 4 are the values of the second layer.

The addNewLayer() function can be ignored.
The getNewLayerData() function inserts ten '0's into 'newLayerData'.

The first layer's values are all correctly loaded but the remaining layers all have zeros for their values. Line 46 is where I'm having the problem. It should be replacing the first 4 values of a given list, with the values that have been loaded into 'theArray' however it just replaces them with zeros. When I debug, it shows me that the values loaded into theArray are always correct, however it seems that the replacing function is inserting zeros rather than the values in theArray.

I'm still really new to programming in general, so I'm having trouble catching my error.

What am I doing wrong?

vishwajeet.dusane
29th January 2010, 07:11
Try this at 46


(_allData.value(i)).at(j) = theArray[j];

BTW what us _allData? is it list of list ?

Ginsengelf
29th January 2010, 07:17
Hi, I think the problem is that _allData.value(i) does not return a reference but a copy of the object at position i so you just change that copy and not the original data. Try the [] operator instead of the value() method.

Ginsengelf

aamer4yu
29th January 2010, 07:18
QList::value() and QList::at() functions return a copy.
Use [] operator instead to refer the actual item.

bbad68
29th January 2010, 17:14
Thanks guys, that worked perfectly!