ItemWidgets: add supportedDragActions()/setSupportedDragActions()

In Qt6 the deprecated function QAIM::setSupportedDragActions() was
removed so the itemwidgets had no possibility to specify the drag
actions anymore. Fix this by adding it to the itemwidgets themselves.

Done-with: David Faure <david.faure@kdab.com>
Fixes: QTBUG-87465
Change-Id: Ib10fa30c5b2c9e8e5cd3f0e1c2664f7a12fb249b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2025-02-12 10:07:48 +01:00 committed by David Faure
parent da1fbf6053
commit 7d0017cda8
12 changed files with 173 additions and 15 deletions

View File

@ -431,6 +431,11 @@ Qt::DropActions QListModel::supportedDropActions() const
}
#endif // QT_CONFIG(draganddrop)
Qt::DropActions QListModel::supportedDragActions() const
{
return view()->supportedDragActions();
}
/*!
\class QListWidgetItem
\brief The QListWidgetItem class provides an item for use with the
@ -1804,7 +1809,7 @@ QMimeData *QListWidget::mimeData(const QList<QListWidgetItem *> &items) const
with the given \a action in the given \a index. Returns \c true if \a data and
\a action can be handled by the model; otherwise returns \c false.
\sa supportedDropActions()
\sa supportedDropActions(), supportedDragActions()
*/
bool QListWidget::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
{
@ -1829,7 +1834,7 @@ void QListWidget::dropEvent(QDropEvent *event)
/*!
Returns the drop actions supported by this view.
\sa Qt::DropActions
\sa Qt::DropActions, supportedDragActions(), dropMimeData()
*/
Qt::DropActions QListWidget::supportedDropActions() const
{
@ -1838,6 +1843,31 @@ Qt::DropActions QListWidget::supportedDropActions() const
}
#endif // QT_CONFIG(draganddrop)
/*!
Returns the drag actions supported by this view.
\since 6.10
\sa Qt::DropActions, setSupportedDragActions(), supportedDropActions()
*/
Qt::DropActions QListWidget::supportedDragActions() const
{
Q_D(const QListWidget);
return d->supportedDragActions.value_or(supportedDropActions());
}
/*!
Sets the drag \a actions supported by this view.
\since 6.10
\sa Qt::DropActions, supportedDragActions()
*/
void QListWidget::setSupportedDragActions(Qt::DropActions actions)
{
Q_D(QListWidget);
d->supportedDragActions = actions;
}
/*!
Returns a list of pointers to the items contained in the \a data object. If
the object was not created by a QListWidget in the same process, the list

View File

@ -221,6 +221,9 @@ public:
QModelIndex indexFromItem(const QListWidgetItem *item) const;
QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
void setSupportedDragActions(Qt::DropActions actions);
Qt::DropActions supportedDragActions() const;
protected:
#if QT_CONFIG(draganddrop)
void dropEvent(QDropEvent *event) override;

View File

@ -98,6 +98,7 @@ public:
int row, int column, const QModelIndex &parent) override;
Qt::DropActions supportedDropActions() const override;
#endif
Qt::DropActions supportedDragActions() const override;
QMimeData *internalMimeData() const;
private:
@ -126,9 +127,10 @@ public:
void emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
void sort();
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
Qt::SortOrder sortOrder;
bool sortingEnabled;
std::optional<Qt::DropActions> supportedDragActions;
std::array<QMetaObject::Connection, 8> connections;
std::array<QMetaObject::Connection, 2> selectionModelConnections;
};

View File

@ -869,6 +869,12 @@ Qt::DropActions QTableModel::supportedDropActions() const
return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
}
Qt::DropActions QTableModel::supportedDragActions() const
{
const QTableWidget *view = this->view();
return (view ? view->supportedDragActions() : Qt::DropActions(Qt::IgnoreAction));
}
/*!
\class QTableWidgetSelectionRange
@ -2605,7 +2611,7 @@ QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem *> &items) const
Returns \c true if the data and action can be handled by the model;
otherwise returns \c false.
\sa supportedDropActions()
\sa supportedDropActions(), supportedDragActions()
*/
bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
{
@ -2624,13 +2630,38 @@ bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::
/*!
Returns the drop actions supported by this view.
\sa Qt::DropActions
\sa Qt::DropActions, supportedDragActions(), dropMimeData()
*/
Qt::DropActions QTableWidget::supportedDropActions() const
{
return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction;
}
/*!
Returns the drag actions supported by this view.
\since 6.10
\sa Qt::DropActions, setSupportedDragActions(), supportedDropActions()
*/
Qt::DropActions QTableWidget::supportedDragActions() const
{
Q_D(const QTableWidget);
return d->supportedDragActions.value_or(supportedDropActions());
}
/*!
Sets the drag \a actions supported by this view.
\since 6.10
\sa Qt::DropActions, supportedDragActions()
*/
void QTableWidget::setSupportedDragActions(Qt::DropActions actions)
{
Q_D(QTableWidget);
d->supportedDragActions = actions;
}
/*!
Returns a list of pointers to the items contained in the \a data object.
If the object was not created by a QTreeWidget in the same process, the list

View File

@ -265,6 +265,9 @@ public:
const QTableWidgetItem *itemPrototype() const;
void setItemPrototype(const QTableWidgetItem *item);
Qt::DropActions supportedDragActions() const;
void setSupportedDragActions(Qt::DropActions actions);
public Q_SLOTS:
void scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible);
void insertRow(int row);

View File

@ -136,6 +136,7 @@ public:
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent) override;
Qt::DropActions supportedDropActions() const override;
Qt::DropActions supportedDragActions() const override;
QMimeData *internalMimeData() const;
@ -173,6 +174,7 @@ public:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
std::array<QMetaObject::Connection, 10> connections;
std::optional<Qt::DropActions> supportedDragActions;
};
class QTableWidgetItemPrivate

View File

@ -737,6 +737,11 @@ Qt::DropActions QTreeModel::supportedDropActions() const
return view()->supportedDropActions();
}
Qt::DropActions QTreeModel::supportedDragActions() const
{
return view()->supportedDragActions();
}
void QTreeModel::itemChanged(QTreeWidgetItem *item)
{
if (item->columnCount() <= 0)
@ -3193,7 +3198,7 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem *> &items) const
successfully handled by decoding the mime data and inserting it
into the model; otherwise it returns \c false.
\sa supportedDropActions()
\sa supportedDropActions(), supportedDragActions()
*/
bool QTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index,
const QMimeData *data, Qt::DropAction action)
@ -3206,13 +3211,38 @@ bool QTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index,
/*!
Returns the drop actions supported by this view.
\sa Qt::DropActions
\sa Qt::DropActions, supportedDragActions(), dropMimeData()
*/
Qt::DropActions QTreeWidget::supportedDropActions() const
{
return model()->QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
}
/*!
Returns the drag actions supported by this view.
\since 6.10
\sa Qt::DropActions, setSupportedDragActions(), supportedDropActions()
*/
Qt::DropActions QTreeWidget::supportedDragActions() const
{
Q_D(const QTreeWidget);
return d->supportedDragActions.value_or(supportedDropActions());
}
/*!
Sets the drag \a actions supported by this view.
\since 6.10
\sa Qt::DropActions, supportedDragActions()
*/
void QTreeWidget::setSupportedDragActions(Qt::DropActions actions)
{
Q_D(QTreeWidget);
d->supportedDragActions = actions;
}
/*!
Returns the QModelIndex associated with the given \a item in the given \a column.

View File

@ -288,6 +288,9 @@ public:
void setSelectionModel(QItemSelectionModel *selectionModel) override;
Qt::DropActions supportedDragActions() const;
void setSupportedDragActions(Qt::DropActions actions);
public Q_SLOTS:
void scrollToItem(const QTreeWidgetItem *item,
QAbstractItemView::ScrollHint hint = EnsureVisible);

View File

@ -96,6 +96,7 @@ public:
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent) override;
Qt::DropActions supportedDropActions() const override;
Qt::DropActions supportedDragActions() const override;
QMimeData *internalMimeData() const;
@ -210,6 +211,7 @@ public:
int explicitSortColumn;
std::array<QMetaObject::Connection, 12> connections;
std::optional<Qt::DropActions> supportedDragActions;
};
QT_END_NAMESPACE

View File

@ -100,15 +100,14 @@ private slots:
void QTBUG50891_ensureSelectionModelSignalConnectionsAreSet();
void createPersistentOnLayoutAboutToBeChanged();
void createPersistentOnLayoutAboutToBeChangedAutoSort();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void clearItemData();
#endif
void moveRows_data();
void moveRows();
void moveRowsInvalid_data();
void moveRowsInvalid();
void noopDragDrop();
void supportedDragActions();
protected slots:
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
@ -2002,7 +2001,6 @@ void tst_QListWidget::noopDragDrop() // QTBUG-100128
CHECK_ITEM;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void tst_QListWidget::clearItemData()
{
QListWidget list;
@ -2022,7 +2020,25 @@ void tst_QListWidget::clearItemData()
QVERIFY(list.model()->clearItemData(list.model()->index(0, 0)));
QCOMPARE(dataChangeSpy.size(), 0);
}
#endif
class MoveOnlyListWidget : public QListWidget
{
Q_OBJECT
public:
using QListWidget::QListWidget;
Qt::DropActions supportedDropActions() const override { return Qt::MoveAction; }
};
void tst_QListWidget::supportedDragActions()
{
MoveOnlyListWidget listWidget;
QCOMPARE(listWidget.model()->supportedDropActions(), Qt::MoveAction);
// For Qt < 6.8 compatibility reasons, supportedDragActions defaults to supportedDropActions
QCOMPARE(listWidget.model()->supportedDragActions(), Qt::MoveAction);
listWidget.setSupportedDragActions(Qt::CopyAction);
QCOMPARE(listWidget.model()->supportedDragActions(), Qt::CopyAction);
}
QTEST_MAIN(tst_QListWidget)
#include "tst_qlistwidget.moc"

View File

@ -87,6 +87,7 @@ private slots:
void moveRows();
void moveRowsInvalid_data();
void moveRowsInvalid();
void supportedDragActions();
private:
std::unique_ptr<QTableWidget> testWidget;
@ -2047,5 +2048,24 @@ void tst_QTableWidget::moveRowsInvalid()
delete baseWidget;
}
class MoveOnlyTableWidget : public QTableWidget
{
Q_OBJECT
public:
using QTableWidget::QTableWidget;
Qt::DropActions supportedDropActions() const override { return Qt::MoveAction; }
};
void tst_QTableWidget::supportedDragActions()
{
MoveOnlyTableWidget tableWidget;
QCOMPARE(tableWidget.model()->supportedDropActions(), Qt::MoveAction);
// For Qt < 6.8 compatibility reasons, supportedDragActions defaults to supportedDropActions
QCOMPARE(tableWidget.model()->supportedDragActions(), Qt::MoveAction);
tableWidget.setSupportedDragActions(Qt::CopyAction);
QCOMPARE(tableWidget.model()->supportedDragActions(), Qt::CopyAction);
}
QTEST_MAIN(tst_QTableWidget)
#include "tst_qtablewidget.moc"

View File

@ -130,9 +130,8 @@ private slots:
void persistentChildIndex();
void createPersistentOnLayoutAboutToBeChanged();
void createPersistentOnLayoutAboutToBeChangedAutoSort();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void clearItemData();
#endif
void supportedDragActions();
public slots:
void itemSelectionChanged();
@ -3589,7 +3588,6 @@ void tst_QTreeWidget::persistentChildIndex() // QTBUG-90030
QCOMPARE(persistentIdx.data().toString(), QStringLiteral("child2"));
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void tst_QTreeWidget::clearItemData()
{
QTreeWidget tree;
@ -3625,7 +3623,6 @@ void tst_QTreeWidget::clearItemData()
QCOMPARE(dataChangeArgs.at(1).value<QModelIndex>(), childIdx);
QVERIFY(dataChangeArgs.at(2).value<QList<int>>().isEmpty());
}
#endif
void tst_QTreeWidget::createPersistentOnLayoutAboutToBeChanged() // QTBUG-93466
{
@ -3694,5 +3691,24 @@ void tst_QTreeWidget::createPersistentOnLayoutAboutToBeChangedAutoSort() // QTBU
QCOMPARE(layoutChangedSpy.size(), 1);
}
class MoveOnlyTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
using QTreeWidget::QTreeWidget;
Qt::DropActions supportedDropActions() const override { return Qt::MoveAction; }
};
void tst_QTreeWidget::supportedDragActions()
{
MoveOnlyTreeWidget treeWidget;
QCOMPARE(treeWidget.model()->supportedDropActions(), Qt::MoveAction);
// For Qt < 6.8 compatibility reasons, supportedDragActions defaults to supportedDropActions
QCOMPARE(treeWidget.model()->supportedDragActions(), Qt::MoveAction);
treeWidget.setSupportedDragActions(Qt::CopyAction);
QCOMPARE(treeWidget.model()->supportedDragActions(), Qt::CopyAction);
}
QTEST_MAIN(tst_QTreeWidget)
#include "tst_qtreewidget.moc"