Important: Please read the Qt Code of Conduct -

Is QStandardItemModel and QStandardItem destuction needed?

  • If I use the following code,

    1. Will QStandardItemModel be deleted automatically when it is replaced by new QStandardItemModel?

    2. If use 3rd code, new on QStandardItem only, how QStandardItem will be deleted?


    @QStandardItemModel *ElementList::getTableModel()
    QStandardItemModel model(this->count(),2); //x Rows and 5 Columns
    model.setHorizontalHeaderItem(0, new QStandardItem(QString("El")));
    model.setHorizontalHeaderItem(1, new QStandardItem(QString("%")));
    for(int i=0; i < this->count();i++){
    ResultElement re=this->at(i);
    model.setItem(i,0,new QStandardItem(re.getName()));
    model.setItem(i,1,new QStandardItem(QString::number(re.percentage())));
    return model;
    @QStandardItemModel *tableModel = list->getTableModel();
    qDebug() << tableModel->parent();
    qDebug() << tableModel->parent();@

    @QStandardItemModel model(4, 4);
    for (int row = 0; row < 4; ++row) {
    for (int column = 0; column < 4; ++column) {
    QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
    model.setItem(row, column, item);

  • Lifetime Qt Champion


    It's all explained in the "setItem": documentation

Log in to reply