Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Metadata in model?



  • Hi everybody,

    I'm kinda new to Qt and it seems I've chosen a bit of a complex application I want to write with it, so if anybody could give me some hints on what to read in the docs to proceed would be nice.

    What I have is a small SQLite Database that contains data from something like a survey. I imagine to have similar to a multiple choice test a number of radiobuttons or checkboxes that signify certain integer values, each going with a question or exercise. But the number of the radiobuttons / checkboxes is dependent on the certain question / exercise. You'd also have "superquestions" that contain several questions, also the info about what belongs where is in the database. Also there are several levels of embedding possible.

    Now I read about Models and Views and figured I'd write my own QAbstractItem Model to contain the database (or parts thereof) and use QWidgetmapper to map everything to a number of widgets (Radiobuttons, Groupboxes, Labels...) and grab the input to the Radiobuttons to write values back to the database.

    What I am not sure about is what data to actually put into the model. It feels wrong (sorry, I cannot explain better) to have metadata that describes how many radiobuttons for a certain question be put in the same model as the question texts and the results that will be input. From what I've seen in tutorials models always only contained data that was edited, not metadata on the structure of the data.

    So my one question would be if my feeling is right and there's a better way to solve this and have data and metadata seperated.
    My second question would be, if there is a yes to the first question: How to tackle this. What do I need to know / read. Which concepts are there I am yet unaware of that would help me solve my problem.

    Thank you for reading and stay healthy or get well soon if you aren't.


  • Lifetime Qt Champion

    Hi and welcome to the forums
    Since the metadata describes how the view should show the actual data, its actually fine to include it in
    a custom model. Qt uses Roles for accessing the data has support for userRoles that is perfect for
    data that you don't want to edit but use by delegates or the view for the visualization.
    Qt includes Font and Color rules so in that regards its does also include metadata.
    https://doc.qt.io/qt-5/modelview.html
    Section "2.2 Extending the Read Only Example with Roles"

    So it's not inherently wrong to include metadata in the model but can pose practical issues like
    having to filter out columns when using a std view and that sort of things.

    Things to be aware of.
    We do have direct SQL models.
    https://doc.qt.io/qt-5/examples-sql.html
    Not that you must use it but it does give easy editing as it can write back the edited data on its own.
    However, it sounds like your structure will be many tables in the DB so not sure any of the predefined models will fit well.

    Also, notice the delegate concept. It's used for custom drawing in views including buttons and checkboxes in cells.

    Its hard to give detailed advice as im not sure what this metadata will be like.
    I do understand the overall concept and you will need to store in the DB.

    However, since you are using QWidgetmapper and real widgets, it might be easier to have the metadata in some other structure since QWidgetmapper only exposes a section to the Widget and the widget has no easy way to ask the model about the meta via QWidgetmapper so you need to provide access somehow anyway.
    But you could make a custom widget for the actual virtualization and give it the model. It would then
    set up the widgets and hook up to the QWidgetmapper. In such case, the model would be good place to store it.

    Do note that starting out with a QAbstractItem model is pretty hard core. :)



  • Thank you for your detailed answer. Now I know where to go and where to look to make things work.
    And yes, I noticed it's a bit more challenging that your average 'Hello World' program ;)


Log in to reply