Problem with QList



  • Hi
    I am workng on a MusicPlayer and I have categorized the songs given to the app and I wanted to store all thetracks in an album in a QList but I got an error:

    code:
    QList<QStringList> *myList = new QList<QStringList>;
    QStringList temp;
    temp.append("firstTrack");
    myList->append(temp);
    myList->at(0).append("secondTrack");

    error:
    passing 'const QStringList' as 'this' argument discards qualifiers [-fpermissive]
    myList->at(0).append("secondTrack");
    ------------------------------^

    What am I doing wrong?
    Is there a better way than mine to do this?
    Any hep would be appreciated;



  • QList<>.at() retruns a const ref to the list-element
    use
    (*myList)[0].append("secondTrack");
    instead.



  • Hi
    thank you for answering @Gerd and @Ratzz
    both of them worked:
    QList<QStringList> *myList = new QList<QStringList>;
    (*myList)[0].append("secondTrack");

    and

    QList<QStringList*> myList = new QList<QStringList>;

    But according to Qt Docs myList->at() is faster than myList[] if I am not mistaking.



  • QList<QStringList*> myList = new QList<QStringList>;

    won't work, must be
    QList<QStringList*> myList = new QList<QStringList *>;

    If you use the List in that way you have to create the list elements with
    new QStringList
    and also take care of deleting the QStringlist by yourself if removed from the list.

    But according to Qt Docs myList->at() is faster than myList[] if I am not mistaking.

    I don't think you would notice any difference here until you have realy big lists.

    btw, if you setup the stringlist completly before adding it to the list would also work in your code:

    QList<QStringList> *myList = new QList<QStringList>;
    QStringList temp;
    temp.append("firstTrack");
    temp.append("secondTrack");
    myList->append(temp);
    

  • Lifetime Qt Champion

    Hi,

    There's usually no need to allocate QList nor QStringList on the heap. Do you have any reasons to do that ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.