Results 1 to 5 of 5

Thread: qstringlist problem

  1. #1
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question qstringlist problem

    I read data from file, line by line, and each will be stored as an element in a vector.
    std::vector<one_screw*> m_recorded_screw;
    Here is the code:
    Qt Code:
    1. typedef struct{
    2. int id;
    3. QString name;
    4. float x;
    5. float y;
    6. float z;
    7. float dis;
    8. QString checked;
    9. QString tool;
    10. }one_screw;
    11.  
    12. void read_file(...)
    13. {
    14. QTextStream in(&file);
    15. for( QString line = in.readLine(); !line.isNull(); line = in.readLine())
    16. {
    17. line = line.trimmed();
    18. one_screw *a_screw;
    19.  
    20. QStringList list = line.split(" ", QString::SkipEmptyParts);
    21.  
    22. a_screw->id = list.at(0).toInt();
    23. a_screw->name = list.at(1); //->prolem
    24. a_screw->x = list.at(2).toFloat();
    25. a_screw->y = list.at(3).toFloat();
    26. a_screw->z = list.at(4).toFloat();
    27. a_screw->dis = list.at(5).toFloat();
    28. a_screw->checked = list.at(6); //->prolem
    29. a_screw->tool = list.at(7); //->prolem
    30.  
    31. m_recorded_screw.push_back(a_screw);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    I am not sure whether I must use iterator to access the QStringList element. But for the marked lines I have run-time error "Access violation writing location...". In the struct I cannot use QString? Or I use QStringList wrongly? Help!

  2. #2
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qstringlist problem

    One thing is missing that, I know the number of elements in one line. They are just one record, with words and numbers, but seperated by unknown number of spaces.
    Maybe better idea instead of using split?

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qstringlist problem

    i) if you insist on storing pointers in that list, you need to fix line 18 to
    Qt Code:
    1. one_screw *a_screw=new one_scree;
    To copy to clipboard, switch view to plain text mode 
    (or just store on_screw by value)
    ii) I would add a check in line 20 to make sure that the list actually contains 7 elements.

    HTH

  4. #4
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qstringlist problem

    Quote Originally Posted by caduel View Post
    i) if you insist on storing pointers in that list, you need to fix line 18 to
    Qt Code:
    1. one_screw *a_screw=new one_scree;
    To copy to clipboard, switch view to plain text mode 
    (or just store on_screw by value)
    ii) I would add a check in line 20 to make sure that the list actually contains 7 elements.

    HTH
    Oh, thanks for the remind! During debugging, I changed it to pointer, but it is not necessary.
    I will double check this. Thanks again.

  5. #5
    Join Date
    Jul 2008
    Location
    Munich
    Posts
    73
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qstringlist problem (solved)

    Now my problem is solved! stupid mistake, but happy.

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. problem clear QStringList
    By jaca in forum Newbie
    Replies: 4
    Last Post: 30th May 2008, 16:10
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36

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.