[Development] Drag 'n' drop with QSqlTableModel
Dmitrii Volosnykh
dmitrii.volosnykh at gmail.com
Tue May 7 20:01:14 CEST 2013
Hi, all!
I am using QTreeView with QAbstractItemModel-inherited model which
internally uses QSqlTableModel. I have successfully added DnD support
regarding item re-parenting. Unfortunately, data is not copied to the
destination row.
QAbstractTableModel::dropMimeData() finally calls
QAbstractItemModel::decodeData() which tries to write to the newly
created row role-data from the old row. For each index it calls
QAbstractItemModel::setItemData() which, in turn, depends on
setData().
As for QSqlTableModel, setData() has the following lines:
if (role != Qt::EditRole)
return QSqlQueryModel::setData(index, value, role);
This means QAbstractItemModel's default implementation of setData() is
called. It always returns false, thus, making
QSqlTableModel::setData() and, as a consequence, setItemData() return
false.
This is reproducible always because of Qt::DisplayRole is also
returned in resulting QMap from QAbstractItemModel::itemData().
I am not sure, if this should be considered as a bug and which of the
mentioned methods needs to be modified in order to properly treat this
case.
Obviously, itemData() is a generic method, so it must include
Qt::DisplayRole. The same true for QSqlTableModel::setData(): it
should fail for Qt::DisplayRole. So, changes should somewhere in
between these two. I guess, setItemData() could be reimplemented in
QSqlTableModel so that it removes Qt::DisplayRole and call
QAbstractTableModel::setItemData() after that.
Regards,
Dmitrii.
More information about the Development
mailing list