Qt World Summit: Submit your Presentation

[solved] QStandardItemModel deep copy

  • Hi there!
    I'm writing a function that updates the contents of a QStandardItemModel that has a more complex structure than just tree or table shape (it's actually a combination of both, having tables as children). For security, instead of immediately altering the actual model, my function builds up a temporary model and, if successful, replaces the actual model by the temporary one.
    Of course, I can't just set the model pointer to the temporary model, as there are widgets pointing to the old model (which would then be destroyed). Therefore I decided to keep the old model and first clear it, then copy the contents of the temporary model in to the actual model.
    Unfortunately, I don't know how to get a deep copy of the temporary model. Does anyone have any ideas for deep copy, or even an alternative approach to solve my problem?
    Thx in advance!

  • Lifetime Qt Champion


    If I understand you correctly, you have a custom structure/class that handles your data, right ? If so, what about just changing that structure with the new one ?

  • Hi Samuel,
    that's exactly what I would like to do. However, I can't figure out how to do that without calling setModel() again for all widgets that point to the model.

  • Lifetime Qt Champion

    Add your own API to exchange/update/replace your data structure. Doing so, it becomes your model's role to propagate the fact that there are new data arriving (which he already does). No need to replace any model

  • Sorry if this question is somewhat ridiculous, but I don't see how I could backup the old model with such an implementation. My goal is to update the model and revert those changes in case of failure.

  • Lifetime Qt Champion

    What changes between the new and old is that the new model internal data structure changes so if you have your own model, you can share the new model internal data with the soon to be replaced one.

  • Thank you Samuel!

  • Lifetime Qt Champion

    You're welcome !

    If you have everything running now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply