Results 1 to 2 of 2

Thread: How to fill a QList<struct> ?

  1. #1
    Join Date
    May 2010
    Posts
    86
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to fill a QList<struct> ?

    Can please someone post a simple example of filling a QList of user-defined structs? I haven't found a good example so far.

    E.g.:

    Qt Code:
    1. struct MeasurementStruct {
    2. QString counterID;
    3. QString networkElementName;
    4. QString netactName;
    5. QString description;
    6. QString object;
    7. };
    8.  
    9. QList<MeasurementStruct *> measurementTypes;
    10.  
    11. for (int i = 0; i < counterID.count(); i++)
    12. {
    13. measurementTypes.append(new MeasurementStruct(??????????));
    14. }
    To copy to clipboard, switch view to plain text mode 
    Edited:

    Found out a workaround solution:

    Header:

    Qt Code:
    1. class MeasurementStruct {
    2. public:
    3. MeasurementStruct(QString counterID, QString networkElementName, QString netactName, QString description, QString object){
    4. MeasurementStruct::counterID = counterID;
    5. MeasurementStruct::networkElementName = networkElementName;
    6. MeasurementStruct::netactName = netactName;
    7. MeasurementStruct::description = description;
    8. MeasurementStruct::object = object;
    9. }
    10.  
    11. QString counterID;
    12. QString networkElementName;
    13. QString netactName;
    14. QString description;
    15. QString object;
    16. };
    To copy to clipboard, switch view to plain text mode 

    Source:

    Qt Code:
    1. {
    2. for (int i = 0; i < counterID.count(); i++)
    3. {
    4. measurementTypes << new MeasurementStruct(counterID.at(i), networkElementName.at(i), "", "", "");
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by falconium; 1st March 2011 at 20:08.

  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: How to fill a QList<struct> ?

    You have a list of pointers, not a list of structure objects.

    Qt Code:
    1. MeasurementStruct *str = new MeasurementStruct;
    2. str->counterID = ...;
    3. //...
    4. list.append(str);
    To copy to clipboard, switch view to plain text mode 

    This is a list of structure objects:
    Qt Code:
    1. QList<MeasurementStruct> list;
    2. MeasurementStruct str;
    3. str.counterID = ...;
    4. //...
    5. list.append(str);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following 2 users say thank you to wysota for this useful post:

    falconium (8th March 2011), sarbh20ss (18th October 2013)

Similar Threads

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

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.