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. Custom object type and QList
Forum Updated to NodeBB v4.3 + New Features

Custom object type and QList

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.7k Views 1 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.
  • M Offline
    M Offline
    maxvanceffer
    wrote on last edited by
    #1

    Guys give some hint about operator overloading

    I have such QList: QList<MyCustomType*> list;
    What kind of operator==() , must have my custom type, to have ability use it in method QList::indexOf(MyCustomType*)

    ?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      That should just work (as long as you are using pointers) without operator overloading.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Indeed. Nothing needed. You are putting in pointers, and pointers already have the operators you need by default. Note that no comparisons will be made on the contents of the objects. It is purely a comparison of the pointer values.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maxvanceffer
          wrote on last edited by
          #4

          ))) Just forgot to check if list is filled out. A on more question

          QList<MyCustomType> list;
          list.clear();

          Is memory released, as objects goes out of scope ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Yes. However, you cannot do that with QObject derived types, as the types you put in your container must be copyable and default constructable. QObject does not meet those requirements. You can put pointer to a QObject in your container, but no delete will be called automatically on those pointers on destruction of the list object. That means that if that was the only pointer to object, you are leaking memory.

            A last alternative is to store shared pointers in your list. [[doc:QSharedPointer]] fulfils the requirements of types to put in a container, and when the list is destructed, they will automatically delete anything they pointed to if there are no other shared pointers pointing to the same data.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxvanceffer
              wrote on last edited by
              #6

              Thx, but if containers contains of such clasess

              @class MyClass : public QObject
              {
              ......
              };

              QList<MyClass> list;
              list.clear();
              @

              ?

              Edit: please use @ tags around code sections; Andre

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                That won't compile. Line 6 will probably throw some rather complicated template-related error messages.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maxvanceffer
                  wrote on last edited by
                  #8

                  Thx, big thx for advise))) !!!

                  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