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. Increasing QList<QStringList> contents twice!
QtWS25 Last Chance

Increasing QList<QStringList> contents twice!

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistqstringlistloop
12 Posts 6 Posters 4.1k Views
  • 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.
  • K Offline
    K Offline
    Kushan
    wrote on last edited by A Former User
    #1

    In my Qt C++ application I have a QList<QstringList> I want to increase the contents of its 0th index by the same amount!

    void MainWindow::on_pushButton_clicked()
    {
    
        list1<<"a"<<"b"<<"c"<<"d"<<"e";
    
        List1<<list1;
    
        for(int i=0;i<2;i++){
            for(int j=0;j<1;j++){
                List1[j]<<List1[j];
            }
        }
    }
    

    from the above code I want to make the contents of 0th index of List (i.e List[0]) a QStringList of "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" but now it is "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" "a" "b" "c" "d" "e"! How can I correct this?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      remove the loop with i

      "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

      K 1 Reply Last reply
      2
      • VRoninV VRonin

        remove the loop with i

        K Offline
        K Offline
        Kushan
        wrote on last edited by
        #3

        @VRonin Thanx but if I add another QStringList list2<<"f"<<"g"<<"h"<<"i"<<"j"; to List1 how can I get twice of its contents also?

        K 1 Reply Last reply
        0
        • K Kushan

          @VRonin Thanx but if I add another QStringList list2<<"f"<<"g"<<"h"<<"i"<<"j"; to List1 how can I get twice of its contents also?

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

          @Kushan

          Not really sure what you are trying to do. Also the outcome seem not to match what I would expect.

          Anyway checkout the doucmentation for QStringList &QStringList::operator<<(const QList<QString> &other)

          QStringList list1;
          list1<<"a"<<"b"<<"c"<<"d"<<"e"; // list1 should contain "a" "b" "c" "d" "e" beause you copy individual strings
          QStringList List1;               // List1 is empty
          // copy one QStringList to another
          List1 << list1; // List1 should contain "a" "b" "c" "d" "e" because you copied with operator<< as described above
          // repeat to add another copy
          List1 << list1; // List1 should contain "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" because you appended twice
          

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

          VRoninV 1 Reply Last reply
          0
          • K koahnig

            @Kushan

            Not really sure what you are trying to do. Also the outcome seem not to match what I would expect.

            Anyway checkout the doucmentation for QStringList &QStringList::operator<<(const QList<QString> &other)

            QStringList list1;
            list1<<"a"<<"b"<<"c"<<"d"<<"e"; // list1 should contain "a" "b" "c" "d" "e" beause you copy individual strings
            QStringList List1;               // List1 is empty
            // copy one QStringList to another
            List1 << list1; // List1 should contain "a" "b" "c" "d" "e" because you copied with operator<< as described above
            // repeat to add another copy
            List1 << list1; // List1 should contain "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" because you appended twice
            
            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @koahnig List1 is of type QList<QstringList> (i guessed from the title)

            @Kushan said in increasing QList<QStringList> contents twice!:

            Thanx but if I add another QStringList list2<<"f"<<"g"<<"h"<<"i"<<"j"; to List1 how can I get twice of its contents also?

            List1<<list1;
            List1[0]<<list2;
            List1[0] << List1[0]; //this is the duplication
            

            "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

            K 1 Reply Last reply
            4
            • VRoninV VRonin

              @koahnig List1 is of type QList<QstringList> (i guessed from the title)

              @Kushan said in increasing QList<QStringList> contents twice!:

              Thanx but if I add another QStringList list2<<"f"<<"g"<<"h"<<"i"<<"j"; to List1 how can I get twice of its contents also?

              List1<<list1;
              List1[0]<<list2;
              List1[0] << List1[0]; //this is the duplication
              
              K Offline
              K Offline
              Kushan
              wrote on last edited by
              #6

              @VRonin yeah its QList<QstringList>

              JonBJ 1 Reply Last reply
              0
              • K Kushan

                @VRonin yeah its QList<QstringList>

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @Kushan
                How come there's a post https://forum.qt.io/topic/85879/runtime-error-terminate-called-after-throwing-an-instance-of-std-bad_alloc by a different person? Do you have a double? Have you each been set homework? :)

                K kshegunovK 2 Replies Last reply
                3
                • JonBJ JonB

                  @Kushan
                  How come there's a post https://forum.qt.io/topic/85879/runtime-error-terminate-called-after-throwing-an-instance-of-std-bad_alloc by a different person? Do you have a double? Have you each been set homework? :)

                  K Offline
                  K Offline
                  Kushan
                  wrote on last edited by
                  #8

                  @JNBarchan I read that post! In my problem there are no errors only the output is incorrect

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Kushan
                    How come there's a post https://forum.qt.io/topic/85879/runtime-error-terminate-called-after-throwing-an-instance-of-std-bad_alloc by a different person? Do you have a double? Have you each been set homework? :)

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by
                    #9

                    @JNBarchan said in increasing QList<QStringList> contents twice!:

                    Have you each been set homework?

                    My guess is the same class at University of Colombo School of Computing. I was too lazy to run a cross-correlation analysis on the posts, though, so don't take any of this at face value ... but then again ... if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

                    Read and abide by the Qt Code of Conduct

                    JonBJ J.HilkJ 2 Replies Last reply
                    2
                    • kshegunovK kshegunov

                      @JNBarchan said in increasing QList<QStringList> contents twice!:

                      Have you each been set homework?

                      My guess is the same class at University of Colombo School of Computing. I was too lazy to run a cross-correlation analysis on the posts, though, so don't take any of this at face value ... but then again ... if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @kshegunov
                      Excluding the code (which is virtually identical, including the lack of spaces), they both have:

                      In my Qt C++ application I have a QList<QstringList> I want to increase the contents of its 0th index by the same amount!
                      ...
                      from the above code I want to make the contents of 0th index of List (i.e List[0]) a QStringList of "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" ... How can I correct this?

                      That's the same down to the "!"-mark! Something fishy is going on with ducks here... ;-)

                      It's interesting to hear that Qt is being used in an education course. But why Qt for this question? There's nothing special about QList or QStringList, this could just as well have been posed for std C++. If you're going to teach Qt I would have expected a Qt-ish question, else don't bother.

                      K 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @kshegunov
                        Excluding the code (which is virtually identical, including the lack of spaces), they both have:

                        In my Qt C++ application I have a QList<QstringList> I want to increase the contents of its 0th index by the same amount!
                        ...
                        from the above code I want to make the contents of 0th index of List (i.e List[0]) a QStringList of "a" "b" "c" "d" "e" "a" "b" "c" "d" "e" ... How can I correct this?

                        That's the same down to the "!"-mark! Something fishy is going on with ducks here... ;-)

                        It's interesting to hear that Qt is being used in an education course. But why Qt for this question? There's nothing special about QList or QStringList, this could just as well have been posed for std C++. If you're going to teach Qt I would have expected a Qt-ish question, else don't bother.

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

                        @JNBarchan @kshegunov nice research guys.

                        I had already the feeling that the initial post is really sloppy formulated like someone is fishing for an easy answer.

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

                        1 Reply Last reply
                        0
                        • kshegunovK kshegunov

                          @JNBarchan said in increasing QList<QStringList> contents twice!:

                          Have you each been set homework?

                          My guess is the same class at University of Colombo School of Computing. I was too lazy to run a cross-correlation analysis on the posts, though, so don't take any of this at face value ... but then again ... if it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @kshegunov said in increasing QList<QStringList> contents twice!:

                          if it looks like a duck, swims like a duck, and quacks like a duck

                          s

                          Slight thread derail, but I would say the op's question general is answered .


                          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
                          0

                          • Login

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