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



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



  • 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".



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



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



  • [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.



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.