Results 1 to 5 of 5

Thread: problem with QList

  1. #1

    Default problem with QList

    Hi guys i've created two classes one called
    circle

    Qt Code:
    1. class circle
    2. {
    3. public:
    4. circle();
    5. QString name ;
    6. int id ;
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 


    and another use this


    Qt Code:
    1. class soso
    2. {
    3. public:
    4. soso();
    5. QList<circle*> lis;
    6. void go();
    7. };
    To copy to clipboard, switch view to plain text mode 


    in the constructor of soso i've just added two circle


    Qt Code:
    1. soso::soso()
    2. {
    3. circle* c1 = new circle();
    4. circle* c2= new circle();
    5. c1->id=1;
    6. c1->name="gimy";
    7. c2->id=2;
    8. c2->name="susana";
    9. lis.append(c1);
    10. lis.append(c2);
    11. }
    To copy to clipboard, switch view to plain text mode 


    and in the main window i've called go method which is included here
    Qt Code:
    1. void soso::go()
    2. {
    3.  
    4. QFile file("database.txt");
    5. if(!file.open(QIODevice::WriteOnly))
    6. throw " cannot open file ! ";
    7. QDataStream out(&file);
    8. int i=0;
    9. QList<circle*>::iterator it1 =lis.begin();
    10.  
    11. for(i=0;it1!=lis.end();it1++);
    12. {
    13. out<<(*it1)->id; // segmentation error in the runtime
    14. out<<(*it1)->name;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 


    but i am getting segmentation error any body know why ?
    Last edited by yamen ajjour; 21st August 2011 at 15:30.

  2. #2
    Join Date
    Aug 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with QList

    your loop must be like these line

    Qt Code:
    1. for(i=0;it1!=lis.end();it1++);
    2. {
    3. out<<(*it1).id;
    4. out<<(*it1).name;
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3

    Default Re: problem with QList

    Quote Originally Posted by softghost View Post
    your loop must be like these line

    Qt Code:
    1. for(i=0;it1!=lis.end();it1++);
    2. {
    3. out<<(*it1).id;
    4. out<<(*it1).name;
    5. }
    To copy to clipboard, switch view to plain text mode 
    i don't think so mate

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: problem with QList

    I couldn't get the for loop to work either, but the following seems to work fine:
    Qt Code:
    1. QList<circle* >::iterator it1=lis.begin();
    2. while(it1!=lis.end())
    3. {
    4. out << (*it1)->id << (*it1)->name;
    5. ++it1;
    6. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: The following worked after I ran make clean and qmake:
    Qt Code:
    1. QList<circle* >::iterator it1;
    2. for(it1=lis.begin();it1!=lis.end();++it1) {
    3. out<< (*it1)->id << (*it1)->name;
    4. }
    To copy to clipboard, switch view to plain text mode 

    or you could do away with the iterator and use:
    Qt Code:
    1. foreach(circle *c, lis) out << c->id << c->name;
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 21st August 2011 at 23:04.

  5. #5

    Default Re: problem with QList

    Quote Originally Posted by norobro View Post
    I couldn't get the for loop to work either, but the following seems to work fine:
    Qt Code:
    1. QList<circle* >::iterator it1=lis.begin();
    2. while(it1!=lis.end())
    3. {
    4. out << (*it1)->id << (*it1)->name;
    5. ++it1;
    6. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: The following worked after I ran make clean and qmake:
    Qt Code:
    1. QList<circle* >::iterator it1;
    2. for(it1=lis.begin();it1!=lis.end();++it1) {
    3. out<< (*it1)->id << (*it1)->name;
    4. }
    To copy to clipboard, switch view to plain text mode 

    or you could do away with the iterator and use:
    Qt Code:
    1. foreach(circle *c, lis) out << c->id << c->name;
    To copy to clipboard, switch view to plain text mode 
    thank you very much i've found the problem the semicolon after the for loob caused the problem so we are accessing the end end element

    Qt Code:
    1. void soso::go()
    2. {
    3.  
    4. QFile file("database.txt");
    5. if(!file.open(QIODevice::WriteOnly))
    6. throw " cannot open file ! ";
    7. QDataStream out(&file);
    8. int i=0;
    9. QList<circle*>::iterator it1 =lis.begin();
    10.  
    11. for(i=0;it1!=lis.end();it1++);
    12. {
    13. out<<(*it1)->id; // segmentation error in the runtime
    14. out<<(*it1)->name;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. Replies: 6
    Last Post: 10th April 2006, 10:47

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
  •  
Qt is a trademark of The Qt Company.