Important: Please read the Qt Code of Conduct -

How to convert QHash into model data?

  • Hello All,

    Every time I'm facing this issue when I want to have list of different data in hierarchy way.
    Example: QHash<QString, QHash<QString, QList<s_data>>> data;

    struct s_data
            QString _d;
            QString _q;
            int _c;
    QHash<QString, QHash<QString, QList<s_data>>> data;

    For this above, I want to have data in model-like, QStandardItemModel, or any other model which is easy to use.

    Please provide your suggestion and need a sample example of it, how I can convert such type of data into a model.


  • Lifetime Qt Champion


    The first question is:what exactly do you want to your view ?

  • I'm not interested in the view, I just want a model, where I can store data and can retrieve it in a proper manner for further processing. It's just like a data storing in variable and then keep it open for further processing but in model way.

    I'm not sure does this possible or good way for this, whatever I understood from a model, where we can store data and it's fast and easy to retrieve data.

  • @npatil15
    Qt models are two-dimensional row/column indexed. Presumably at some level your outer QHash might be the rows and the inner QHash be the columns. The inner QList would be the data.

    However, I'll just throw this out there. Ready to be shot down, but ISTM a row/column model will not mesh well with QHashs. So far as I can see, Qt QHash does not even offer an option to access its elements via an index number, only via a key, which for you is QString. And QHash elements are not ordered, you cannot "insert at element 10", etc. Although there will be some way of manipulating it suitably for numeric indexes, I'm thinking it would not work well....

  • Lifetime Qt Champion

    Just as a note.
    If you are not planning on using any views with the model im not sure its worth it since you would then read your
    data using QModelIndexes and have to maintain working indexes across add/edit/delete.

  • @npatil15
    As @mrjj has written, if you're not interested in using Qt views you probably don't want to go down the QAbstractModel route. Your data organisation does not mesh well with Qt's models' rows/columns, so don't bother forcing a square peg into a round hole.

    There is nothing magic or set-in-stone about Qt's models, or for that matter views. It's just an implementation of good practice for managing data and keeping the data manipulation separate from the presentation.

    So use Qt's model methods as an inspiration for what you might provide. Your outer QHash might be like rows, and the inner QHash like columns. But unlike Qt models, yours will be indexed by (QString, QString) instead of (int, int). If you do want views, you can invent your own signals & slots like Qt models-views have, but passing around QStrings as arguments instead of ints.

    For your "fast and easy to retrieve data", nothing will be faster/easier than sticking with QStrings as indexes instead of ints.

Log in to reply