Is QStyledItemDelegate::createEditor returned value monitored?



  • Hi all,

    I'm working on QTableView with a combo box inside. I chose QStyledItemDelegate for my combo box display.

    By doing this method, QWidget* createEditor(...) and other methods need to be re-implemented. I can say, everytime a cell containing ComboBox in QTableView is in edit mode, this createEditor is called.

    My source code is referred at https://wiki.qt.io/Combo_Boxes_in_Item_Views as below

    QWidget* ComboBoxItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        // ComboBox ony in column 2
        if (index.column() != 1)
            return QStyledItemDelegate::createEditor(parent, option, index);
    
        // Create the combobox and populate it
        QComboBox* cb = new QComboBox(parent);
        int row = index.row();
        cb->addItem(QString("one in row %1").arg(row));
        cb->addItem(QString("two in row %1").arg(row));
        cb->addItem(QString("three in row %1").arg(row));
        return cb;
    }
    

    My question is, is the pointer cb monitored? I mean, when will it be deleted and who has responsibility to do that? Because as I mentioned above, when a cell contains comboBox is clicked for editing, this function is called which means a lot of memory areas are allocated.

    Thank you.


  • Qt Champions 2017

    It is taken care. All the editors will be set with parent. When you delete the parent, editors will be automatically deleted. You don't have to anything extra.



  • One side note: if (index.column() inside a delegate is bad design as it ties the delegate to a specific model. use QAbstractItemView::setItemDelegateForColumn instead



  • @dheerendra As example at above link https://wiki.qt.io/Combo_Boxes_in_Item_Views, in the function main(), the ComboBoxItemDelegate* cbid object is a child of parent QTableWidget tw;

    As you said, as long as the tw object is alive, the cbid object is still there. That means, a lot of cbid objects will be created when clicking a cell containing combo box for editing. Do I understand it correctly?



  • @VRonin Thank you for your suggestion. Let me study QAbstractItemView::setItemDelegateForColumn as you mentioned first.



  • @tanmanh0707 said in Is QStyledItemDelegate::createEditor returned value monitored?:

    as long as the tw object is alive, the cbid object is still there

    Nope, the view will call QAbstractItemDelegate::destroyEditor once it doesn't need the editor anymore



  • @VRonin Thank you very much. I am clear now



  • @VRonin said in Is QStyledItemDelegate::createEditor returned value monitored?:

    One side note: if (index.column() inside a delegate is bad design as it ties the delegate to a specific model. use QAbstractItemView::setItemDelegateForColumn instead

    Hi VRonin again,

    I just want to use delegate on some cells, not a whole column nor row. Is there any way to do that without using if (index.column()) if (index.row())



  • In these cases you normally use a QItemEditorFactory in setItemEditorFactory to create the editor you need for the cell based on the data in the model


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.