Results 1 to 6 of 6

Thread: QStringList scope problem

  1. #1
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default QStringList scope problem

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringList scope problem

    How do you create the QStringList? If it is not a pointer then you don't need any instantiation mechanisms, just use it.

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QStringList scope problem

    can you show us some code ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #4
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QStringList scope problem

    Thanks for your help. I'd be happy to show you my code:

    My *.h file

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. ..........
    4. public:
    5. void function1();
    6. void function2();
    7.  
    8. private:
    9. QStringList list2;
    10. ..........
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void MyWidget::function1(){
    2. QStringList list; // declared and filled with data, everything OK here
    3. .....................
    4. i =4;
    5. QStringList list2(list);
    6. for (int j =0; j <list.size(); j++)
    7. {
    8. list2[j] = list[i];
    9. i +=1;
    10. if (i>=list.size()) i=0;
    11. }
    12.  
    13. function2();
    14. }
    15.  
    16. void MyWidget::function2(){
    17. // contains my C code to test if the right thing happened
    18. //nothing is written into the output file
    19. // but it works if I put this code inside function1()
    20. //so I know there are no logic or syntax error
    21. FILE *my;
    22. if ((my=fopen("output.txt","w")) == 0) {exit(1);}
    23. char filename2[70];
    24. for (int k =0; k <list2.size(); k++){
    25. strncpy(filename2, list2[k].toLocal8Bit().constData(), 70);
    26. fprintf(my, "%s\n", filename2);
    27. }
    28.  
    29. fclose(my);
    30. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QStringList scope problem

    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
    Qt Code:
    1. QStringList list2(list);
    To copy to clipboard, switch view to plain text mode 

    with
    Qt Code:
    1. list2 = list;
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. The following user says thank you to Gopala Krishna for this useful post:

    ht1 (30th November 2007)

  7. #6
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Thanks!

    Thank you Gopala Krishna !!

Similar Threads

  1. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  2. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  3. QT4 scope problem
    By the_bis in forum Newbie
    Replies: 5
    Last Post: 29th January 2007, 23:01
  4. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  5. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.