PDA

View Full Version : QList question



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.

wysota
29th January 2015, 16:14
A list keeps a copy of your original object.

As for your error, what is the type of tokens? Isn't it by any chance QVector? :) What does tokens.size() return?

DoomerDan
29th January 2015, 16:16
Sorry... I should've mentioned that.

line is a QString and tokens is a QStringList.

I'm reading a spreadsheet and splitting the line by the commas.

wysota
29th January 2015, 16:18
Why do you say the problem is with some slider? You are not running this code from a worker thread, are you?

DoomerDan
29th January 2015, 16:21
No. This code is in the UI thread.

That's what is confusing me. I can't figure out why the slider is crashing. It works fine before this code runs.
I've worked on this for two days now. My best guess is that some code or memory is getting corrupted somehow.

wysota
29th January 2015, 16:28
So if you comment out lines #17-26 then some other code stops crashing?

DoomerDan
29th January 2015, 16:36
I tried that. It still crashes.
If I comment out the lines dealing with the QList, then the other code stops crashing.
The line that crashes is...


ui->sldT->setValue(10);

Added after 4 minutes:

Also... it's only sldT. The other sliders work fine.

wysota
29th January 2015, 16:39
What is the size of the list (check, don't guess)?

DoomerDan
29th January 2015, 16:45
I found it. This is a slap myself in the face moment. In the event handler for the slider value change I was attempting to assign a value to a non-existent QVector element. :rolleyes:
The debugger stopped on the setValue line. If it had stopped in the event handler instead I wouldn't have wasted hours of work.

Thanks for your help wysota.