2D QStringList



  • I need to have a 2D QStringList that stores a table. I have this but it doesn't work:

    QFile outfile("file.txt"); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
    QTextStream out_stream(&outfile);
    
    QList<QStringList> list;
    list[0].append("cat");
    list[1].append("dog");
    list[2].append("mouse");
    out_stream << list.at(0) << list.at(1) << list.at(2);
    
    list.clear(); 
    outfile.close();
    

    I think this should be a list of three QStringLists. So why can I not output the first three entries? Thank you!



  • Hi,

    QList<QStringList> list; is the same as QList<QList<QString> > list;
    

    that mean to access a QString of that struct you need 2 indices
    QString s = list[indexOfList][indexOfString];

    with

    QList<QStringList> list;
    list[0].append("cat");
    list[1].append("dog");
    list[2].append("mouse");
    

    you expect list to allready have 3 QStringLists where you than append a new QString

    to append a QStringList to your List
    you could write the following

    QList<QStringList> list;
    list.append(QList<QString>{"cat"});
    list.append(QList<QString>{"dog"});
    list.append(QList<QString>{"mouse"});
    


  • @J.Hilk
    Than you. I see. But then the out_stream line still won't compile. I now have this:

    QFile outfile("file.txt"); if(!outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {exit(1);}
    QTextStream out_stream(&outfile);
    
    QList<QStringList> list;
    list.append(QList<QString>{"cat"});
    list.append(QList<QString>{"dog"});
    list.append(QList<QString>{"mouse"});
    out_stream << list[0][0];
    
    list.clear(); 
    outfile.close();
    


  • I'd actually really like to use pointers, if possible. This is something really simple.

    QStringList* stringp[3];
    stringp[0]->append("cat");
    QString Cat=stringp[0][0];
    

    The first two lines should be OK. If I could only find a way to retrieve that "cat" entry (as a QString for example, as presented here) I should be good to go.


  • Lifetime Qt Champion

    Hi,

    What is out_stream ?

    What errors do you get ?


  • Moderators

    @t021 You should avoid pointers as much as possible.
    To retrieve cat do:

    QStringList stringp[3];
    stringp[0].append("cat");
    QString Cat=stringp[0][0];
    

    If you insist to use pointers:

    QStringList* stringp[3];
    stringp[0]->append("cat");
    QString Cat=(*stringp[0])[0];
    


  • @t021 there has to be an error with your outstream because, using <QDebug> I can access all QStrings of the 2D List without any problem:

    QList<QStringList> list;
    list.append(QList<QString>{"cat"});
    list.append(QList<QString>{"dog"});
    list.append(QList<QString>{"mouse"});
    list.append(QList<QString>{"cat","dog","mouse"});
    
    for(auto a : list)
            for(auto b : a)
                qDebug() << b;
    

    Console result:

    • "cat"
    • "dog"
    • "mouse"
    • "cat"
    • "dog"
    • "mouse"

    Show us the error msg you get.



  • Thank you to all!! This version works, so the problem is solved:

    QStringList stringp[3];
    stringp[0].append("cat");
    QString Cat=stringp[0][0];
    

Log in to reply
 

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