Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QHash behaviour
Qt 6.12 LTS is out! See what's new in the release blog

QHash behaviour

Scheduled Pinned Locked Moved General and Desktop
24 Posts 5 Posters 24.1k 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
    mlong
    wrote on last edited by
    #7

    See my post above.

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

      In that case, QHash is not your class to use. Nor is QMap. Use a something like this:
      @
      typedef QPair<int, QString> intStringPair;
      QVector<intStringPair> vector(3);
      //do your inserts
      @

      You may replace the QPair with a struct, and the QVector with a QList if you prefer.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #9

        So what would be the solution to keep the insertion order and still enjoy the fast lookup of a hash?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #10

          I suggested above, keeping a QList (or QVector) of the keys for iteration purposes, and using the QHash to store the values.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #11

            Certainly not as elegant as a single container. Perhaps such a container would make a good addition to Qt?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #12

              It's not something that I've needed to do very often. And in the instances it has been, it's been simple enough to write a wrapper class that combines the functionality of the the two. But YMMV.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                QMap is usually a good enough replacement, but when needed, I have also used a list of structures in combination with a QHash. I usually just stored either the list index in the hash, or alternatively a pointer to the struct in the list. Also: you should consider how big your list really is, and if it even makes sense to use a QHash. You say you need QHash, but remember that QHash is not always faster than QMap or even iterating over a QVector.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #14

                  Obviously such a data structure would make sense in large collections where order is important just as fast lookup. In such scenario even a single extra pointer could end up costing plenty of memory. And then there is the indirection performance penalty.

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

                    Basically, what you are looking for is a hash with multiple keys. There are such classes available, but not in Qt. That does not mean you can't use them. For instance, take a look at "Boost.MultiIndex":http://www.boost.org/doc/libs/1_41_0/libs/multi_index/doc/tutorial/index.html

                    Note that the additional storage and indirection penalties will always be there. Either they are inside the container, or external, but you are going to need additional data for bookkeeping if you want to access the data in more than one way.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #16

                      I'm not been able to create QHash<int,QVariant> var.

                      Is that impossible?

                      Why?

                      When I try that I see the errors below:

                      In file included from c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/QHash:1,
                      from ....\complementos/matriz.h:5,
                      from ....\complementos\matriz.cpp:1:
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h: In instantiation of 'QHashNode<int, QVariant>':
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:521: instantiated from 'static void QHash<Key, T>::deleteNode2(QHashData::Node*) [with Key = int, T = QVariant]'
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:570: instantiated from 'void QHash<Key, T>::freeData(QHashData*) [with Key = int, T = QVariant]'
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:283: instantiated from 'QHash<Key, T>::~QHash() [with Key = int, T = QVariant]'
                      ....\complementos\matriz.cpp:4: instantiated from here
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:253: error: 'QHashNode<int, T>::value' has incomplete type
                      c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:66: error: forward declaration of 'struct QVariant'
                      mingw32-make.exe[1]: *** [debug/matriz.o] Error 1
                      mingw32-make.exe: *** [debug] Error 2
                      10:55:23: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
                      Error while building project cliente (target: Desktop)
                      When executing build step 'Make'

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

                        You probably didn't #include <QVariant>

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #18

                          Ok!

                          It works!

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

                            <schoolmaster mode>
                            Do you understand how I arrived at that conclusion?

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #20

                              No.

                              I would appreciate if you explain.

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

                                @In file included from c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/QHash:1, from ....\complementos/matriz.h:5, from ....\complementos\matriz.cpp:1:
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h: In instantiation of ‘QHashNode<int, QVariant>’:
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:521: instantiated from ‘static void QHash<Key, T>::deleteNode2(QHashData::Node*) [with Key = int, T = QVariant]’
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:570: instantiated from ‘void QHash<Key, T>::freeData(QHashData*) [with Key = int, T = QVariant]’
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:283: instantiated from ‘QHash<Key, T>::~QHash() [with Key = int, T = QVariant]’
                                ....\complementos\matriz.cpp:4: instantiated from here
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qhash.h:253: error: ‘QHashNode<int, T>::value’ has incomplete type
                                c:\QtSDK\Desktop\Qt\4.8.1\mingw\include\QtCore/qobject.h:66: error: forward declaration of ‘struct QVariant’
                                mingw32-make.exe1: *** [debug/matriz.o] Error 1
                                mingw32-make.exe: *** [debug] Error 2
                                10:55:23: The process “C:\QtSDK\mingw\bin\mingw32-make.exe” exited with code 2.
                                Error while building project cliente (target: Desktop)
                                When executing build step ‘Make’
                                @

                                It is a useful skill to learn how to read compilation errors. This one has a couple of instantiated from... lines, that are not all that relevant here. Especially if they point into the Qt library. Look for the lines related to your own code instead. Line 7 is where it gets interesting. Do you spot the "... has incomplete type"? And on the next line: forward declaration of struct QVariant?

                                Hmmm... QVariant is not a struct, but a class. So, it seems your QHash doesn't know all it needs to know about QVariant, as it assumes it is a struct. That led me to the conclusion that you did not include the relevant header file.

                                Also, you should understand that using forward declared types in templates doesn't work. You need to have the full type information available when instantiating your template.

                                1 Reply Last reply
                                0
                                • ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #22

                                  In range0's defense - those compiler errors are pretty far from informative. It does take some time and experience to be able to make out something useful out of them.

                                  Andre - how come Creator doesn't offer some Java like feature to automatically add the needed includes? Or maybe there is one already?

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

                                    Nothing to defend. Template errors can be tricky to learn to read, that's why I asked and took the time to explain.

                                    As for auto-add the header: There is the magic ALT+Enter that you can try. It does offer in some cases to add the needed include. However, I did not find it very reliable in this case. Or rather, I did not find the whole pattern yet. I would not appreciate QtCreator automatically including every class I use. Especially in headers, I tend to use forward declares wherever I can.

                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #24

                                      Andre,

                                      Thank you very much for your explanation!

                                      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
                                      • Unsolved