Results 1 to 12 of 12

Thread: QList<struct>

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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 

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.