Results 1 to 12 of 12

Thread: QList<struct>

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList<struct>

    Hi
    I am working in winХР, qt.4.3.3.
    First will show my code:
    Qt Code:
    1. #include <QVector>
    2. #include <QList>
    3. #include <QString>
    4. #include <QImage>
    5. //#include <list.h>
    6. //#include <vector.h>
    7. typedef struct{
    8. unsigned int x;
    9. unsigned int y;
    10. unsigned int z;
    11. unsigned short int A1;
    12. unsigned short int A2;
    13. unsigned short int A3;
    14. unsigned short int A4;
    15. unsigned short int A[1024];
    16. } dataPoint;
    17. typedef struct{
    18. QVector<dataPoint> dataVector;
    19. QImage preview;
    20. QString author;
    21. QString date;
    22. QString time;
    23. } dataScan;
    24. class DataStorage
    25. {
    26. //Q_OBJECT
    27. public:
    28. DataStorage();
    29. QVector<dataPoint> point;
    30. QList<dataScan> dataContainer;
    31. QList<dataScan>::iterator dataContainerIterator;
    32. };
    To copy to clipboard, switch view to plain text mode 
    I need to get access to elements of structure dataContainer and dataVector which is an element of dataContainer. I tried various ways, but at execution the program gives out errors like "QVector: index out of range". When I used classical C++ list and vector I receive RuntimeError.
    Below some ways with which I tried to get access to the necessary elements of structures
    are shown. But any of them does not work.
    Qt Code:
    1. DataStorage::DataStorage()
    2. {
    3. const int nPoints = 360;
    4. int q=0;
    5. dataContainerIterator = dataContainer.begin();
    6. for (int i=0;i<=nPoints;i++)
    7. {
    8. q = qAbs(qRound(100*sin(i*3.14/180)));
    9. dataContainerIterator->dataVector[i].x << i;
    10. dataContainerIterator->dataVector[i].x = i;
    11. dataContainerIterator->dataVector.at(i).x << 1;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    Where is my mistake?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<struct>

    Did you allocate enough space in the vector? You can't add data to an empty vector using the index operator.

  3. #3
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<struct>

    Quote Originally Posted by wysota View Post
    Did you allocate enough space in the vector? You can't add data to an empty vector using the index operator.
    No, I don't know how much memory can be neccessary. If I do as it is shown below I receive errer: class QVector<dataPoint>' has no member named 'x'.
    Qt Code:
    1. dataContainerIterator->dataVector.x << i;
    To copy to clipboard, switch view to plain text mode 
    As far as I know there is no necessity to allocate memory before add a data.
    Last edited by Axsis; 9th April 2008 at 10:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<struct>

    Quote Originally Posted by Axsis View Post
    As far as I know there is no necessity to allocate memory before add a data.
    You are not adding data. You are assigning data.

  5. The following user says thank you to wysota for this useful post:

    Axsis (9th April 2008)

  6. #5
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<struct>

    Ok. It's clear. But how I can allocate memory for dataVector? I mean that it is an element of structure not a usual vector.

    Solved...
    Last edited by Axsis; 9th April 2008 at 12:19.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<struct>


  8. #7
    Join Date
    Apr 2008
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList<struct>

    Quote Originally Posted by wysota View Post
    I know about this functions but I dont know how to use it. I mean that I have QVector<struct> and I don't know how to call QVector::push_back if I want to add .x, then .y ...
    Last edited by Axsis; 9th April 2008 at 14:16.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList<struct>

    Qt Code:
    1. QVector<MyStruct> vec;
    2. MyStruct s;
    3. s.x = 7;
    4. s.y = 8;
    5. vec.push_back(s);
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList<struct>

    Hi, from the solution posted above. How do you access the items in the vectors struct.

    Hi, from the solution posted above. How do you access the items in the vectors struct.
    Not sure but would this work ?


    Qt Code:
    1. cout<<vec.at(0).x;
    To copy to clipboard, switch view to plain text mode 


    Added after 10 minutes:


    Haha it works.
    I wonder why none of this has been documented.


    Added after 14 minutes:


    Wait one problem, how do you modify the data.
    I tried
    Qt Code:
    1. m_programs.at(0).exitcode=1;
    To copy to clipboard, switch view to plain text mode 
    which fails to compile with
    Qt Code:
    1. error: assignment of member 'program::exitcode' in read-only object
    To copy to clipboard, switch view to plain text mode 
    Hope there is a cleaner way so I won't have to create a new data structure and assign it over again.
    Last edited by kenkit; 10th October 2015 at 10:57.

  11. #10
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList<struct>

    As to the list (vector or a similar structure), the index operator or the at() function can access only existing data. You need to insert the data into the list first and then you can reference them using [] or at(). When you create the vector, use append() (or push_back(), which is the same for STL compatibility). See Wysota's comments above. AFAIK, reserve() will not suffice because reserve() only reallocates space but it does not add any elements.
    Qt Code:
    1. dataScan tmpstruct;
    2.  
    3. for( int i = 0; i < nmbOfElements; ++i )
    4. {
    5. fill tmpstruct with new data;
    6. dataContainer.append(tmpstruct);
    7. }
    To copy to clipboard, switch view to plain text mode 
    if the data aren't too sequential, you can also insert() new data to the proper place but the element where you are inserting must already exist when you command insert(). In the worst case, you can prefill the list with placeholders (using append() ) and then use replace(). Again, the data at replace() position must already exist.

    As to accessing the data: The index operator or the at() functions returns data of the type of the list element. You can process the data subsequently. Therefore:
    Qt Code:
    1. cout << dataContainer.at(0).author;
    2. cout << dataContainer.at(0).datavector.at(0).x;
    3. cout << dataContainer.at(0).datavector.at(0).A[15];
    To copy to clipboard, switch view to plain text mode 
    should be OK.

    Actual size of the list (the biggest valid index into the list plus one) - use the size() function.

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList<struct>

    Quote Originally Posted by kenkit View Post
    Not sure but would this work ?
    Qt Code:
    1. cout<<vec.at(0).x;
    To copy to clipboard, switch view to plain text mode 
    Haha it works.
    I wonder why none of this has been documented.
    Of course this is documented.
    Documentation for access to the vector element using QVector::at() and access to a struct member is documented in the C++ standard.

    Quote Originally Posted by kenkit View Post
    which fails to compile with
    Qt Code:
    1. error: assignment of member 'program::exitcode' in read-only object
    To copy to clipboard, switch view to plain text mode 
    Since you are obviously very new to C++: the const keyword on a type means that the object cannot be modified, it is a constant.

    Quote Originally Posted by kenkit View Post
    Hope there is a cleaner way so I won't have to create a new data structure and assign it over again.
    Of course, there are accessors that return a non-const reference, the index operator for example

    Cheers,
    _

  13. #12
    Join Date
    Oct 2015
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QList<struct>

    As far as I know there is no necessity to allocate memory before add a data.

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.