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. How to replace model correctly?
Forum Updated to NodeBB v4.3 + New Features

How to replace model correctly?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • B Offline
    B Offline
    beedaddy
    wrote on last edited by
    #1

    Sorry for this newbie question, but I'm not sure how to do that correctly. I have a QTreeView and a custom model, derived from QAbstractItemModel. It can happen that the data, on which the model operates completely changes, that is, I pass a pointer to the data to the model and when the data changes, the pointer gets invalid. Thus, I try to remove the model beforehand and set it again after the data has changed.

    If I understood it correctly, I'm responsible for the deletion of the model, is that correct? Just doing a myTreeView->setModel(nullptr) would create a memory leak, I guess?

    And would it be feasible to do something like:

    delete ui_.salesView->model()
    // something that changes the data
    ui_.salesView->setModel(new SaleModel(getMarketplace(), ui_.salesView));
    

    Or is there a better way to handle this situation?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by VRonin
      #2

      The best way is this, oherwise the view will access to a dangling pointer during setModel() since it will disconnect the old model which is already deleted...

       auto model = ui.salesView->model();
       ui_.salesView->setModel(new SaleModel(getMarketplace(), ui_.salesView));
       delete model;
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in How to replace model correctly?:

        oherwise the view will access to a dangling pointer during setModel()

        Views connect to destroyed() signal of the model and detach from it automatically when it is destroyed so this is not true. At the call to setModel() there will already be no model attached to the view.

        So both ways are entirely correct.

        1 Reply Last reply
        2
        • B Offline
          B Offline
          beedaddy
          wrote on last edited by
          #4

          Thanks a lot. So deleting the model is important and both ways are ok.

          @Christian-Ehrlicher :
          Wouldn't it be wise to call

          ui_.salesView->setModel(nullptr)
          

          directly after

          auto model = ui.salesView->model();
          

          to make sure that the model can't operate on invalid data. Or is that not a problem?

          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