Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Change items in a container (e.g. QList)

    General and Desktop
    3
    19
    3027
    Loading More Posts
    • 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.
    • M
      Maser last edited by Maser

      Hi.
      I have a QList<MyClass> and i want to access and change the items in the list. Through google i came up with

      list->operator [](index).setSomeValue();
      

      where list is a pointer to QList.
      I have two questions. Why

      list[index].setSomeValue();
      

      doesnt work? Shouldnt it be the same?
      And the second: when i come to this point "list->operator [] (index)." the qcreator doesnt show me any posibility to chose something in the autocompletion, although when i just type the setSomeValue() after the point it compiles and work?

      Perhas there is a better way to change an item in a container?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        Hi
        is that

        QList<MyClass> mylist;
        OR
        QList<MyClass *> mylist;
        

        as that change the syntax
        version 1 would be
        mylist[index].setSomeValue();
        and version 2 would be
        mylist[index]->setSomeValue();

        1 Reply Last reply Reply Quote 1
        • M
          Maser last edited by Maser

          it is

          QList<MyClass> *list = new QList<MyClass>
          

          and when im doing mylist[index].setSomeValue(); is says there is no member named setSomeValue

          jsulm 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            class MyClass {
             public:
              void bing() {};
              MyClass() {}
            };
            
              QList<MyClass>* list = new QList<MyClass>;
              MyClass tmp;
              list->append(tmp);
              (*list)[0].bing();
            (delete list;)
            

            Can I ask why you want the list to be a pointer to a list and not just a list?

            1 Reply Last reply Reply Quote 4
            • jsulm
              jsulm Lifetime Qt Champion @Maser last edited by

              @Maser As @mrjj said: list is a pointer, so you have first to dereference it and then use [] operator:

              (*list)[index].setSomeValue();
              

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

              1 Reply Last reply Reply Quote 2
              • M
                Maser last edited by

                Ah, thanks. I didnt thought of seting parenthesize the dereferencing.
                Well, all the Qt examples create pointer and then create the container on the heap, dont they?

                mrjj 1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion @Maser last edited by mrjj

                  @Maser
                  well the normal case if that its a non pointer
                  QList<MyClass *> list;

                  but you get same situation if u send it as paramater

                  void somefunc( QList<MyClass *> * List);

                  somefunc(&list);

                  so it really depends on what you do with list and how long it must live.
                  (note normally it would be &)

                  1 Reply Last reply Reply Quote 1
                  • M
                    Maser last edited by

                    I dont understand. What QList<MyClass *> list; benefits me? That i only have to create them once? Doesnt QList<MyClass> *list do the same as i create the items on the heap and pass the pointer around?

                    mrjj 1 Reply Last reply Reply Quote 0
                    • mrjj
                      mrjj Lifetime Qt Champion @Maser last edited by

                      @Maser
                      hi
                      all QWidgets are QObjects.
                      QObjects have copy constructor disabled. They cannot be cloned.
                      So you cannot have QWidgets in a list if you define it as
                      QList<MyClass>
                      IT must be pointers.

                      However for you own classes, there is no need for pointers.

                      QList<MyClass> *list = pointer to list of objects. Not possible with most QWidgets
                      QList<MyClass *> *list = pointer to list of pointers to objects. can contain QWidgets
                      QList<MyClass> list; Just a list of Myclasses. ( best option normally)

                      1 Reply Last reply Reply Quote 2
                      • M
                        Maser last edited by Maser

                        So this means if i have my class derived from QObject, i can only store them as pointers like QList<MyClass *> *list or QList<MyClass *> list ?

                        mrjj 1 Reply Last reply Reply Quote 1
                        • mrjj
                          mrjj Lifetime Qt Champion @Maser last edited by

                          @Maser
                          yes correct.

                          1 Reply Last reply Reply Quote 0
                          • M
                            Maser last edited by

                            A, thanks! One more mystery solved)) I already wondered why when i wanted add to MyClass the abilities from QObject something crash down in my program, so i made some workarounds)

                            mrjj 1 Reply Last reply Reply Quote 0
                            • mrjj
                              mrjj Lifetime Qt Champion @Maser last edited by

                              Also, just as note.
                              Most QWidgets takes a parent (another qwidget) in constructor.
                              It means the parent now owns the child and will delete it when it dies it self.

                              So if you have childs in a list and give them to parents,
                              the list should Not delete the childs as the parent will. :)

                              1 Reply Last reply Reply Quote 0
                              • M
                                Maser last edited by

                                Ye, i stil havent had time to go into that stuff with smartpointers and all)

                                mrjj 1 Reply Last reply Reply Quote 0
                                • mrjj
                                  mrjj Lifetime Qt Champion @Maser last edited by

                                  @Maser
                                  well its not so much smart pointers but more a ownership.
                                  Must Widgets in Qt can own other widgets and will delete them when deleted.

                                  So often even you new a Qwidget you dont need to delete it yourself as parent will.

                                  1 Reply Last reply Reply Quote 1
                                  • M
                                    Maser last edited by

                                    Yes, but this is stil related to watching for pointers, if they stil good or deleted somewhere else, doesnt it?

                                    BTW, why cant i mark the thread solved?

                                    mrjj 1 Reply Last reply Reply Quote 0
                                    • mrjj
                                      mrjj Lifetime Qt Champion @Maser last edited by mrjj

                                      @Maser
                                      yes watching pointers is always needed :)

                                      • BTW, why cant i mark the thread solved?
                                        You might not have asked it as question.
                                        On first post look in topic tools button.
                                        There is "Ask as Question"
                                        Then you can mark it as solved :)
                                      1 Reply Last reply Reply Quote 1
                                      • M
                                        Maser last edited by

                                        Tricky) Thanks.

                                        mrjj 1 Reply Last reply Reply Quote 0
                                        • mrjj
                                          mrjj Lifetime Qt Champion @Maser last edited by

                                          @Maser
                                          yes its slightly funky :)

                                          1 Reply Last reply Reply Quote 0
                                          • First post
                                            Last post