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. [Solved]Appending to Last added QString in QStringList
Forum Updated to NodeBB v4.3 + New Features

[Solved]Appending to Last added QString in QStringList

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 696 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.
  • R Offline
    R Offline
    Rututraj
    wrote on 3 Mar 2020, 13:07 last edited by Rututraj 3 Apr 2020, 11:28
    #1

    Hello All,
    As my previous post this might also be a silly question, but I am processing a data and creating QStringList as its output,
    but at certain point I want to append to same index of QStringList multiple times.
    PseudoCode:

    QStringList mylist;
    for(i = 0; i < 35; i++)
    {
    mylist.append("this is ");
    if(condition)
    {
    mylist.append("the output");
    }
    }

    So I want "This is the output" as single string appended to same location in QStringList.
    Is this possible?

    Thanks!

    J K 2 Replies Last reply 3 Mar 2020, 13:20
    0
    • R Rututraj
      3 Mar 2020, 13:07

      Hello All,
      As my previous post this might also be a silly question, but I am processing a data and creating QStringList as its output,
      but at certain point I want to append to same index of QStringList multiple times.
      PseudoCode:

      QStringList mylist;
      for(i = 0; i < 35; i++)
      {
      mylist.append("this is ");
      if(condition)
      {
      mylist.append("the output");
      }
      }

      So I want "This is the output" as single string appended to same location in QStringList.
      Is this possible?

      Thanks!

      K Offline
      K Offline
      KroMignon
      wrote on 3 Mar 2020, 13:30 last edited by
      #3

      @Rututraj said in Appending to Last added QString in QStringList:

      So I want "This is the output" as single string appended to same location in QStringList.
      Is this possible?
      Thanks!

      Why not just use QStringList::last()?

      QStringList mylist;
      for(i = 0; i < 35; i++)
      {
          mylist.append(QString("this is"));
          if(condition) 
              mylist.last().append(" the output");
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      J R 2 Replies Last reply 3 Mar 2020, 13:35
      4
      • R Rututraj
        3 Mar 2020, 13:07

        Hello All,
        As my previous post this might also be a silly question, but I am processing a data and creating QStringList as its output,
        but at certain point I want to append to same index of QStringList multiple times.
        PseudoCode:

        QStringList mylist;
        for(i = 0; i < 35; i++)
        {
        mylist.append("this is ");
        if(condition)
        {
        mylist.append("the output");
        }
        }

        So I want "This is the output" as single string appended to same location in QStringList.
        Is this possible?

        Thanks!

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 3 Mar 2020, 13:20 last edited by J.Hilk 3 Mar 2020, 13:20
        #2

        @Rututraj

        the obvious solution would be to check the condition before inserting (as it's inside your loop)

        QStringList mylist;
        for(i = 0; i < 35; i++)
        {
             QString str("this is");
             if(condition) 
                   mylist.append(str);
             else
                   mylist.append(str + QString("the output"));
        }
        

        alternatively:

        QStringList mylist;
        for(i = 0; i < 35; i++)
        {
             mylist.append(QString("this is"));
             if(condition) 
                 mylist[mylist.size()-1] = mylist.last() + QString("the output");
        }
        

        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
        4
        • R Rututraj
          3 Mar 2020, 13:07

          Hello All,
          As my previous post this might also be a silly question, but I am processing a data and creating QStringList as its output,
          but at certain point I want to append to same index of QStringList multiple times.
          PseudoCode:

          QStringList mylist;
          for(i = 0; i < 35; i++)
          {
          mylist.append("this is ");
          if(condition)
          {
          mylist.append("the output");
          }
          }

          So I want "This is the output" as single string appended to same location in QStringList.
          Is this possible?

          Thanks!

          K Offline
          K Offline
          KroMignon
          wrote on 3 Mar 2020, 13:30 last edited by
          #3

          @Rututraj said in Appending to Last added QString in QStringList:

          So I want "This is the output" as single string appended to same location in QStringList.
          Is this possible?
          Thanks!

          Why not just use QStringList::last()?

          QStringList mylist;
          for(i = 0; i < 35; i++)
          {
              mylist.append(QString("this is"));
              if(condition) 
                  mylist.last().append(" the output");
          }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          J R 2 Replies Last reply 3 Mar 2020, 13:35
          4
          • K KroMignon
            3 Mar 2020, 13:30

            @Rututraj said in Appending to Last added QString in QStringList:

            So I want "This is the output" as single string appended to same location in QStringList.
            Is this possible?
            Thanks!

            Why not just use QStringList::last()?

            QStringList mylist;
            for(i = 0; i < 35; i++)
            {
                mylist.append(QString("this is"));
                if(condition) 
                    mylist.last().append(" the output");
            }
            
            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 3 Mar 2020, 13:35 last edited by
            #4

            @KroMignon
            I could have sworn last() is a const reference only, but apparently it also has a pure reference overload 🤷‍♂️


            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.

            artwawA 1 Reply Last reply 3 Mar 2020, 14:30
            1
            • J J.Hilk
              3 Mar 2020, 13:35

              @KroMignon
              I could have sworn last() is a const reference only, but apparently it also has a pure reference overload 🤷‍♂️

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on 3 Mar 2020, 14:30 last edited by
              #5

              @J-Hilk Other way around: const T &QList::last() const is an overload of T &QList::last(). I was surprised as well.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 3 Mar 2020, 19:23 last edited by
                #6

                Just a side note in all the answers above where QString(const char*) is used, it should be replaced with QStringLiteral (see docs for the explanation)

                "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

                1 Reply Last reply
                1
                • K KroMignon
                  3 Mar 2020, 13:30

                  @Rututraj said in Appending to Last added QString in QStringList:

                  So I want "This is the output" as single string appended to same location in QStringList.
                  Is this possible?
                  Thanks!

                  Why not just use QStringList::last()?

                  QStringList mylist;
                  for(i = 0; i < 35; i++)
                  {
                      mylist.append(QString("this is"));
                      if(condition) 
                          mylist.last().append(" the output");
                  }
                  
                  R Offline
                  R Offline
                  Rututraj
                  wrote on 4 Mar 2020, 11:26 last edited by
                  #7

                  @KroMignon This works fine, Thanks :)

                  1 Reply Last reply
                  0

                  1/7

                  3 Mar 2020, 13:07

                  • Login

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