Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
19
22 Qt::DropAction action, int row, int column, const QModelIndex &parent) const
23{
24 Q_UNUSED(action);
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}
39 Qt::DropAction action, int row, int column, const QModelIndex &parent)
40{
41 if (!canDropMimeData(data, action, row, column, parent))
42 return false;
43
44 if (action == Qt::IgnoreAction)
45 return true;
47
49 int beginRow;
50
51 if (row != -1)
52 beginRow = row;
54 else if (parent.isValid())
55 beginRow = parent.row();
57 else
58 beginRow = rowCount(QModelIndex());
60
62 QByteArray encodedData = data->data("application/vnd.text.list");
64 QStringList newItems;
65 int rows = 0;
66
67 while (!stream.atEnd()) {
69 stream >> text;
70 newItems << text;
71 ++rows;
72 }
74
76 insertRows(beginRow, rows, QModelIndex());
77 for (const QString &text : std::as_const(newItems)) {
78 QModelIndex idx = index(beginRow, 0, QModelIndex());
79 setData(idx, text);
80 beginRow++;
81 }
82
83 return true;
84}
86
88Qt::ItemFlags DragDropListModel::flags(const QModelIndex &index) const
89{
90 Qt::ItemFlags defaultFlags = QStringListModel::flags(index);
91
92 if (index.isValid())
93 return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
94 else
95 return Qt::ItemIsDropEnabled | defaultFlags;
96}
98
101{
103 QByteArray encodedData;
104
106
107 for (const QModelIndex &index : indexes) {
108 if (index.isValid()) {
109 QString text = data(index, Qt::DisplayRole).toString();
110 stream << text;
111 }
112 }
113
114 mimeData->setData("application/vnd.text.list", encodedData);
115 return mimeData;
116}
118
121{
123 types << "application/vnd.text.list";
124 return types;
125}
127
130{
132}
QMimeData * mimeData(const QModelIndexList &indexes) const override
[7]
Definition model.cpp:100
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition model.cpp:38
QStringList mimeTypes() const override
[8]
Definition model.cpp:120
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
[0]
Definition model.cpp:21
Qt::DropActions supportedDropActions() const override
[9]
Definition model.cpp:129
Qt::ItemFlags flags(const QModelIndex &index) const override
[6]
Definition model.cpp:88
DragDropListModel(const QStringList &strings, QObject *parent=nullptr)
Definition model.cpp:14
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
Definition qmimedata.h:16
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Inserts count rows into the model, beginning at the given row.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the flags for the item with the given index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows in the model.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the data for the specified role in the item with the given index in the model,...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString text
@ DisplayRole
DropAction
@ CopyAction
@ IgnoreAction
@ MoveAction
@ ItemIsDragEnabled
@ ItemIsDropEnabled
EGLStreamKHR stream
GLuint index
[2]
GLsizei GLenum GLenum * types
GLsizei const GLchar ** strings
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLenum GLsizei void GLsizei void * column
GLenum GLenum GLsizei void * row
#define Q_UNUSED(x)
QMimeData * mimeData