QTableView, QStandardItem - Ownership

  • Hi All,

    My first time working with a TableView and all that jazz...

    I have a couple questions. I'm basically creating a value editor where the first column will be a property name or value name, not editable. The second column will be the value that the user can edit. Example

    Bay2_SPV_PortA 67888
    Bay2_SPC_PortB 98540

    So in this case the names on the left would not be editable, values on right would. I've figured out how to set a QStandardItem to be editable so I think I have that handled.

    My specific questions are this:

    1. Ownership of the created QStandardItems... I have a loop that basically does the following:

       QStandardItem *item;
       QMapIterator<QString,inst_InstrumentPosition *> i(m_valuemap);
       while( i.hasNext() )
         inst_InstrumentPosition *pos = i.value();
         if( pos!=nullptr )
           item = new QStandardItem( pos->Name );
           model->setItem( row, 0, item);
           item = new QStandardItem( pos->Value.toString() );
           model->setItem( row, 1, item );

    It iterates through my map, creates a QStandardItem for the name and another QStandardItem for the value.

    Question: Who owns all these created QStandardItems? How do they get released?

    1. Next question: Editing. The above loop populates the TableView nicely and as I want the second column is editable. What is not clear is how to know that something has been edited so I can gather the data back and update my original map. How is this normally done?

    I see the DataChanged signal but it doesn't seem to take any parameters so my question also is, the receiver of this signal... how do they know what to do upon reception?

    Also I might have 2000 of these. I assume I'd need to connect data changed to all editable QStandardItems.

    UPDATE: I see that dataChanged at the abstract level has a bunch of parameters. I'm not sure how to refactor those back to my original data. Hints would be appreciated.

    Thanks in advance.

  • Lifetime Qt Champion

    The model owns the item.

    also when using models and views, the data is in the model. so you can
    get the changed item with

    So to know what items belongs to what data , you can use
    to store a ID of sorts in the item. (like name) maybe.

    You can then use this ID/name to know what original data it is.
    (if needed)
    and save back the value from model to the original data. (like a map or list)

Log in to reply