bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if ( !index.isValid() ) { return false; } TableItem *itm = item(index); if (itm) { itm->setData(role, value); return true; } // don't create dummy table items for empty values if ( !value.isValid() ) { return false; } TableView *view = qobject_cast<TableView*>( QObject::parent() ); if (!view) { return false; } itm = createItem(); itm->setData(role, value); view->setItem(index.row(), index.column(), itm); return true; }
bool TableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) { if ( !index.isValid() ) { return false; } TableView *view = qobject_cast<TableView*>( QObject::parent() ); TableItem *itm = item(index); if (itm) { itm->view = 0; // prohibits item from calling itemChanged() bool changed = false; for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) { if ( itm->data( it.key() ) != it.value() ) { itm->setData( it.key(), it.value() ); changed = true; } } itm->view = view; if (changed) { itemChanged(itm); } return true; } if (!view) { return false; } itm = createItem(); for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) { itm->setData( it.key(), it.value() ); } view->setItem(index.row(), index.column(), itm); return true; }