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. How to save a element of a QList<QString> to variable of QString type
Forum Update on Monday, May 27th 2025

How to save a element of a QList<QString> to variable of QString type

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.2k 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.
  • A Offline
    A Offline
    aurquiel
    wrote on last edited by aurquiel
    #1

    I filled a QList with string but i can't figure out how to pass that string to a QString variable. I know i could fill the list because with the debug i could saw the string of the path of those files.

    QList<QString> *list_name=new QList<QString>[list.size()];
        for (int i = 0; i < list.size(); ++i)
        {
            QFileInfo fileInfo_for_path_2 = list.at(i);
            QString route=fileInfo_for_path_2.absoluteFilePath();
            list_name->insert(i,route);
        }
    
        QString save;
        save=list_name[0];
    

    Getting this error

    /home/zen/QT/fisrt/mainwindow.cpp:60: error: no match for ‘operator=’ (operand types are ‘QString’ and ‘QList<QString>’)
    save=list_name[0];
    ^

    kshegunovK 1 Reply Last reply
    0
    • M Offline
      M Offline
      mostefa
      wrote on last edited by
      #2

      Hi @aurquiel

      What about

      save = list_name->at(0)

      1 Reply Last reply
      0
      • A aurquiel

        I filled a QList with string but i can't figure out how to pass that string to a QString variable. I know i could fill the list because with the debug i could saw the string of the path of those files.

        QList<QString> *list_name=new QList<QString>[list.size()];
            for (int i = 0; i < list.size(); ++i)
            {
                QFileInfo fileInfo_for_path_2 = list.at(i);
                QString route=fileInfo_for_path_2.absoluteFilePath();
                list_name->insert(i,route);
            }
        
            QString save;
            save=list_name[0];
        

        Getting this error

        /home/zen/QT/fisrt/mainwindow.cpp:60: error: no match for ‘operator=’ (operand types are ‘QString’ and ‘QList<QString>’)
        save=list_name[0];
        ^

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @aurquiel said in How to save a element of a QList<QString> to variable of QString type:

        QList<QString> *list_name=new QList<QString>[list.size()];
        

        Don't do that. Create the list as a local variable. The compiler is correct complaining as QList<QString> * which is the type of list_name has no operator = accepting the mentioned arguments.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4
          QList<QList<QString>> list_name;
          // ...
          QString save = list_name[0]; //< See the problem. list_name[0] is of type QList<QString> there's no known conversion so the compiler gives up here.
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            You declare a pointer to an array of StringList, ex:

            QList<QString> *list_name=new QList<QString>[2];  // pointer to 2 StringList
            list_name[0]<<"hello1"<<"hello2";
            list_name[1]<<"bye1"<<"bye2";
            
            qDebug()<<list_name[0][0];  // print hello1
            qDebug()<<list_name[1][1];  // print bye2
            

            I guess is not what you want.
            My strong advice: only use pointers in case of absolute necessity !

            1 Reply Last reply
            2

            • Login

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