DoomerDan
29th January 2015, 16:04
Does the insertion operator make a copy of the value it is inserting?
For example... Lets say I have the following code...
struct someStruct {
int x;
double y;
};
QList<someStruct> testList;
void className::methodName()
{
someStruct tempStruct;
for (int x = 0; x < 10; x++) {
tempStruct.x = x;
tempStruct.y = 0;
testList <<tempStruct;
}
}
tempStruct is local to the "methodName" method.
After this function returns, is testList valid?
In my test code it is. All works ok. But I'm trying to track down a particularly elusive bug and this is one of my concerns.
Here's a snippet of my actual code. I'll try to explain the error.
LayerDef layer; // LayerDef is the struct definition
if (line.startsWith("Original")) {
while (!line.startsWith(',')) {
line = stream.readLine();
if (line.startsWith(',')){
continue;
}
tokens = line.split(',');
layer.t = tokens[1].toDouble(); // layer is a struct.
layer.dc = tokens[2].toDouble();
layer.dlt = tokens[3].toDouble();
layer.mlt = tokens[5].toDouble();
layer.dcc = tokens[6].toDouble();
layer.rp = tokens[7].toDouble();
layer.name = tokens[8];
layersOriginal <<layer; // layersOriginal is a QList (QList<LayerDef> layersOriginal;)
if (firstPass) {
firstPass = false;
// Update slider values on first pass only.
ui->sldT->setValue(layer.t); // Note this line.
ui->sldDC->setValue(layer.dc);
ui->sldDLT->setValue(layer.dlt);
ui->sldMLT->setValue(layer.mlt);
ui->sldDCC->setValue(layer.dcc);
ui->sldRP->setValue(layer.rp);
}
ui->lstLayers->addItem(layer.name);
}
This code runs ok. However, after it runs, any attempt to access sldT (a QSlider) causes an assertion failure.
The failure is "ASSERT failure in QVector<T>::operator[]: "index out of range", file ..\..\Qt\Qt5.3.2\5.3\mingw482_32\include/QtCore/qvector.h, line 385".
I've went through the code for QSlider and QAbstractSlider. I can't find any reference to a QVector in either of them.
This is my final step before I start banging my head on my desk.
For example... Lets say I have the following code...
struct someStruct {
int x;
double y;
};
QList<someStruct> testList;
void className::methodName()
{
someStruct tempStruct;
for (int x = 0; x < 10; x++) {
tempStruct.x = x;
tempStruct.y = 0;
testList <<tempStruct;
}
}
tempStruct is local to the "methodName" method.
After this function returns, is testList valid?
In my test code it is. All works ok. But I'm trying to track down a particularly elusive bug and this is one of my concerns.
Here's a snippet of my actual code. I'll try to explain the error.
LayerDef layer; // LayerDef is the struct definition
if (line.startsWith("Original")) {
while (!line.startsWith(',')) {
line = stream.readLine();
if (line.startsWith(',')){
continue;
}
tokens = line.split(',');
layer.t = tokens[1].toDouble(); // layer is a struct.
layer.dc = tokens[2].toDouble();
layer.dlt = tokens[3].toDouble();
layer.mlt = tokens[5].toDouble();
layer.dcc = tokens[6].toDouble();
layer.rp = tokens[7].toDouble();
layer.name = tokens[8];
layersOriginal <<layer; // layersOriginal is a QList (QList<LayerDef> layersOriginal;)
if (firstPass) {
firstPass = false;
// Update slider values on first pass only.
ui->sldT->setValue(layer.t); // Note this line.
ui->sldDC->setValue(layer.dc);
ui->sldDLT->setValue(layer.dlt);
ui->sldMLT->setValue(layer.mlt);
ui->sldDCC->setValue(layer.dcc);
ui->sldRP->setValue(layer.rp);
}
ui->lstLayers->addItem(layer.name);
}
This code runs ok. However, after it runs, any attempt to access sldT (a QSlider) causes an assertion failure.
The failure is "ASSERT failure in QVector<T>::operator[]: "index out of range", file ..\..\Qt\Qt5.3.2\5.3\mingw482_32\include/QtCore/qvector.h, line 385".
I've went through the code for QSlider and QAbstractSlider. I can't find any reference to a QVector in either of them.
This is my final step before I start banging my head on my desk.