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. Allocating memory in Desctructor for deleting QHash values

Allocating memory in Desctructor for deleting QHash values

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k 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.
  • 1 Offline
    1 Offline
    1231
    wrote on last edited by
    #1

    Hello ! :)

    is there a QHash method which returns one key or its hashed value (as a reference)?

    I attempt to delete objects stored as values in a QHash in the Destructor of a class of mine.

    I only found the method keys() to get all keys of the hash which requires allocating a QList, or don't I have to allocate a QList?

    If the keys() method fails due to memory constraints, I can't destruct my class, (because I don't get the keys which I need to find the values to delete) or can I ?

    Or is there an other approach to this kind of destructing?

    Thank you for your attempt and Best Regards!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can go thorough all values with:
      @
      QHashIterator<QString, int> i(hash);
      while (i.hasNext()) {
      i.next();
      cout << i.key() << ": " << i.value() << endl;
      }
      @

      As mentioned in - surprise! - "official documentation":http://qt-project.org/doc/qt-5.0/qtcore/qhash.html. Also, remember that if your values are QObject pointers that are properly parented, you don't need to delete them, Qt will do it for you.

      (Z(:^

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

        Yes, I read that. Does creating the QHashIterator not allocate memory?
        The values are of a custom-made class. Can I read up on the benefit of deriving of QObject somewhere? Where?
        Thank you.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          I'm not sure. Probably it's already allocated when you create the hash. Take a look at the code if you are so inclined, or use a profiler to see what happens at runtime.

          "QObject":http://qt-project.org/doc/qt-4.8/metaobjects.html stuff is here. It does introduce overhead, so if your device is low on memory and you have a lot of objects to create, then maybe it's not a good idea.

          (Z(:^

          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