Different behavior between setData() and user editing for QAbstractTableModel
-
I wrote a class derived from
QAbstractTableModeland mysetData()function is like this:struct item_t { int idx; quint64 val; }; bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (index.row() >= _items.count()) return false; item_t item = _items.at(index.row()); switch (role) { case Qt::EditRole: switch (index.column()) { case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break; case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break; default: break; } break; } (_items)[index.row()] = item; emit dataChanged(index, index); return true; }I need to have a different behavior when I programmatically call
setData(), i.e. when I fill the model and when the user edits the cells.Is there a specific role for this?
My final goal is the following. The item.val is defined as
quint64but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16)); -
I wrote a class derived from
QAbstractTableModeland mysetData()function is like this:struct item_t { int idx; quint64 val; }; bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (index.row() >= _items.count()) return false; item_t item = _items.at(index.row()); switch (role) { case Qt::EditRole: switch (index.column()) { case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break; case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break; default: break; } break; } (_items)[index.row()] = item; emit dataChanged(index, index); return true; }I need to have a different behavior when I programmatically call
setData(), i.e. when I fill the model and when the user edits the cells.Is there a specific role for this?
My final goal is the following. The item.val is defined as
quint64but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16));@Mark81
you are free to define custom roles and use them as you like to distinguish your cases.Just make sure your custom role starts off from the value of
Qt::UserRole:enum ItemRoles { MyItemRole = Qt::UserRole+1 }; ... model->setData( index, data, MyItemRole ); -
@Mark81
you are free to define custom roles and use them as you like to distinguish your cases.Just make sure your custom role starts off from the value of
Qt::UserRole:enum ItemRoles { MyItemRole = Qt::UserRole+1 }; ... model->setData( index, data, MyItemRole );@raven-worx Ok, so as far as I understand the built-in edit of the cell will use
EditRole. Simple enough to change my code, then. Thanks! -
I wrote a class derived from
QAbstractTableModeland mysetData()function is like this:struct item_t { int idx; quint64 val; }; bool ModelLocalWhiteList::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (index.row() >= _items.count()) return false; item_t item = _items.at(index.row()); switch (role) { case Qt::EditRole: switch (index.column()) { case MODEL_IDX: item.idx = static_cast<quint16>(value.toUInt()); break; case MODEL_VAL: item.val = static_cast<quint64>(value.toLongLong()); break; default: break; } break; } (_items)[index.row()] = item; emit dataChanged(index, index); return true; }I need to have a different behavior when I programmatically call
setData(), i.e. when I fill the model and when the user edits the cells.Is there a specific role for this?
My final goal is the following. The item.val is defined as
quint64but when I set it from the code I pass a literal value to the function. Instead, when the user edits it he should enter the data as an hex. Hence I should change the conversion to:item.val = static_cast<quint64>(value.toString().toLongLong(nullptr, 16));@Mark81 said in Different behavior between setData() and user editing for QAbstractTableModel:
when the user edits [...] I should change the conversion to:
This kind of stuff is the job of the delegate. subclass
QStyledItemDelegate, reimplementsetModelDataand do the conversion there.
The model should not care about who/what is callingsetData