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. [Solved] QHash: is there a better way than building the object at each startup?

[Solved] QHash: is there a better way than building the object at each startup?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.6k 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.
  • S Offline
    S Offline
    soukyuu
    wrote on last edited by
    #1

    I'm currently using QHash<QString,ulong> objects to store information about several offsets. This means I have to fill each of those objects with all offsets I might be using, making the code quite... long (currently, the QHash filling part is almost as long as the actual code).

    Is there a better way than building the object at each startup? I thought about some way of pre-building the QHash object and bundle it in an internal resource file (I prefer to have a single binary file). Any ideas?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      monkeyDancer
      wrote on last edited by
      #2

      Such data should never be hardcoded.

      Your suggestion is good.

      If the file should be part of the resource or not is a question of the meaning of the data.

      If these offsets belong to data that gets analyzed you should NOT put the file to the resources.

      If the content of the file will be always the same (with every start of the program) you should use it as resource.

      A simple way to structure the file would be:

      string1 20032323
      string2 32348293
      string3 23213333

      and so on.

      Here you could simply read the stream till the end of the line and cut it into tuples "key value".

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

        If your data is always the same, I'd suggest to serialize it once, offline, via QDataStream and include it into your executable as a resource. You can, then, load it in one go, again with QDataStream:
        @
        QHash<QString, quint32> data;
        //set up QDataStream instance in
        in >> data;
        @

        Make sure you use quint32 or quint64 so that your data can be loaded as expected everywhere.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          soukyuu
          wrote on last edited by
          #4

          The offsets basically define where to look for info inside a binary file, so they never change. I'm currently trying out the approach with the resource files, but writing out the object also came into mind as ckakman suggests.

          I guess I could make a separate "db-editor" where I build the offset database and then rebuild it into the main tool's resource. There is also the fact that for small entry count editing a couple of text files is easy, but as the count rises I might run into problems. Besides, you can't really handle all sorts of typos...

          Speaking of quint32/64 vs ulong, is quint32 = ulong on 32-bit systems or quint64?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ckakman
            wrote on last edited by
            #5

            [quote author="soukyuu" date="1421521096"]
            Speaking of quint32/64 vs ulong, is quint32 = ulong on 32-bit systems or quint64?
            [/quote]

            Check out Qt's documentation or source.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              soukyuu
              wrote on last edited by
              #6

              Looks like I'm fine with quint32.

              Running into a problem with QDataStream though:
              I opened files as R+W so after reading the status of the streams is 1 - read past data.

              To solve that I did
              @
              out.resetStatus();
              file.reset();
              @

              The status checks out fine, but only the first entry is written to disc. qDebug shows that the QHash objects have all the entries, so it seems that it's the writing out to disc that's misbehaving.

              What's the proper way to reset its position? Or should I just not bother and open in ReadOnly mode then close, make new streams and open the file in WriteOnly mode?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ckakman
                wrote on last edited by
                #7

                Just do one thing at a time, either read or write.

                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