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. 2D QStringList

2D QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 4.8k Views 3 Watching
  • 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.
  • T Offline
    T Offline
    t021
    wrote on last edited by
    #1

    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!

    J.HilkJ 1 Reply Last reply
    0
    • T t021

      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!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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"});
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      T 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        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"});
        
        T Offline
        T Offline
        t021
        wrote on last edited by
        #3

        @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();
        
        T 1 Reply Last reply
        0
        • T t021

          @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();
          
          T Offline
          T Offline
          t021
          wrote on last edited by
          #4

          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.

          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            What is out_stream ?

            What errors do you get ?

            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
            0
            • T t021

              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.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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];
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • T t021

                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.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by J.Hilk
                #7

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


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • T Offline
                  T Offline
                  t021
                  wrote on last edited by
                  #8

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

                  QStringList stringp[3];
                  stringp[0].append("cat");
                  QString Cat=stringp[0][0];
                  
                  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