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. Is it ok? - QStandardItemModel and ownership of item

Is it ok? - QStandardItemModel and ownership of item

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 375 Views
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on 1 Nov 2020, 15:11 last edited by
    #1

    Hi,

    A few weeks ago I ask:

    Have I delete qstandarditem when I setItem() in QStandardItemModel. I get answer:

    "The model owns the items and will delete them so no manually deleting is needed."

    And docs:
    https://doc.qt.io/qt-5/qstandarditemmodel.html#setItem

    Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted.
    

    Ok. So I check It:

        for(auto text: itemsText)
        {
            QStandardItem *item = new QStandardItem(text);
            wek.append(item);
            qInfo()<<item<<item->parent()<<"before";
            itemModel->setItem(itemModel->rowCount(),0,item);
            qInfo()<<item<<item->parent()<<"after";
        }
    

    wek is the vector of QStandardItems *. I get texts:

    0x2e9d780 0x0 before
    0x2e9d780 0x0 after
    0x2e9d400 0x0 before
    0x2e9d400 0x0 after
    0x2e9d4e0 0x0 before
    0x2e9d4e0 0x0 after
    

    I check wek after setModel() in QComboBox:

    setModel(itemModel);
        for(auto x:wek)
            qInfo()<<x<<x->parent();
    

    I get:

    0x2e9d780 0x0
    0x2e9d400 0x0
    0x2e9d4e0 0x0
    

    And my question: Are parents ok? I get 0x0 and when I read doc I think I should get parent = itemModel. Is it ok?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Nov 2020, 15:45 last edited by
      #2

      Ownership and qobject parent / child relationship are two similar but different things. Just because an object takes ownership of another object there is not need that one is the parent of the other.

      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
      3

      2/2

      1 Nov 2020, 15:45

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved