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 add a QString value to QList of QLists?
Forum Updated to NodeBB v4.3 + New Features

How to add a QString value to QList of QLists?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 4.4k Views 2 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.
  • E Offline
    E Offline
    Eduardo12l
    wrote on last edited by Eduardo12l
    #1

    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?

    K E Taz742T 3 Replies Last reply
    0
    • E Eduardo12l

      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?

      K Offline
      K Offline
      koahnig
      wrote on last edited by koahnig
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      E 1 Reply Last reply
      2
      • K koahnig

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

        E Offline
        E Offline
        Eduardo12l
        wrote on last edited by Eduardo12l
        #3

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

        K 2 Replies Last reply
        0
        • E Eduardo12l

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

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

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

          Vote the answer(s) that helped you to solve your issue(s)

          E 1 Reply Last reply
          4
          • E Eduardo12l

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

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

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

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            1
            • K koahnig

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

              E Offline
              E Offline
              Eduardo12l
              wrote on last edited by Eduardo12l
              #6

              @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

              1 Reply Last reply
              0
              • E Eduardo12l

                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?

                E Offline
                E Offline
                Eduardo12l
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  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

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  Taz742T 1 Reply Last reply
                  4
                  • E Eduardo12l

                    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?

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by Taz742
                    #9

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

                    Do what you want.

                    1 Reply Last reply
                    1
                    • VRoninV VRonin

                      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

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #10

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

                      Do what you want.

                      1 Reply Last reply
                      1
                      • C Offline
                        C Offline
                        captainBee
                        wrote on last edited by
                        #11

                        Qlist<T>*qlt;
                        for(int j; j<qlt.count(); j++) qlr.replace(j,"whatever");

                        1 Reply Last reply
                        0

                        • Login

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