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. If I append an item to a QList can I still change it?
Qt 6.11 is out! See what's new in the release blog

If I append an item to a QList can I still change it?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 909 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by kitfox
    #1

    In the following code, when I call s.foo = 7;, will this modify the value in the list? I'd like to be able to allocate an object on the list and then be able to manipulate it via a reference, but am not sure of the most efficient way to go about this.

    struct MyObj
    {
        int foo = 0;
    }
    
    void testListAdd()
    {
        MyStruct s;
        QList<MyStruct> list;
        list.append(s);
        s.foo = 7;
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      In this case, since you have 1 element in your list:

      list[0].foo = 7;
      

      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
      1
      • K Offline
        K Offline
        kitfox
        wrote on last edited by
        #3

        What if I wanted to say 'append a new element and set the new last element'.

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'm not sure I'm following you, do you mean:

          list.last().foo = 7;
          

          ?

          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
          1
          • K Offline
            K Offline
            kitfox
            wrote on last edited by
            #5

            As I understand it, that will set the last element currently in the list. I want to append a new element and then set that.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you show what you have in mind with some code.

              list.last will return a reference to the last item of the list, so if you call append and then last, you'll get the latest inserted element in the list.

              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
              1
              • K kitfox

                What if I wanted to say 'append a new element and set the new last element'.

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

                @kitfox

                What if I wanted to say 'append a new element and set the new last element'.
                As I understand it, that will set the last element currently in the list. I want to append a new element and then set that.

                The code you showed already does just this. Since the signature is void QList::append(const T &value), what it appends is a reference, so changing the original item once it is in the list does change what is in the list. Is that what you were asking?

                K 1 Reply Last reply
                0
                • JonBJ JonB

                  @kitfox

                  What if I wanted to say 'append a new element and set the new last element'.
                  As I understand it, that will set the last element currently in the list. I want to append a new element and then set that.

                  The code you showed already does just this. Since the signature is void QList::append(const T &value), what it appends is a reference, so changing the original item once it is in the list does change what is in the list. Is that what you were asking?

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

                  @JonB Yes, that is what I wanted to know, JonB. Thanks..

                  Pablo J. RoginaP 1 Reply Last reply
                  0
                  • K kitfox

                    @JonB Yes, that is what I wanted to know, JonB. Thanks..

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @kitfox if your issue is solved, please don't forget to mark your post as such. Thanks

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    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