diff --git a/src/widgets/itemviews/qlistwidget.cpp b/src/widgets/itemviews/qlistwidget.cpp index 3b810e64359..bffc1eadb6b 100644 --- a/src/widgets/itemviews/qlistwidget.cpp +++ b/src/widgets/itemviews/qlistwidget.cpp @@ -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 &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 diff --git a/src/widgets/itemviews/qlistwidget.h b/src/widgets/itemviews/qlistwidget.h index c6ba714c43d..ef2dcfb21eb 100644 --- a/src/widgets/itemviews/qlistwidget.h +++ b/src/widgets/itemviews/qlistwidget.h @@ -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; diff --git a/src/widgets/itemviews/qlistwidget_p.h b/src/widgets/itemviews/qlistwidget_p.h index 73bf9d509e4..acd48ed5602 100644 --- a/src/widgets/itemviews/qlistwidget_p.h +++ b/src/widgets/itemviews/qlistwidget_p.h @@ -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 ¤t, const QModelIndex &previous); void sort(); void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); + Qt::SortOrder sortOrder; bool sortingEnabled; - + std::optional supportedDragActions; std::array connections; std::array selectionModelConnections; }; diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp index 5d98961b0ed..5f5f22451d0 100644 --- a/src/widgets/itemviews/qtablewidget.cpp +++ b/src/widgets/itemviews/qtablewidget.cpp @@ -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 &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 diff --git a/src/widgets/itemviews/qtablewidget.h b/src/widgets/itemviews/qtablewidget.h index 303f4d5f5bf..833d79c19f1 100644 --- a/src/widgets/itemviews/qtablewidget.h +++ b/src/widgets/itemviews/qtablewidget.h @@ -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); diff --git a/src/widgets/itemviews/qtablewidget_p.h b/src/widgets/itemviews/qtablewidget_p.h index 0904f077c2e..34a4ade3155 100644 --- a/src/widgets/itemviews/qtablewidget_p.h +++ b/src/widgets/itemviews/qtablewidget_p.h @@ -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 connections; + std::optional supportedDragActions; }; class QTableWidgetItemPrivate diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index bca1fefe07f..fa4d7250b33 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -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 &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. diff --git a/src/widgets/itemviews/qtreewidget.h b/src/widgets/itemviews/qtreewidget.h index 992f6cdb1cf..19154e1e290 100644 --- a/src/widgets/itemviews/qtreewidget.h +++ b/src/widgets/itemviews/qtreewidget.h @@ -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); diff --git a/src/widgets/itemviews/qtreewidget_p.h b/src/widgets/itemviews/qtreewidget_p.h index ea2d5453af3..aaf0ff9ea27 100644 --- a/src/widgets/itemviews/qtreewidget_p.h +++ b/src/widgets/itemviews/qtreewidget_p.h @@ -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 connections; + std::optional supportedDragActions; }; QT_END_NAMESPACE diff --git a/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp b/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp index 14a6cee0d98..80dfa8be911 100644 --- a/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp +++ b/tests/auto/widgets/itemviews/qlistwidget/tst_qlistwidget.cpp @@ -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" diff --git a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp index b2be925a011..7f322e3d9d6 100644 --- a/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp +++ b/tests/auto/widgets/itemviews/qtablewidget/tst_qtablewidget.cpp @@ -87,6 +87,7 @@ private slots: void moveRows(); void moveRowsInvalid_data(); void moveRowsInvalid(); + void supportedDragActions(); private: std::unique_ptr 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" diff --git a/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp b/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp index f4423831ca4..0761d445afc 100644 --- a/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp +++ b/tests/auto/widgets/itemviews/qtreewidget/tst_qtreewidget.cpp @@ -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(), childIdx); QVERIFY(dataChangeArgs.at(2).value>().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"