QAbstractItemView: call canDropMimeData, as one would expect.
The virtual method was added for 5.0 but never called. The old code (only checking mimetypes) is now the default implementation for canDropMimeData. Model subclasses can now refine this by having index-specific logic instead, or in order to inspect the dropped data (e.g. to accept files and refuse directories, which are all text/uri-list). [ChangeLog][QtWidgets][QAbstractItemView] now calls canDropMimeData in order to decide whether or not to accept the drop. Task-number: QTBUG-30534 Change-Id: Ied3aa964b4025bae6a1a26df89a681bfe61c3faa Reviewed-by: Stephen Kelly <steveire@gmail.com>
This commit is contained in:
parent
736ac19156
commit
b13aa15e10
@ -1842,7 +1842,9 @@ QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
|
||||
|
||||
/*!
|
||||
Returns \c{true} if a model can accept a drop of the \a data. This
|
||||
default implementation always returns \c{true}.
|
||||
default implementation only checks if \a data has at least one format
|
||||
in the list of mimeTypes() and if \a action is among the
|
||||
model's supportedDropActions().
|
||||
|
||||
Reimplement this function in your custom model, if you want to
|
||||
test whether the \a data can be dropped at \a row, \a column,
|
||||
@ -1855,12 +1857,19 @@ bool QAbstractItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction a
|
||||
int row, int column,
|
||||
const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(data)
|
||||
Q_UNUSED(action)
|
||||
Q_UNUSED(row)
|
||||
Q_UNUSED(column)
|
||||
Q_UNUSED(parent)
|
||||
return true;
|
||||
|
||||
if (!(action & supportedDropActions()))
|
||||
return false;
|
||||
|
||||
const QStringList modelTypes = mimeTypes();
|
||||
for (int i = 0; i < modelTypes.count(); ++i) {
|
||||
if (data->hasFormat(modelTypes.at(i)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -38,7 +38,7 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
|
@ -38,7 +38,7 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtGui>
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "model.h"
|
||||
|
@ -65,18 +65,31 @@ DragDropListModel::DragDropListModel(const QStringList &strings,
|
||||
}
|
||||
|
||||
//! [0]
|
||||
bool DragDropListModel::dropMimeData(const QMimeData *data,
|
||||
bool DragDropListModel::canDropMimeData(const QMimeData *data,
|
||||
Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
if (action == Qt::IgnoreAction)
|
||||
return true;
|
||||
Q_UNUSED(action);
|
||||
Q_UNUSED(row);
|
||||
Q_UNUSED(parent);
|
||||
|
||||
if (!data->hasFormat("application/vnd.text.list"))
|
||||
return false;
|
||||
|
||||
if (column > 0)
|
||||
//! [0] //! [1]
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
//! [0]
|
||||
//! [1]
|
||||
bool DragDropListModel::dropMimeData(const QMimeData *data,
|
||||
Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
if (!canDropMimeData(data, action, row, column, parent))
|
||||
return false;
|
||||
|
||||
if (action == Qt::IgnoreAction)
|
||||
return true;
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
|
@ -63,6 +63,8 @@ public:
|
||||
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const;
|
||||
|
||||
bool canDropMimeData(const QMimeData *data, Qt::DropAction action,
|
||||
int row, int column, const QModelIndex &parent);
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
|
||||
int row, int column, const QModelIndex &parent);
|
||||
QMimeData *mimeData(const QModelIndexList &indexes) const;
|
||||
|
@ -3,3 +3,4 @@ SOURCES = main.cpp \
|
||||
model.cpp
|
||||
HEADERS = mainwindow.h \
|
||||
model.h
|
||||
QT += widgets
|
||||
|
@ -1769,6 +1769,9 @@
|
||||
dropped onto existing items separately to data dropped into the top level
|
||||
of the model (i.e., onto an invalid item).
|
||||
|
||||
Models can forbid dropping on certain items, or depending on the dropped data,
|
||||
by reimplementing QAbstractItemModel::canDropMimeData().
|
||||
|
||||
The model first has to make sure that the operation should be acted on,
|
||||
the data supplied is in a format that can be used, and that its destination
|
||||
within the model is valid:
|
||||
|
@ -164,13 +164,15 @@ public:
|
||||
#ifndef QT_NO_DRAGANDDROP
|
||||
virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
|
||||
|
||||
inline bool canDecode(QDropEvent *e) const {
|
||||
QStringList modelTypes = model->mimeTypes();
|
||||
const QMimeData *mime = e->mimeData();
|
||||
for (int i = 0; i < modelTypes.count(); ++i)
|
||||
if (mime->hasFormat(modelTypes.at(i))
|
||||
&& (e->dropAction() & model->supportedDropActions()))
|
||||
return true;
|
||||
inline bool canDecode(QDropEvent *event) {
|
||||
QModelIndex index;
|
||||
int col = -1;
|
||||
int row = -1;
|
||||
if (dropOn(event, &row, &col, &index)) {
|
||||
return model->canDropMimeData(event->mimeData(),
|
||||
dragDropMode == QAbstractItemView::InternalMove ? Qt::MoveAction : event->dropAction(),
|
||||
row, col, index);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user