diff options
| author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-09-07 15:45:36 +0200 |
|---|---|---|
| committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2022-09-13 10:34:57 +0200 |
| commit | 4a11b82ea6ae4803cc817e59088f5586db53ed7b (patch) | |
| tree | 1f71106ec6250090e9f952d011f120310a31506b /src/quick/items/qquicktableview.cpp | |
| parent | 61314a2863f5fe055d8ff685076892c6cb0b3278 (diff) | |
QQuickTableView: document how to implement copy/paste support
Pick-to: 6.4
Change-Id: I9d60b4efdce4ebaf47a229325de8eb5d88cb5bff
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quick/items/qquicktableview.cpp')
| -rw-r--r-- | src/quick/items/qquicktableview.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/quick/items/qquicktableview.cpp b/src/quick/items/qquicktableview.cpp index 9aad32d177..ddc2d358d5 100644 --- a/src/quick/items/qquicktableview.cpp +++ b/src/quick/items/qquicktableview.cpp @@ -195,6 +195,67 @@ the model's \l {ItemSelectionModel::currentIndex}{currentIndex}. You can disable keyboard navigation fully (in case you want to implement your own key handlers) by setting \l keyNavigationEnabled to \c false. + + \section1 Copy and paste + + Implementing copy and paste operations for a TableView usually also includes using + a QUndoStack (or some other undo/redo framework). The QUndoStack can be used to + store the different operations done on the model, like adding or removing rows, or + pasting data from the clipboard, with a way to undo it again later. However, an + accompanying QUndoStack that describes the possible operations, and how to undo them, + should be designed according to the needs of the model and the application. + As such, TableView doesn't offer a built-in API for handling copy and paste. + + The following snippet can be used as a reference for how to add copy and paste support + to your model and TableView. It uses the existing mime data API in QAbstractItemModel, + together with QClipboard. The snippet will work as it is, but can also be extended to + use a QUndoStack. + + \code + // Inside your C++ QAbstractTableModel subclass: + + Q_INVOKABLE void copyToClipboard(const QModelIndexList &indexes) const + { + QGuiApplication::clipboard()->setMimeData(mimeData(indexes)); + } + + Q_INVOKABLE bool pasteFromClipboard(const QModelIndex &targetIndex) + { + const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData(); + // Consider using a QUndoCommand for the following call. It should store + // the (mime) data for the model items that are about to be overwritten, so + // that a later call to undo can revert it. + return dropMimeData(mimeData, Qt::CopyAction, -1, -1, targetIndex); + } + \endcode + + The two functions can, for example, be used from QML like this: + + \code + TableView { + id: tableView + model: tableModel + selectionModel: ItemSelectionModel {} + + Shortcut { + sequence: StandardKey.Copy + onActivated: { + let indexes = tableView.selectionModel.selectedIndexes + tableView.model.copyToClipboard(indexes) + } + } + + Shortcut { + sequence: StandardKey.Paste + onActivated: { + let targetIndex = tableView.selectionModel.currentIndex + tableView.model.pasteFromClipboard(targetIndex) + } + } + } + \endcode + + \sa mimeData(), dropMimeData(), QUndoStack, QUndoCommand, QClipboard */ /*! |
