I solved the second problem. To allow drops only outside the items and not over the items I use the following flags reimplementation in my model:
Qt
::ItemFlags FieldModel
::flags(const QModelIndex &index
) const{
if (!index.isValid() || index.row() >= m_fields.count() || index.model() != this)
return Qt::ItemIsDropEnabled; // we allow drops outside the items
return QAbstractListModel::flags(index
) | Qt
::ItemIsUserCheckable | Qt
::ItemIsDragEnabled;
}
Qt::ItemFlags FieldModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || index.row() >= m_fields.count() || index.model() != this)
return Qt::ItemIsDropEnabled; // we allow drops outside the items
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
}
To copy to clipboard, switch view to plain text mode
This solution is taken from qtbase/src/widgets/qlistwidget.cpp.
Edit: I also solved the first problem. These are the correct reimplementations of insertRows and removeRows in my case:
bool FieldModel
::insertRows(int row,
int count,
const QModelIndex &parent
) {
if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
return false;
for(int index = row, end = row + count; index < end; ++index)
m_fields.insert(index, p(KnownField::Invalid, false));
endInsertRows();
return true;
}
bool FieldModel
::removeRows(int row,
int count,
const QModelIndex &parent
) {
if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
return false;
for(int index = row, end = row + count; index < end; ++index)
m_fields.removeAt(index);
endRemoveRows();
return true;
}
bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || row > rowCount() || parent.isValid())
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
for(int index = row, end = row + count; index < end; ++index)
m_fields.insert(index, p(KnownField::Invalid, false));
endInsertRows();
return true;
}
bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (count < 1 || row < 0 || (row + count) > rowCount() || parent.isValid())
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for(int index = row, end = row + count; index < end; ++index)
m_fields.removeAt(index);
endRemoveRows();
return true;
}
To copy to clipboard, switch view to plain text mode
The problem was caused here: beginInsertRows(parent, row, 0);
The third argument has to be row + count - 1.
Bookmarks