Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QList
QtWS25 Last Chance

Problem with QList

Scheduled Pinned Locked Moved Solved General and Desktop
qlistqstringlist
5 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shahriar25
    wrote on last edited by
    #1

    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;

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gerd
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gerd
          wrote on last edited by
          #4

          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);
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved