How to add a QString value to QList of QLists?



  • I've tried this:

    QList<QList<QString>>Temporales
    QList<QString> DeFNombre
    QList<QString> DefGruop
    QString aux = DeFNombre[0]+DefGruop[0];
    Temporle[0].append(aux)
    

    However, this way crash when debugging. What can i do?


  • Moderators

    @Eduardo12l

    Are you aware that there is a QStringList in Qt?

    QStringList may be extended using the operator<<.
    The classical push_back is another option.

    Where is it exactly crashing when you do it this way?

    When using the five lines of code given above, it will crash of course, because DeFNombre and DefGruop are still empty.



  • @koahnig

    • It means that i have no alternative and I must use QStringList ?

    • Those both QList are not empty they were initialized before, the only one that is empty is QList of QLists - Temporales

    Sorry i forgot it. It crashed at :

    Temporales[0].append(aux)
    


  • Moderators

    @Eduardo12l

    What do you mean by crashed?

    A crash is typically when an crashes (stops) during execution with some stack output.

    What you are seeing is compile error and not a crash. In such cases the error message tells you what is wrong.

    In your first post you show:
    @Eduardo12l said in How to add a QString value to QList of QLists?:

    QList<QList<QString>>Temporales
    QList<QString> DeFNombre
    QList<QString> DefGruop
    QString aux = DeFNombre[0]+DefGruop[0];
    Temporle[0].append(aux)
    

    which cannot be compiled without a compile error.

    In your second post, it shows at least the problem, but it is still not a complete code line, since it will not compile as it is shown.

    @Eduardo12l said in How to add a QString value to QList of QLists?:

    @koahnig

    • It means that i have no alternative and I must use QStringList ?

    • Those both QList are not empty they were initialized before, the only one that is empty is QList of QLists - Temporales

    Sorry i forgot it. It crashed at :

    Temporales[0].append(aux)
    

    In case those lines are only missing the semicolon in post and Temporales has already a first element, it should work.
    See operator[]


  • Moderators

    @Eduardo12l said in How to add a QString value to QList of QLists?:

    @koahnig

    • It means that i have no alternative and I must use QStringList ?

    No, it is not mandatory, but makes life a bit easier.



  • @koahnig When i run my app it suddenly stops and Qt Output tells me:
    The program has unexpectedly finished.
    C:\Qt\Saves\build-Proyecto1-Desktop_Qt_5_8_0_MSVC2015_64bit-Release\release\Proyecto1.exe crashed.

    Instead, if I write this: (Instead of my first way)

    Temporales.append(DefNombre); //DefNombre is a QList<QString> that is filled
     qDebug()<<Temporales.at(0).at(0);
    

    I have no problems, i get from qDebug() the first column of the first row but it misses one value that comes from DefGrupo for that reason i cannot use this way
    In other words my main problem is that QList<QList<QString>> doesnt let me to append QStrings



  • This post is deleted!


  • From http://doc.qt.io/qt-5/qlist.html#operator-5b-5d:

    Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size())

    Temporle[0] returns an invalid reference so it crashes. you have to insert an item before accessing it.

    
    Temporle.append(QList<QString>());
    Temporle[0].append(aux);
    

    P.S.
    QStringList inherits QList<QString> and just adds a few methods so they are basically the same thing



  • @Eduardo12l
    Used Qvector<>

        const int MAXN = 10;
    
        QVector<QVector<QString> > lst;
    
        lst.resize(MAXN);
    
        lst[0].push_back("foo");
    
        qDebug() << lst[0][0];
    

    or

        const int MAXN = 10;
    
        std::vector<std::vector<std::string> > lst(MAXN);
    
        lst.at(0).push_back("faf");
    
        qDebug() << QString::fromStdString(lst[0][0]);
    


  • or
    @VRonin said in How to add a QString value to QList of QLists?:

    Temporle.append(QList<QString>());
    Temporle[0].append(aux);

    QList<QList<QString> > lst;
    
    for(int i = 0; i < 10; i++){
        lst.append(QList<QString>());
    }
    
    lst[0].append("foo");
    
    qDebug() << lst[0][0];

Log in to reply
 

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