Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
model.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4/*
5 model.cpp
6
7 A simple model that uses a QStringList as its data source.
8*/
9
10#include <QtWidgets>
11
12#include "model.h"
13
14DragDropListModel::DragDropListModel(const QStringList &strings,
15 QObject *parent)
16 : QStringListModel(strings, parent)
17{
18}
19
20//! [0]
21bool DragDropListModel::canDropMimeData(const QMimeData *data,
22 Qt::DropAction action, int row, int column, const QModelIndex &parent) const
23{
24 Q_UNUSED(action);
25 Q_UNUSED(row);
26 Q_UNUSED(parent);
27
28 if (!data->hasFormat("application/vnd.text.list"))
29 return false;
30
31 if (column > 0)
32 return false;
33
34 return true;
35}
36//! [0]
37
38//! [1]
39bool DragDropListModel::dropMimeData(const QMimeData *data,
40 Qt::DropAction action, int row, int column, const QModelIndex &parent)
41{
42 if (!canDropMimeData(data, action, row, column, parent))
43 return false;
44
45 if (action == Qt::IgnoreAction)
46 return true;
47 //! [1]
48
49 //! [2]
50 int beginRow;
51
52 if (row != -1)
53 beginRow = row;
54 //! [2]
55
56 //! [3]
57 else if (parent.isValid())
58 beginRow = parent.row();
59 //! [3]
60
61 //! [4]
62 else
63 beginRow = rowCount(QModelIndex());
64 //! [4]
65
66 //! [5]
67 QByteArray encodedData = data->data("application/vnd.text.list");
68 QDataStream stream(&encodedData, QIODevice::ReadOnly);
69 QStringList newItems;
70 int rows = 0;
71
72 while (!stream.atEnd()) {
73 QString text;
74 stream >> text;
75 newItems << text;
76 ++rows;
77 }
78 //! [5]
79
80//! [6]
81 insertRows(beginRow, rows, QModelIndex());
82 for (const QString &text : std::as_const(newItems)) {
83 QModelIndex idx = index(beginRow, 0, QModelIndex());
84 setData(idx, text);
85 beginRow++;
86 }
87
88 return true;
89}
90//! [6]
91
92//! [7]
93Qt::ItemFlags DragDropListModel::flags(const QModelIndex &index) const
94{
95 Qt::ItemFlags defaultFlags = QStringListModel::flags(index);
96
97 if (index.isValid())
98 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
99 else
100 return Qt::ItemIsDropEnabled | defaultFlags;
101}
102//! [7]
103
104//! [8]
105QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const
106{
107 QMimeData *mimeData = new QMimeData;
108 QByteArray encodedData;
109
110 QDataStream stream(&encodedData, QIODevice::WriteOnly);
111
112 for (const QModelIndex &index : indexes) {
113 if (index.isValid()) {
114 QString text = data(index, Qt::DisplayRole).toString();
115 stream << text;
116 }
117 }
118
119 mimeData->setData("application/vnd.text.list", encodedData);
120 return mimeData;
121}
122//! [8]
123
124//! [9]
125QStringList DragDropListModel::mimeTypes() const
126{
127 QStringList types;
128 types << "application/vnd.text.list";
129 return types;
130}
131//! [9]
132
133//! [10]
134Qt::DropActions DragDropListModel::supportedDropActions() const
135{
136 return Qt::CopyAction | Qt::MoveAction;
137}
138//! [10]