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



  • 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];
    ^



  • Hi @aurquiel

    What about

    save = list_name->at(0)


  • Qt Champions 2016

    @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.


  • Qt Champions 2016

    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.
    


  • 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 !


Log in to reply
 

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