PDA

View Full Version : QStringList scope problem



ht1
30th November 2007, 20:01
Hi,

I have a QStringList that's declared under "private" of my subclass declaration.
I can do all the manipulations with this QStringList in the function where it was created but not in any other function (and these other functions are in the same subclass as the QStringList, and they are all public functions).

Why do I have this problem with the scope? Is there anything special about making a certain QStringList available to all functins within the same sublass?
Thanks.

marcel
30th November 2007, 20:14
How do you create the QStringList? If it is not a pointer then you don't need any instantiation mechanisms, just use it.

Gopala Krishna
30th November 2007, 20:16
can you show us some code ?

ht1
30th November 2007, 20:34
Thanks for your help. I'd be happy to show you my code:

My *.h file



class MyWidget : public QWidget
{
..........
public:
void function1();
void function2();

private:
QStringList list2;
..........
}


There's nothing in the constructor for the QStringList.
My main *.cpp file:



void MyWidget::function1(){
QStringList list; // declared and filled with data, everything OK here
.....................
i =4;
QStringList list2(list);
for (int j =0; j <list.size(); j++)
{
list2[j] = list[i];
i +=1;
if (i>=list.size()) i=0;
}

function2();
}

void MyWidget::function2(){
// contains my C code to test if the right thing happened
//nothing is written into the output file
// but it works if I put this code inside function1()
//so I know there are no logic or syntax error
FILE *my;
if ((my=fopen("output.txt","w")) == 0) {exit(1);}
char filename2[70];
for (int k =0; k <list2.size(); k++){
strncpy(filename2, list2[k].toLocal8Bit().constData(), 70);
fprintf(my, "%s\n", filename2);
}

fclose(my);
}

Gopala Krishna
30th November 2007, 20:39
The error you have is you declare stringlist with same name - one as class member and the other inside function1. Due to scoping rules of c++, your class member isn't modified instead only the one declared in function1 is modified.

Replace this line in function1

QStringList list2(list);

with

list2 = list;

ht1
30th November 2007, 20:44
Thank you Gopala Krishna !!