Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qtablewidget.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
24 | ** | - | ||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
35 | ** | - | ||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||
39 | - | |||||||||||||
40 | #include "qtablewidget.h" | - | ||||||||||||
41 | - | |||||||||||||
42 | #ifndef QT_NO_TABLEWIDGET | - | ||||||||||||
43 | #include <qitemdelegate.h> | - | ||||||||||||
44 | #include <qpainter.h> | - | ||||||||||||
45 | #include <private/qtablewidget_p.h> | - | ||||||||||||
46 | - | |||||||||||||
47 | #include <algorithm> | - | ||||||||||||
48 | - | |||||||||||||
49 | QT_BEGIN_NAMESPACE | - | ||||||||||||
50 | - | |||||||||||||
51 | QTableModel::QTableModel(int rows, int columns, QTableWidget *parent) | - | ||||||||||||
52 | : QAbstractTableModel(parent), | - | ||||||||||||
53 | prototype(0), | - | ||||||||||||
54 | tableItems(rows * columns, 0), | - | ||||||||||||
55 | verticalHeaderItems(rows, 0), | - | ||||||||||||
56 | horizontalHeaderItems(columns, 0) | - | ||||||||||||
57 | {} | - | ||||||||||||
58 | - | |||||||||||||
59 | QTableModel::~QTableModel() | - | ||||||||||||
60 | { | - | ||||||||||||
61 | clear(); | - | ||||||||||||
62 | delete prototype; | - | ||||||||||||
63 | } | - | ||||||||||||
64 | - | |||||||||||||
65 | bool QTableModel::insertRows(int row, int count, const QModelIndex &) | - | ||||||||||||
66 | { | - | ||||||||||||
67 | if (count < 1 || row < 0 || row > verticalHeaderItems.count()) | - | ||||||||||||
68 | return false; | - | ||||||||||||
69 | - | |||||||||||||
70 | beginInsertRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||
71 | int rc = verticalHeaderItems.count(); | - | ||||||||||||
72 | int cc = horizontalHeaderItems.count(); | - | ||||||||||||
73 | verticalHeaderItems.insert(row, count, 0); | - | ||||||||||||
74 | if (rc == 0) | - | ||||||||||||
75 | tableItems.resize(cc * count); | - | ||||||||||||
76 | else | - | ||||||||||||
77 | tableItems.insert(tableIndex(row, 0), cc * count, 0); | - | ||||||||||||
78 | endInsertRows(); | - | ||||||||||||
79 | return true; | - | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | bool QTableModel::insertColumns(int column, int count, const QModelIndex &) | - | ||||||||||||
83 | { | - | ||||||||||||
84 | if (count < 1 || column < 0 || column > horizontalHeaderItems.count()) | - | ||||||||||||
85 | return false; | - | ||||||||||||
86 | - | |||||||||||||
87 | beginInsertColumns(QModelIndex(), column, column + count - 1); | - | ||||||||||||
88 | int rc = verticalHeaderItems.count(); | - | ||||||||||||
89 | int cc = horizontalHeaderItems.count(); | - | ||||||||||||
90 | horizontalHeaderItems.insert(column, count, 0); | - | ||||||||||||
91 | if (cc == 0) | - | ||||||||||||
92 | tableItems.resize(rc * count); | - | ||||||||||||
93 | else | - | ||||||||||||
94 | for (int row = 0; row < rc; ++row) | - | ||||||||||||
95 | tableItems.insert(tableIndex(row, column), count, 0); | - | ||||||||||||
96 | endInsertColumns(); | - | ||||||||||||
97 | return true; | - | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | bool QTableModel::removeRows(int row, int count, const QModelIndex &) | - | ||||||||||||
101 | { | - | ||||||||||||
102 | if (count < 1 || row < 0 || row + count > verticalHeaderItems.count()) | - | ||||||||||||
103 | return false; | - | ||||||||||||
104 | - | |||||||||||||
105 | beginRemoveRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||
106 | int i = tableIndex(row, 0); | - | ||||||||||||
107 | int n = count * columnCount(); | - | ||||||||||||
108 | QTableWidgetItem *oldItem = 0; | - | ||||||||||||
109 | for (int j = i; j < n + i; ++j) { | - | ||||||||||||
110 | oldItem = tableItems.at(j); | - | ||||||||||||
111 | if (oldItem) | - | ||||||||||||
112 | oldItem->view = 0; | - | ||||||||||||
113 | delete oldItem; | - | ||||||||||||
114 | } | - | ||||||||||||
115 | tableItems.remove(qMax(i, 0), n); | - | ||||||||||||
116 | for (int v = row; v < row + count; ++v) { | - | ||||||||||||
117 | oldItem = verticalHeaderItems.at(v); | - | ||||||||||||
118 | if (oldItem) | - | ||||||||||||
119 | oldItem->view = 0; | - | ||||||||||||
120 | delete oldItem; | - | ||||||||||||
121 | } | - | ||||||||||||
122 | verticalHeaderItems.remove(row, count); | - | ||||||||||||
123 | endRemoveRows(); | - | ||||||||||||
124 | return true; | - | ||||||||||||
125 | } | - | ||||||||||||
126 | - | |||||||||||||
127 | bool QTableModel::removeColumns(int column, int count, const QModelIndex &) | - | ||||||||||||
128 | { | - | ||||||||||||
129 | if (count < 1 || column < 0 || column + count > horizontalHeaderItems.count()) | - | ||||||||||||
130 | return false; | - | ||||||||||||
131 | - | |||||||||||||
132 | beginRemoveColumns(QModelIndex(), column, column + count - 1); | - | ||||||||||||
133 | QTableWidgetItem *oldItem = 0; | - | ||||||||||||
134 | for (int row = rowCount() - 1; row >= 0; --row) { | - | ||||||||||||
135 | int i = tableIndex(row, column); | - | ||||||||||||
136 | for (int j = i; j < i + count; ++j) { | - | ||||||||||||
137 | oldItem = tableItems.at(j); | - | ||||||||||||
138 | if (oldItem) | - | ||||||||||||
139 | oldItem->view = 0; | - | ||||||||||||
140 | delete oldItem; | - | ||||||||||||
141 | } | - | ||||||||||||
142 | tableItems.remove(i, count); | - | ||||||||||||
143 | } | - | ||||||||||||
144 | for (int h=column; h<column+count; ++h) { | - | ||||||||||||
145 | oldItem = horizontalHeaderItems.at(h); | - | ||||||||||||
146 | if (oldItem) | - | ||||||||||||
147 | oldItem->view = 0; | - | ||||||||||||
148 | delete oldItem; | - | ||||||||||||
149 | } | - | ||||||||||||
150 | horizontalHeaderItems.remove(column, count); | - | ||||||||||||
151 | endRemoveColumns(); | - | ||||||||||||
152 | return true; | - | ||||||||||||
153 | } | - | ||||||||||||
154 | - | |||||||||||||
155 | void QTableModel::setItem(int row, int column, QTableWidgetItem *item) | - | ||||||||||||
156 | { | - | ||||||||||||
157 | int i = tableIndex(row, column); | - | ||||||||||||
158 | if (i < 0 || i >= tableItems.count()) | - | ||||||||||||
159 | return; | - | ||||||||||||
160 | QTableWidgetItem *oldItem = tableItems.at(i); | - | ||||||||||||
161 | if (item == oldItem) | - | ||||||||||||
162 | return; | - | ||||||||||||
163 | - | |||||||||||||
164 | // remove old | - | ||||||||||||
165 | if (oldItem) | - | ||||||||||||
166 | oldItem->view = 0; | - | ||||||||||||
167 | delete tableItems.at(i); | - | ||||||||||||
168 | - | |||||||||||||
169 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
170 | - | |||||||||||||
171 | // set new | - | ||||||||||||
172 | if (item) | - | ||||||||||||
173 | item->d->id = i; | - | ||||||||||||
174 | tableItems[i] = item; | - | ||||||||||||
175 | - | |||||||||||||
176 | if (view && view->isSortingEnabled() | - | ||||||||||||
177 | && view->horizontalHeader()->sortIndicatorSection() == column) { | - | ||||||||||||
178 | // sorted insertion | - | ||||||||||||
179 | Qt::SortOrder order = view->horizontalHeader()->sortIndicatorOrder(); | - | ||||||||||||
180 | QVector<QTableWidgetItem*> colItems = columnItems(column); | - | ||||||||||||
181 | if (row < colItems.count()) | - | ||||||||||||
182 | colItems.remove(row); | - | ||||||||||||
183 | int sortedRow; | - | ||||||||||||
184 | if (item == 0) { | - | ||||||||||||
185 | // move to after all non-0 (sortable) items | - | ||||||||||||
186 | sortedRow = colItems.count(); | - | ||||||||||||
187 | } else { | - | ||||||||||||
188 | QVector<QTableWidgetItem*>::iterator it; | - | ||||||||||||
189 | it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item); | - | ||||||||||||
190 | sortedRow = qMax((int)(it - colItems.begin()), 0); | - | ||||||||||||
191 | } | - | ||||||||||||
192 | if (sortedRow != row) { | - | ||||||||||||
193 | emit layoutAboutToBeChanged(); | - | ||||||||||||
194 | // move the items @ row to sortedRow | - | ||||||||||||
195 | int cc = columnCount(); | - | ||||||||||||
196 | QVector<QTableWidgetItem*> rowItems(cc); | - | ||||||||||||
197 | for (int j = 0; j < cc; ++j) | - | ||||||||||||
198 | rowItems[j] = tableItems.at(tableIndex(row, j)); | - | ||||||||||||
199 | tableItems.remove(tableIndex(row, 0), cc); | - | ||||||||||||
200 | tableItems.insert(tableIndex(sortedRow, 0), cc, 0); | - | ||||||||||||
201 | for (int j = 0; j < cc; ++j) | - | ||||||||||||
202 | tableItems[tableIndex(sortedRow, j)] = rowItems.at(j); | - | ||||||||||||
203 | QTableWidgetItem *header = verticalHeaderItems.at(row); | - | ||||||||||||
204 | verticalHeaderItems.remove(row); | - | ||||||||||||
205 | verticalHeaderItems.insert(sortedRow, header); | - | ||||||||||||
206 | // update persistent indexes | - | ||||||||||||
207 | QModelIndexList oldPersistentIndexes = persistentIndexList(); | - | ||||||||||||
208 | QModelIndexList newPersistentIndexes = oldPersistentIndexes; | - | ||||||||||||
209 | updateRowIndexes(newPersistentIndexes, row, sortedRow); | - | ||||||||||||
210 | changePersistentIndexList(oldPersistentIndexes, | - | ||||||||||||
211 | newPersistentIndexes); | - | ||||||||||||
212 | - | |||||||||||||
213 | emit layoutChanged(); | - | ||||||||||||
214 | return; | - | ||||||||||||
215 | } | - | ||||||||||||
216 | } | - | ||||||||||||
217 | QModelIndex idx = QAbstractTableModel::index(row, column); | - | ||||||||||||
218 | emit dataChanged(idx, idx); | - | ||||||||||||
219 | } | - | ||||||||||||
220 | - | |||||||||||||
221 | QTableWidgetItem *QTableModel::takeItem(int row, int column) | - | ||||||||||||
222 | { | - | ||||||||||||
223 | long i = tableIndex(row, column); | - | ||||||||||||
224 | QTableWidgetItem *itm = tableItems.value(i); | - | ||||||||||||
225 | if (itm) { | - | ||||||||||||
226 | itm->view = 0; | - | ||||||||||||
227 | itm->d->id = -1; | - | ||||||||||||
228 | tableItems[i] = 0; | - | ||||||||||||
229 | QModelIndex ind = index(itm); | - | ||||||||||||
230 | emit dataChanged(ind, ind); | - | ||||||||||||
231 | } | - | ||||||||||||
232 | return itm; | - | ||||||||||||
233 | } | - | ||||||||||||
234 | - | |||||||||||||
235 | QTableWidgetItem *QTableModel::item(int row, int column) const | - | ||||||||||||
236 | { | - | ||||||||||||
237 | return item(index(row, column)); | - | ||||||||||||
238 | } | - | ||||||||||||
239 | - | |||||||||||||
240 | QTableWidgetItem *QTableModel::item(const QModelIndex &index) const | - | ||||||||||||
241 | { | - | ||||||||||||
242 | if (!isValid(index)) | - | ||||||||||||
243 | return 0; | - | ||||||||||||
244 | return tableItems.at(tableIndex(index.row(), index.column())); | - | ||||||||||||
245 | } | - | ||||||||||||
246 | - | |||||||||||||
247 | void QTableModel::removeItem(QTableWidgetItem *item) | - | ||||||||||||
248 | { | - | ||||||||||||
249 | int i = tableItems.indexOf(item); | - | ||||||||||||
250 | if (i != -1) { | - | ||||||||||||
251 | tableItems[i] = 0; | - | ||||||||||||
252 | QModelIndex idx = index(item); | - | ||||||||||||
253 | emit dataChanged(idx, idx); | - | ||||||||||||
254 | return; | - | ||||||||||||
255 | } | - | ||||||||||||
256 | - | |||||||||||||
257 | i = verticalHeaderItems.indexOf(item); | - | ||||||||||||
258 | - | |||||||||||||
259 | if (i != -1) { | - | ||||||||||||
260 | verticalHeaderItems[i] = 0; | - | ||||||||||||
261 | emit headerDataChanged(Qt::Vertical, i, i); | - | ||||||||||||
262 | return; | - | ||||||||||||
263 | } | - | ||||||||||||
264 | i = horizontalHeaderItems.indexOf(item); | - | ||||||||||||
265 | if (i != -1) { | - | ||||||||||||
266 | horizontalHeaderItems[i] = 0; | - | ||||||||||||
267 | emit headerDataChanged(Qt::Horizontal, i, i); | - | ||||||||||||
268 | return; | - | ||||||||||||
269 | } | - | ||||||||||||
270 | } | - | ||||||||||||
271 | - | |||||||||||||
272 | void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item) | - | ||||||||||||
273 | { | - | ||||||||||||
274 | if (section < 0 || section >= horizontalHeaderItems.count()) | - | ||||||||||||
275 | return; | - | ||||||||||||
276 | QTableWidgetItem *oldItem = horizontalHeaderItems.at(section); | - | ||||||||||||
277 | if (item == oldItem) | - | ||||||||||||
278 | return; | - | ||||||||||||
279 | - | |||||||||||||
280 | if (oldItem) | - | ||||||||||||
281 | oldItem->view = 0; | - | ||||||||||||
282 | delete oldItem; | - | ||||||||||||
283 | - | |||||||||||||
284 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
285 | - | |||||||||||||
286 | if (item) { | - | ||||||||||||
287 | item->view = view; | - | ||||||||||||
288 | item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem); | - | ||||||||||||
289 | } | - | ||||||||||||
290 | horizontalHeaderItems[section] = item; | - | ||||||||||||
291 | emit headerDataChanged(Qt::Horizontal, section, section); | - | ||||||||||||
292 | } | - | ||||||||||||
293 | - | |||||||||||||
294 | void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item) | - | ||||||||||||
295 | { | - | ||||||||||||
296 | if (section < 0 || section >= verticalHeaderItems.count()) | - | ||||||||||||
297 | return; | - | ||||||||||||
298 | QTableWidgetItem *oldItem = verticalHeaderItems.at(section); | - | ||||||||||||
299 | if (item == oldItem) | - | ||||||||||||
300 | return; | - | ||||||||||||
301 | - | |||||||||||||
302 | if (oldItem) | - | ||||||||||||
303 | oldItem->view = 0; | - | ||||||||||||
304 | delete oldItem; | - | ||||||||||||
305 | - | |||||||||||||
306 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
307 | - | |||||||||||||
308 | if (item) { | - | ||||||||||||
309 | item->view = view; | - | ||||||||||||
310 | item->itemFlags = Qt::ItemFlags(int(item->itemFlags)|ItemIsHeaderItem); | - | ||||||||||||
311 | } | - | ||||||||||||
312 | verticalHeaderItems[section] = item; | - | ||||||||||||
313 | emit headerDataChanged(Qt::Vertical, section, section); | - | ||||||||||||
314 | } | - | ||||||||||||
315 | - | |||||||||||||
316 | QTableWidgetItem *QTableModel::takeHorizontalHeaderItem(int section) | - | ||||||||||||
317 | { | - | ||||||||||||
318 | if (section < 0 || section >= horizontalHeaderItems.count()) | - | ||||||||||||
319 | return 0; | - | ||||||||||||
320 | QTableWidgetItem *itm = horizontalHeaderItems.at(section); | - | ||||||||||||
321 | if (itm) { | - | ||||||||||||
322 | itm->view = 0; | - | ||||||||||||
323 | itm->itemFlags &= ~ItemIsHeaderItem; | - | ||||||||||||
324 | horizontalHeaderItems[section] = 0; | - | ||||||||||||
325 | } | - | ||||||||||||
326 | return itm; | - | ||||||||||||
327 | } | - | ||||||||||||
328 | - | |||||||||||||
329 | QTableWidgetItem *QTableModel::takeVerticalHeaderItem(int section) | - | ||||||||||||
330 | { | - | ||||||||||||
331 | if (section < 0 || section >= verticalHeaderItems.count()) | - | ||||||||||||
332 | return 0; | - | ||||||||||||
333 | QTableWidgetItem *itm = verticalHeaderItems.at(section); | - | ||||||||||||
334 | if (itm) { | - | ||||||||||||
335 | itm->view = 0; | - | ||||||||||||
336 | itm->itemFlags &= ~ItemIsHeaderItem; | - | ||||||||||||
337 | verticalHeaderItems[section] = 0; | - | ||||||||||||
338 | } | - | ||||||||||||
339 | return itm; | - | ||||||||||||
340 | } | - | ||||||||||||
341 | - | |||||||||||||
342 | QTableWidgetItem *QTableModel::horizontalHeaderItem(int section) | - | ||||||||||||
343 | { | - | ||||||||||||
344 | return horizontalHeaderItems.value(section); | - | ||||||||||||
345 | } | - | ||||||||||||
346 | - | |||||||||||||
347 | QTableWidgetItem *QTableModel::verticalHeaderItem(int section) | - | ||||||||||||
348 | { | - | ||||||||||||
349 | return verticalHeaderItems.value(section); | - | ||||||||||||
350 | } | - | ||||||||||||
351 | - | |||||||||||||
352 | QModelIndex QTableModel::index(const QTableWidgetItem *item) const | - | ||||||||||||
353 | { | - | ||||||||||||
354 | if (!item) | - | ||||||||||||
355 | return QModelIndex(); | - | ||||||||||||
356 | int i = -1; | - | ||||||||||||
357 | const int id = item->d->id; | - | ||||||||||||
358 | if (id >= 0 && id < tableItems.count() && tableItems.at(id) == item) { | - | ||||||||||||
359 | i = id; | - | ||||||||||||
360 | } else { // we need to search for the item | - | ||||||||||||
361 | i = tableItems.indexOf(const_cast<QTableWidgetItem*>(item)); | - | ||||||||||||
362 | if (i == -1) // not found | - | ||||||||||||
363 | return QModelIndex(); | - | ||||||||||||
364 | } | - | ||||||||||||
365 | int row = i / columnCount(); | - | ||||||||||||
366 | int col = i % columnCount(); | - | ||||||||||||
367 | return QAbstractTableModel::index(row, col); | - | ||||||||||||
368 | } | - | ||||||||||||
369 | - | |||||||||||||
370 | void QTableModel::setRowCount(int rows) | - | ||||||||||||
371 | { | - | ||||||||||||
372 | int rc = verticalHeaderItems.count(); | - | ||||||||||||
373 | if (rows < 0 || rc == rows) | - | ||||||||||||
374 | return; | - | ||||||||||||
375 | if (rc < rows) | - | ||||||||||||
376 | insertRows(qMax(rc, 0), rows - rc); | - | ||||||||||||
377 | else | - | ||||||||||||
378 | removeRows(qMax(rows, 0), rc - rows); | - | ||||||||||||
379 | } | - | ||||||||||||
380 | - | |||||||||||||
381 | void QTableModel::setColumnCount(int columns) | - | ||||||||||||
382 | { | - | ||||||||||||
383 | int cc = horizontalHeaderItems.count(); | - | ||||||||||||
384 | if (columns < 0 || cc == columns) | - | ||||||||||||
385 | return; | - | ||||||||||||
386 | if (cc < columns) | - | ||||||||||||
387 | insertColumns(qMax(cc, 0), columns - cc); | - | ||||||||||||
388 | else | - | ||||||||||||
389 | removeColumns(qMax(columns, 0), cc - columns); | - | ||||||||||||
390 | } | - | ||||||||||||
391 | - | |||||||||||||
392 | int QTableModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||
393 | { | - | ||||||||||||
394 | return parent.isValid() ? 0 : verticalHeaderItems.count(); | - | ||||||||||||
395 | } | - | ||||||||||||
396 | - | |||||||||||||
397 | int QTableModel::columnCount(const QModelIndex &parent) const | - | ||||||||||||
398 | { | - | ||||||||||||
399 | return parent.isValid() ? 0 : horizontalHeaderItems.count(); | - | ||||||||||||
400 | } | - | ||||||||||||
401 | - | |||||||||||||
402 | QVariant QTableModel::data(const QModelIndex &index, int role) const | - | ||||||||||||
403 | { | - | ||||||||||||
404 | QTableWidgetItem *itm = item(index); | - | ||||||||||||
405 | if (itm) | - | ||||||||||||
406 | return itm->data(role); | - | ||||||||||||
407 | return QVariant(); | - | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | bool QTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||
411 | { | - | ||||||||||||
412 | if (!index.isValid()) | - | ||||||||||||
413 | return false; | - | ||||||||||||
414 | - | |||||||||||||
415 | QTableWidgetItem *itm = item(index); | - | ||||||||||||
416 | if (itm) { | - | ||||||||||||
417 | itm->setData(role, value); | - | ||||||||||||
418 | return true; | - | ||||||||||||
419 | } | - | ||||||||||||
420 | - | |||||||||||||
421 | // don't create dummy table items for empty values | - | ||||||||||||
422 | if (!value.isValid()) | - | ||||||||||||
423 | return false; | - | ||||||||||||
424 | - | |||||||||||||
425 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
426 | if (!view) | - | ||||||||||||
427 | return false; | - | ||||||||||||
428 | - | |||||||||||||
429 | itm = createItem(); | - | ||||||||||||
430 | itm->setData(role, value); | - | ||||||||||||
431 | view->setItem(index.row(), index.column(), itm); | - | ||||||||||||
432 | return true; | - | ||||||||||||
433 | } | - | ||||||||||||
434 | - | |||||||||||||
435 | QMap<int, QVariant> QTableModel::itemData(const QModelIndex &index) const | - | ||||||||||||
436 | { | - | ||||||||||||
437 | QMap<int, QVariant> roles; | - | ||||||||||||
438 | QTableWidgetItem *itm = item(index); | - | ||||||||||||
439 | if (itm) { | - | ||||||||||||
440 | for (int i = 0; i < itm->values.count(); ++i) { | - | ||||||||||||
441 | roles.insert(itm->values.at(i).role, | - | ||||||||||||
442 | itm->values.at(i).value); | - | ||||||||||||
443 | } | - | ||||||||||||
444 | } | - | ||||||||||||
445 | return roles; | - | ||||||||||||
446 | } | - | ||||||||||||
447 | - | |||||||||||||
448 | // reimplemented to ensure that only one dataChanged() signal is emitted | - | ||||||||||||
449 | bool QTableModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) | - | ||||||||||||
450 | { | - | ||||||||||||
451 | if (!index.isValid()) | - | ||||||||||||
452 | return false; | - | ||||||||||||
453 | - | |||||||||||||
454 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
455 | QTableWidgetItem *itm = item(index); | - | ||||||||||||
456 | if (itm) { | - | ||||||||||||
457 | itm->view = 0; // prohibits item from calling itemChanged() | - | ||||||||||||
458 | bool changed = false; | - | ||||||||||||
459 | for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) { | - | ||||||||||||
460 | if (itm->data(it.key()) != it.value()) { | - | ||||||||||||
461 | itm->setData(it.key(), it.value()); | - | ||||||||||||
462 | changed = true; | - | ||||||||||||
463 | } | - | ||||||||||||
464 | } | - | ||||||||||||
465 | itm->view = view; | - | ||||||||||||
466 | if (changed) | - | ||||||||||||
467 | itemChanged(itm); | - | ||||||||||||
468 | return true; | - | ||||||||||||
469 | } | - | ||||||||||||
470 | - | |||||||||||||
471 | if (!view) | - | ||||||||||||
472 | return false; | - | ||||||||||||
473 | - | |||||||||||||
474 | itm = createItem(); | - | ||||||||||||
475 | for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) | - | ||||||||||||
476 | itm->setData(it.key(), it.value()); | - | ||||||||||||
477 | view->setItem(index.row(), index.column(), itm); | - | ||||||||||||
478 | return true; | - | ||||||||||||
479 | } | - | ||||||||||||
480 | - | |||||||||||||
481 | Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const | - | ||||||||||||
482 | { | - | ||||||||||||
483 | if (!index.isValid()) | - | ||||||||||||
484 | return Qt::ItemIsDropEnabled; | - | ||||||||||||
485 | if (QTableWidgetItem *itm = item(index)) | - | ||||||||||||
486 | return itm->flags(); | - | ||||||||||||
487 | return (Qt::ItemIsEditable | - | ||||||||||||
488 | |Qt::ItemIsSelectable | - | ||||||||||||
489 | |Qt::ItemIsUserCheckable | - | ||||||||||||
490 | |Qt::ItemIsEnabled | - | ||||||||||||
491 | |Qt::ItemIsDragEnabled | - | ||||||||||||
492 | |Qt::ItemIsDropEnabled); | - | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | void QTableModel::sort(int column, Qt::SortOrder order) | - | ||||||||||||
496 | { | - | ||||||||||||
497 | QVector<QPair<QTableWidgetItem*, int> > sortable; | - | ||||||||||||
498 | QVector<int> unsortable; | - | ||||||||||||
499 | - | |||||||||||||
500 | sortable.reserve(rowCount()); | - | ||||||||||||
501 | unsortable.reserve(rowCount()); | - | ||||||||||||
502 | - | |||||||||||||
503 | for (int row = 0; row < rowCount(); ++row) { | - | ||||||||||||
504 | if (QTableWidgetItem *itm = item(row, column)) | - | ||||||||||||
505 | sortable.append(QPair<QTableWidgetItem*,int>(itm, row)); | - | ||||||||||||
506 | else | - | ||||||||||||
507 | unsortable.append(row); | - | ||||||||||||
508 | } | - | ||||||||||||
509 | - | |||||||||||||
510 | LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); | - | ||||||||||||
511 | std::stable_sort(sortable.begin(), sortable.end(), compare); | - | ||||||||||||
512 | - | |||||||||||||
513 | QVector<QTableWidgetItem*> sorted_table(tableItems.count()); | - | ||||||||||||
514 | QModelIndexList from; | - | ||||||||||||
515 | QModelIndexList to; | - | ||||||||||||
516 | const int numRows = rowCount(); | - | ||||||||||||
517 | const int numColumns = columnCount(); | - | ||||||||||||
518 | from.reserve(numRows * numColumns); | - | ||||||||||||
519 | to.reserve(numRows * numColumns); | - | ||||||||||||
520 | for (int i = 0; i < numRows; ++i) { | - | ||||||||||||
521 | int r = (i < sortable.count() | - | ||||||||||||
522 | ? sortable.at(i).second | - | ||||||||||||
523 | : unsortable.at(i - sortable.count())); | - | ||||||||||||
524 | for (int c = 0; c < numColumns; ++c) { | - | ||||||||||||
525 | sorted_table[tableIndex(i, c)] = item(r, c); | - | ||||||||||||
526 | from.append(createIndex(r, c)); | - | ||||||||||||
527 | to.append(createIndex(i, c)); | - | ||||||||||||
528 | } | - | ||||||||||||
529 | } | - | ||||||||||||
530 | - | |||||||||||||
531 | emit layoutAboutToBeChanged(); | - | ||||||||||||
532 | - | |||||||||||||
533 | tableItems = sorted_table; | - | ||||||||||||
534 | changePersistentIndexList(from, to); // ### slow | - | ||||||||||||
535 | - | |||||||||||||
536 | emit layoutChanged(); | - | ||||||||||||
537 | } | - | ||||||||||||
538 | - | |||||||||||||
539 | /* | - | ||||||||||||
540 | \internal | - | ||||||||||||
541 | - | |||||||||||||
542 | Ensures that rows in the interval [start, end] are | - | ||||||||||||
543 | sorted according to the contents of column \a column | - | ||||||||||||
544 | and the given sort \a order. | - | ||||||||||||
545 | */ | - | ||||||||||||
546 | void QTableModel::ensureSorted(int column, Qt::SortOrder order, | - | ||||||||||||
547 | int start, int end) | - | ||||||||||||
548 | { | - | ||||||||||||
549 | int count = end - start + 1; | - | ||||||||||||
550 | QVector < QPair<QTableWidgetItem*,int> > sorting; | - | ||||||||||||
551 | sorting.reserve(count); | - | ||||||||||||
552 | for (int row = start; row <= end; ++row) { | - | ||||||||||||
553 | QTableWidgetItem *itm = item(row, column); | - | ||||||||||||
554 | if (itm == 0) { | - | ||||||||||||
555 | // no more sortable items (all 0-items are | - | ||||||||||||
556 | // at the end of the table when it is sorted) | - | ||||||||||||
557 | break; | - | ||||||||||||
558 | } | - | ||||||||||||
559 | sorting.append(QPair<QTableWidgetItem*,int>(itm, row)); | - | ||||||||||||
560 | } | - | ||||||||||||
561 | - | |||||||||||||
562 | LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); | - | ||||||||||||
563 | std::stable_sort(sorting.begin(), sorting.end(), compare); | - | ||||||||||||
564 | QModelIndexList oldPersistentIndexes, newPersistentIndexes; | - | ||||||||||||
565 | QVector<QTableWidgetItem*> newTable = tableItems; | - | ||||||||||||
566 | QVector<QTableWidgetItem*> newVertical = verticalHeaderItems; | - | ||||||||||||
567 | QVector<QTableWidgetItem*> colItems = columnItems(column); | - | ||||||||||||
568 | QVector<QTableWidgetItem*>::iterator vit = colItems.begin(); | - | ||||||||||||
569 | bool changed = false; | - | ||||||||||||
570 | for (int i = 0; i < sorting.count(); ++i) { | - | ||||||||||||
571 | int oldRow = sorting.at(i).second; | - | ||||||||||||
572 | QTableWidgetItem *item = colItems.at(oldRow); | - | ||||||||||||
573 | colItems.remove(oldRow); | - | ||||||||||||
574 | vit = sortedInsertionIterator(vit, colItems.end(), order, item); | - | ||||||||||||
575 | int newRow = qMax((int)(vit - colItems.begin()), 0); | - | ||||||||||||
576 | if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item)) | - | ||||||||||||
577 | newRow = oldRow; | - | ||||||||||||
578 | vit = colItems.insert(vit, item); | - | ||||||||||||
579 | if (newRow != oldRow) { | - | ||||||||||||
580 | if (!changed) { | - | ||||||||||||
581 | emit layoutAboutToBeChanged(); | - | ||||||||||||
582 | oldPersistentIndexes = persistentIndexList(); | - | ||||||||||||
583 | newPersistentIndexes = oldPersistentIndexes; | - | ||||||||||||
584 | changed = true; | - | ||||||||||||
585 | } | - | ||||||||||||
586 | // move the items @ oldRow to newRow | - | ||||||||||||
587 | int cc = columnCount(); | - | ||||||||||||
588 | QVector<QTableWidgetItem*> rowItems(cc); | - | ||||||||||||
589 | for (int j = 0; j < cc; ++j) | - | ||||||||||||
590 | rowItems[j] = newTable.at(tableIndex(oldRow, j)); | - | ||||||||||||
591 | newTable.remove(tableIndex(oldRow, 0), cc); | - | ||||||||||||
592 | newTable.insert(tableIndex(newRow, 0), cc, 0); | - | ||||||||||||
593 | for (int j = 0; j < cc; ++j) | - | ||||||||||||
594 | newTable[tableIndex(newRow, j)] = rowItems.at(j); | - | ||||||||||||
595 | QTableWidgetItem *header = newVertical.at(oldRow); | - | ||||||||||||
596 | newVertical.remove(oldRow); | - | ||||||||||||
597 | newVertical.insert(newRow, header); | - | ||||||||||||
598 | // update persistent indexes | - | ||||||||||||
599 | updateRowIndexes(newPersistentIndexes, oldRow, newRow); | - | ||||||||||||
600 | // the index of the remaining rows may have changed | - | ||||||||||||
601 | for (int j = i + 1; j < sorting.count(); ++j) { | - | ||||||||||||
602 | int otherRow = sorting.at(j).second; | - | ||||||||||||
603 | if (oldRow < otherRow && newRow >= otherRow) | - | ||||||||||||
604 | --sorting[j].second; | - | ||||||||||||
605 | else if (oldRow > otherRow && newRow <= otherRow) | - | ||||||||||||
606 | ++sorting[j].second; | - | ||||||||||||
607 | } | - | ||||||||||||
608 | } | - | ||||||||||||
609 | } | - | ||||||||||||
610 | - | |||||||||||||
611 | if (changed) { | - | ||||||||||||
612 | tableItems = newTable; | - | ||||||||||||
613 | verticalHeaderItems = newVertical; | - | ||||||||||||
614 | changePersistentIndexList(oldPersistentIndexes, | - | ||||||||||||
615 | newPersistentIndexes); | - | ||||||||||||
616 | emit layoutChanged(); | - | ||||||||||||
617 | } | - | ||||||||||||
618 | } | - | ||||||||||||
619 | - | |||||||||||||
620 | /* | - | ||||||||||||
621 | \internal | - | ||||||||||||
622 | - | |||||||||||||
623 | Returns the non-0 items in column \a column. | - | ||||||||||||
624 | */ | - | ||||||||||||
625 | QVector<QTableWidgetItem*> QTableModel::columnItems(int column) const | - | ||||||||||||
626 | { | - | ||||||||||||
627 | QVector<QTableWidgetItem*> items; | - | ||||||||||||
628 | int rc = rowCount(); | - | ||||||||||||
629 | items.reserve(rc); | - | ||||||||||||
630 | for (int row = 0; row < rc; ++row) { | - | ||||||||||||
631 | QTableWidgetItem *itm = item(row, column); | - | ||||||||||||
632 | if (itm == 0) { | - | ||||||||||||
633 | // no more sortable items (all 0-items are | - | ||||||||||||
634 | // at the end of the table when it is sorted) | - | ||||||||||||
635 | break; | - | ||||||||||||
636 | } | - | ||||||||||||
637 | items.append(itm); | - | ||||||||||||
638 | } | - | ||||||||||||
639 | return items; | - | ||||||||||||
640 | } | - | ||||||||||||
641 | - | |||||||||||||
642 | /* | - | ||||||||||||
643 | \internal | - | ||||||||||||
644 | - | |||||||||||||
645 | Adjusts the row of each index in \a indexes if necessary, given | - | ||||||||||||
646 | that a row of items has been moved from row \a movedFrom to row | - | ||||||||||||
647 | \a movedTo. | - | ||||||||||||
648 | */ | - | ||||||||||||
649 | void QTableModel::updateRowIndexes(QModelIndexList &indexes, | - | ||||||||||||
650 | int movedFromRow, int movedToRow) | - | ||||||||||||
651 | { | - | ||||||||||||
652 | QModelIndexList::iterator it; | - | ||||||||||||
653 | for (it = indexes.begin(); it != indexes.end(); ++it) { | - | ||||||||||||
654 | int oldRow = (*it).row(); | - | ||||||||||||
655 | int newRow = oldRow; | - | ||||||||||||
656 | if (oldRow == movedFromRow) | - | ||||||||||||
657 | newRow = movedToRow; | - | ||||||||||||
658 | else if (movedFromRow < oldRow && movedToRow >= oldRow) | - | ||||||||||||
659 | newRow = oldRow - 1; | - | ||||||||||||
660 | else if (movedFromRow > oldRow && movedToRow <= oldRow) | - | ||||||||||||
661 | newRow = oldRow + 1; | - | ||||||||||||
662 | if (newRow != oldRow) | - | ||||||||||||
663 | *it = index(newRow, (*it).column(), (*it).parent()); | - | ||||||||||||
664 | } | - | ||||||||||||
665 | } | - | ||||||||||||
666 | - | |||||||||||||
667 | /* | - | ||||||||||||
668 | \internal | - | ||||||||||||
669 | - | |||||||||||||
670 | Returns an iterator to the item where \a item should be | - | ||||||||||||
671 | inserted in the interval (\a begin, \a end) according to | - | ||||||||||||
672 | the given sort \a order. | - | ||||||||||||
673 | */ | - | ||||||||||||
674 | QVector<QTableWidgetItem*>::iterator QTableModel::sortedInsertionIterator( | - | ||||||||||||
675 | const QVector<QTableWidgetItem*>::iterator &begin, | - | ||||||||||||
676 | const QVector<QTableWidgetItem*>::iterator &end, | - | ||||||||||||
677 | Qt::SortOrder order, QTableWidgetItem *item) | - | ||||||||||||
678 | { | - | ||||||||||||
679 | if (order == Qt::AscendingOrder) | - | ||||||||||||
680 | return std::lower_bound(begin, end, item, QTableModelLessThan()); | - | ||||||||||||
681 | return std::lower_bound(begin, end, item, QTableModelGreaterThan()); | - | ||||||||||||
682 | } | - | ||||||||||||
683 | - | |||||||||||||
684 | bool QTableModel::itemLessThan(const QPair<QTableWidgetItem*,int> &left, | - | ||||||||||||
685 | const QPair<QTableWidgetItem*,int> &right) | - | ||||||||||||
686 | { | - | ||||||||||||
687 | return *(left.first) < *(right.first); | - | ||||||||||||
688 | } | - | ||||||||||||
689 | - | |||||||||||||
690 | bool QTableModel::itemGreaterThan(const QPair<QTableWidgetItem*,int> &left, | - | ||||||||||||
691 | const QPair<QTableWidgetItem*,int> &right) | - | ||||||||||||
692 | { | - | ||||||||||||
693 | return (*(right.first) < *(left .first)); | - | ||||||||||||
694 | } | - | ||||||||||||
695 | - | |||||||||||||
696 | QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const | - | ||||||||||||
697 | { | - | ||||||||||||
698 | if (section < 0) | - | ||||||||||||
699 | return QVariant(); | - | ||||||||||||
700 | - | |||||||||||||
701 | QTableWidgetItem *itm = 0; | - | ||||||||||||
702 | if (orientation == Qt::Horizontal && section < horizontalHeaderItems.count()) | - | ||||||||||||
703 | itm = horizontalHeaderItems.at(section); | - | ||||||||||||
704 | else if (orientation == Qt::Vertical && section < verticalHeaderItems.count()) | - | ||||||||||||
705 | itm = verticalHeaderItems.at(section); | - | ||||||||||||
706 | else | - | ||||||||||||
707 | return QVariant(); // section is out of bounds | - | ||||||||||||
708 | - | |||||||||||||
709 | if (itm) | - | ||||||||||||
710 | return itm->data(role); | - | ||||||||||||
711 | if (role == Qt::DisplayRole) | - | ||||||||||||
712 | return section + 1; | - | ||||||||||||
713 | return QVariant(); | - | ||||||||||||
714 | } | - | ||||||||||||
715 | - | |||||||||||||
716 | bool QTableModel::setHeaderData(int section, Qt::Orientation orientation, | - | ||||||||||||
717 | const QVariant &value, int role) | - | ||||||||||||
718 | { | - | ||||||||||||
719 | if (section < 0 || | - | ||||||||||||
720 | (orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) || | - | ||||||||||||
721 | (orientation == Qt::Vertical && verticalHeaderItems.size() <= section)) | - | ||||||||||||
722 | return false; | - | ||||||||||||
723 | - | |||||||||||||
724 | QTableWidgetItem *itm = 0; | - | ||||||||||||
725 | if (orientation == Qt::Horizontal) | - | ||||||||||||
726 | itm = horizontalHeaderItems.at(section); | - | ||||||||||||
727 | else | - | ||||||||||||
728 | itm = verticalHeaderItems.at(section); | - | ||||||||||||
729 | if (itm) { | - | ||||||||||||
730 | itm->setData(role, value); | - | ||||||||||||
731 | return true; | - | ||||||||||||
732 | } | - | ||||||||||||
733 | return false; | - | ||||||||||||
734 | } | - | ||||||||||||
735 | - | |||||||||||||
736 | bool QTableModel::isValid(const QModelIndex &index) const | - | ||||||||||||
737 | { | - | ||||||||||||
738 | return (index.isValid() | - | ||||||||||||
739 | && index.row() < verticalHeaderItems.count() | - | ||||||||||||
740 | && index.column() < horizontalHeaderItems.count()); | - | ||||||||||||
741 | } | - | ||||||||||||
742 | - | |||||||||||||
743 | void QTableModel::clear() | - | ||||||||||||
744 | { | - | ||||||||||||
745 | for (int j = 0; j < verticalHeaderItems.count(); ++j) { | - | ||||||||||||
746 | if (verticalHeaderItems.at(j)) { | - | ||||||||||||
747 | verticalHeaderItems.at(j)->view = 0; | - | ||||||||||||
748 | delete verticalHeaderItems.at(j); | - | ||||||||||||
749 | verticalHeaderItems[j] = 0; | - | ||||||||||||
750 | } | - | ||||||||||||
751 | } | - | ||||||||||||
752 | for (int k = 0; k < horizontalHeaderItems.count(); ++k) { | - | ||||||||||||
753 | if (horizontalHeaderItems.at(k)) { | - | ||||||||||||
754 | horizontalHeaderItems.at(k)->view = 0; | - | ||||||||||||
755 | delete horizontalHeaderItems.at(k); | - | ||||||||||||
756 | horizontalHeaderItems[k] = 0; | - | ||||||||||||
757 | } | - | ||||||||||||
758 | } | - | ||||||||||||
759 | clearContents(); | - | ||||||||||||
760 | } | - | ||||||||||||
761 | - | |||||||||||||
762 | void QTableModel::clearContents() | - | ||||||||||||
763 | { | - | ||||||||||||
764 | beginResetModel(); | - | ||||||||||||
765 | for (int i = 0; i < tableItems.count(); ++i) { | - | ||||||||||||
766 | if (tableItems.at(i)) { | - | ||||||||||||
767 | tableItems.at(i)->view = 0; | - | ||||||||||||
768 | delete tableItems.at(i); | - | ||||||||||||
769 | tableItems[i] = 0; | - | ||||||||||||
770 | } | - | ||||||||||||
771 | } | - | ||||||||||||
772 | endResetModel(); | - | ||||||||||||
773 | } | - | ||||||||||||
774 | - | |||||||||||||
775 | void QTableModel::itemChanged(QTableWidgetItem *item) | - | ||||||||||||
776 | { | - | ||||||||||||
777 | if (!item) | - | ||||||||||||
778 | return; | - | ||||||||||||
779 | if (item->flags() & ItemIsHeaderItem) { | - | ||||||||||||
780 | int row = verticalHeaderItems.indexOf(item); | - | ||||||||||||
781 | if (row >= 0) { | - | ||||||||||||
782 | emit headerDataChanged(Qt::Vertical, row, row); | - | ||||||||||||
783 | } else { | - | ||||||||||||
784 | int column = horizontalHeaderItems.indexOf(item); | - | ||||||||||||
785 | if (column >= 0) | - | ||||||||||||
786 | emit headerDataChanged(Qt::Horizontal, column, column); | - | ||||||||||||
787 | } | - | ||||||||||||
788 | } else { | - | ||||||||||||
789 | QModelIndex idx = index(item); | - | ||||||||||||
790 | if (idx.isValid()) | - | ||||||||||||
791 | emit dataChanged(idx, idx); | - | ||||||||||||
792 | } | - | ||||||||||||
793 | } | - | ||||||||||||
794 | - | |||||||||||||
795 | QTableWidgetItem* QTableModel::createItem() const | - | ||||||||||||
796 | { | - | ||||||||||||
797 | return prototype ? prototype->clone() : new QTableWidgetItem; | - | ||||||||||||
798 | } | - | ||||||||||||
799 | - | |||||||||||||
800 | const QTableWidgetItem *QTableModel::itemPrototype() const | - | ||||||||||||
801 | { | - | ||||||||||||
802 | return prototype; | - | ||||||||||||
803 | } | - | ||||||||||||
804 | - | |||||||||||||
805 | void QTableModel::setItemPrototype(const QTableWidgetItem *item) | - | ||||||||||||
806 | { | - | ||||||||||||
807 | if (prototype != item) { | - | ||||||||||||
808 | delete prototype; | - | ||||||||||||
809 | prototype = item; | - | ||||||||||||
810 | } | - | ||||||||||||
811 | } | - | ||||||||||||
812 | - | |||||||||||||
813 | QStringList QTableModel::mimeTypes() const | - | ||||||||||||
814 | { | - | ||||||||||||
815 | const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent()); | - | ||||||||||||
816 | return (view ? view->mimeTypes() : QStringList()); | - | ||||||||||||
817 | } | - | ||||||||||||
818 | - | |||||||||||||
819 | QMimeData *QTableModel::internalMimeData() const | - | ||||||||||||
820 | { | - | ||||||||||||
821 | return QAbstractTableModel::mimeData(cachedIndexes); | - | ||||||||||||
822 | } | - | ||||||||||||
823 | - | |||||||||||||
824 | QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const | - | ||||||||||||
825 | { | - | ||||||||||||
826 | QList<QTableWidgetItem*> items; | - | ||||||||||||
827 | const int indexesCount = indexes.count(); | - | ||||||||||||
828 | items.reserve(indexesCount); | - | ||||||||||||
829 | for (int i = 0; i < indexesCount; ++i) | - | ||||||||||||
830 | items << item(indexes.at(i)); | - | ||||||||||||
831 | const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent()); | - | ||||||||||||
832 | - | |||||||||||||
833 | // cachedIndexes is a little hack to avoid copying from QModelIndexList to | - | ||||||||||||
834 | // QList<QTreeWidgetItem*> and back again in the view | - | ||||||||||||
835 | cachedIndexes = indexes; | - | ||||||||||||
836 | QMimeData *mimeData = (view ? view->mimeData(items) : 0); | - | ||||||||||||
837 | cachedIndexes.clear(); | - | ||||||||||||
838 | return mimeData; | - | ||||||||||||
839 | } | - | ||||||||||||
840 | - | |||||||||||||
841 | bool QTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, | - | ||||||||||||
842 | int row , int column, const QModelIndex &index) | - | ||||||||||||
843 | { | - | ||||||||||||
844 | if (index.isValid()) { | - | ||||||||||||
845 | row = index.row(); | - | ||||||||||||
846 | column = index.column(); | - | ||||||||||||
847 | }else if (row == -1 || column == -1) { // The user dropped outside the table. | - | ||||||||||||
848 | row = rowCount(); | - | ||||||||||||
849 | column = 0; | - | ||||||||||||
850 | } | - | ||||||||||||
851 | - | |||||||||||||
852 | QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent()); | - | ||||||||||||
853 | return (view ? view->dropMimeData(row, column, data, action) : false); | - | ||||||||||||
854 | } | - | ||||||||||||
855 | - | |||||||||||||
856 | Qt::DropActions QTableModel::supportedDropActions() const | - | ||||||||||||
857 | { | - | ||||||||||||
858 | const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent()); | - | ||||||||||||
859 | return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction)); | - | ||||||||||||
860 | } | - | ||||||||||||
861 | - | |||||||||||||
862 | /*! | - | ||||||||||||
863 | \class QTableWidgetSelectionRange | - | ||||||||||||
864 | - | |||||||||||||
865 | \brief The QTableWidgetSelectionRange class provides a way to interact with | - | ||||||||||||
866 | selection in a model without using model indexes and a selection model. | - | ||||||||||||
867 | - | |||||||||||||
868 | \ingroup model-view | - | ||||||||||||
869 | \inmodule QtWidgets | - | ||||||||||||
870 | - | |||||||||||||
871 | The QTableWidgetSelectionRange class stores the top left and bottom | - | ||||||||||||
872 | right rows and columns of a selection range in a table. The | - | ||||||||||||
873 | selections in the table may consist of several selection ranges. | - | ||||||||||||
874 | - | |||||||||||||
875 | \note If the item within the selection range is marked as not selectable, | - | ||||||||||||
876 | e.g., \c{itemFlags() & Qt::ItemIsSelectable == 0} then it will not appear | - | ||||||||||||
877 | in the selection range. | - | ||||||||||||
878 | - | |||||||||||||
879 | \sa QTableWidget | - | ||||||||||||
880 | */ | - | ||||||||||||
881 | - | |||||||||||||
882 | /*! | - | ||||||||||||
883 | Constructs an table selection range, i.e. a range | - | ||||||||||||
884 | whose rowCount() and columnCount() are 0. | - | ||||||||||||
885 | */ | - | ||||||||||||
886 | QTableWidgetSelectionRange::QTableWidgetSelectionRange() | - | ||||||||||||
887 | : top(-1), left(-1), bottom(-2), right(-2) | - | ||||||||||||
888 | { | - | ||||||||||||
889 | } | - | ||||||||||||
890 | - | |||||||||||||
891 | /*! | - | ||||||||||||
892 | Constructs the table selection range from the given \a top, \a | - | ||||||||||||
893 | left, \a bottom and \a right table rows and columns. | - | ||||||||||||
894 | - | |||||||||||||
895 | \sa topRow(), leftColumn(), bottomRow(), rightColumn() | - | ||||||||||||
896 | */ | - | ||||||||||||
897 | QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right) | - | ||||||||||||
898 | : top(top), left(left), bottom(bottom), right(right) | - | ||||||||||||
899 | { | - | ||||||||||||
900 | } | - | ||||||||||||
901 | - | |||||||||||||
902 | /*! | - | ||||||||||||
903 | Constructs a the table selection range by copying the given \a | - | ||||||||||||
904 | other table selection range. | - | ||||||||||||
905 | */ | - | ||||||||||||
906 | QTableWidgetSelectionRange::QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other) | - | ||||||||||||
907 | : top(other.top), left(other.left), bottom(other.bottom), right(other.right) | - | ||||||||||||
908 | { | - | ||||||||||||
909 | } | - | ||||||||||||
910 | - | |||||||||||||
911 | /*! | - | ||||||||||||
912 | Destroys the table selection range. | - | ||||||||||||
913 | */ | - | ||||||||||||
914 | QTableWidgetSelectionRange::~QTableWidgetSelectionRange() | - | ||||||||||||
915 | { | - | ||||||||||||
916 | } | - | ||||||||||||
917 | - | |||||||||||||
918 | /*! | - | ||||||||||||
919 | \fn int QTableWidgetSelectionRange::topRow() const | - | ||||||||||||
920 | - | |||||||||||||
921 | Returns the top row of the range. | - | ||||||||||||
922 | - | |||||||||||||
923 | \sa bottomRow(), leftColumn(), rowCount() | - | ||||||||||||
924 | */ | - | ||||||||||||
925 | - | |||||||||||||
926 | /*! | - | ||||||||||||
927 | \fn int QTableWidgetSelectionRange::bottomRow() const | - | ||||||||||||
928 | - | |||||||||||||
929 | Returns the bottom row of the range. | - | ||||||||||||
930 | - | |||||||||||||
931 | \sa topRow(), rightColumn(), rowCount() | - | ||||||||||||
932 | */ | - | ||||||||||||
933 | - | |||||||||||||
934 | /*! | - | ||||||||||||
935 | \fn int QTableWidgetSelectionRange::leftColumn() const | - | ||||||||||||
936 | - | |||||||||||||
937 | Returns the left column of the range. | - | ||||||||||||
938 | - | |||||||||||||
939 | \sa rightColumn(), topRow(), columnCount() | - | ||||||||||||
940 | */ | - | ||||||||||||
941 | - | |||||||||||||
942 | /*! | - | ||||||||||||
943 | \fn int QTableWidgetSelectionRange::rightColumn() const | - | ||||||||||||
944 | - | |||||||||||||
945 | Returns the right column of the range. | - | ||||||||||||
946 | - | |||||||||||||
947 | \sa leftColumn(), bottomRow(), columnCount() | - | ||||||||||||
948 | */ | - | ||||||||||||
949 | - | |||||||||||||
950 | /*! | - | ||||||||||||
951 | \since 4.1 | - | ||||||||||||
952 | \fn int QTableWidgetSelectionRange::rowCount() const | - | ||||||||||||
953 | - | |||||||||||||
954 | Returns the number of rows in the range. | - | ||||||||||||
955 | - | |||||||||||||
956 | This is equivalent to bottomRow() - topRow() + 1. | - | ||||||||||||
957 | - | |||||||||||||
958 | \sa columnCount(), topRow(), bottomRow() | - | ||||||||||||
959 | */ | - | ||||||||||||
960 | - | |||||||||||||
961 | /*! | - | ||||||||||||
962 | \since 4.1 | - | ||||||||||||
963 | \fn int QTableWidgetSelectionRange::columnCount() const | - | ||||||||||||
964 | - | |||||||||||||
965 | Returns the number of columns in the range. | - | ||||||||||||
966 | - | |||||||||||||
967 | This is equivalent to rightColumn() - leftColumn() + 1. | - | ||||||||||||
968 | - | |||||||||||||
969 | \sa rowCount(), leftColumn(), rightColumn() | - | ||||||||||||
970 | */ | - | ||||||||||||
971 | - | |||||||||||||
972 | /*! | - | ||||||||||||
973 | \class QTableWidgetItem | - | ||||||||||||
974 | \brief The QTableWidgetItem class provides an item for use with the | - | ||||||||||||
975 | QTableWidget class. | - | ||||||||||||
976 | - | |||||||||||||
977 | \ingroup model-view | - | ||||||||||||
978 | \inmodule QtWidgets | - | ||||||||||||
979 | - | |||||||||||||
980 | Table items are used to hold pieces of information for table widgets. | - | ||||||||||||
981 | Items usually contain text, icons, or checkboxes | - | ||||||||||||
982 | - | |||||||||||||
983 | The QTableWidgetItem class is a convenience class that replaces the | - | ||||||||||||
984 | \c QTableItem class in Qt 3. It provides an item for use with | - | ||||||||||||
985 | the QTableWidget class. | - | ||||||||||||
986 | - | |||||||||||||
987 | Top-level items are constructed without a parent then inserted at the | - | ||||||||||||
988 | position specified by a pair of row and column numbers: | - | ||||||||||||
989 | - | |||||||||||||
990 | \snippet qtablewidget-using/mainwindow.cpp 3 | - | ||||||||||||
991 | - | |||||||||||||
992 | Each item can have its own background brush which is set with | - | ||||||||||||
993 | the setBackground() function. The current background brush can be | - | ||||||||||||
994 | found with background(). | - | ||||||||||||
995 | The text label for each item can be rendered with its own font and brush. | - | ||||||||||||
996 | These are specified with the setFont() and setForeground() functions, | - | ||||||||||||
997 | and read with font() and foreground(). | - | ||||||||||||
998 | - | |||||||||||||
999 | By default, items are enabled, editable, selectable, checkable, and can be | - | ||||||||||||
1000 | used both as the source of a drag and drop operation and as a drop target. | - | ||||||||||||
1001 | Each item's flags can be changed by calling setFlags() with the appropriate | - | ||||||||||||
1002 | value (see \l{Qt::ItemFlags}). Checkable items can be checked and unchecked | - | ||||||||||||
1003 | with the setCheckState() function. The corresponding checkState() function | - | ||||||||||||
1004 | indicates whether the item is currently checked. | - | ||||||||||||
1005 | - | |||||||||||||
1006 | \section1 Subclassing | - | ||||||||||||
1007 | - | |||||||||||||
1008 | When subclassing QTableWidgetItem to provide custom items, it is possible to | - | ||||||||||||
1009 | define new types for them so that they can be distinguished from standard | - | ||||||||||||
1010 | items. The constructors for subclasses that require this feature need to | - | ||||||||||||
1011 | call the base class constructor with a new type value equal to or greater | - | ||||||||||||
1012 | than \l UserType. | - | ||||||||||||
1013 | - | |||||||||||||
1014 | \sa QTableWidget, {Model/View Programming}, QListWidgetItem, QTreeWidgetItem | - | ||||||||||||
1015 | */ | - | ||||||||||||
1016 | - | |||||||||||||
1017 | /*! | - | ||||||||||||
1018 | \fn int QTableWidgetItem::row() const | - | ||||||||||||
1019 | \since 4.2 | - | ||||||||||||
1020 | - | |||||||||||||
1021 | Returns the row of the item in the table. | - | ||||||||||||
1022 | If the item is not in a table, this function will return -1. | - | ||||||||||||
1023 | - | |||||||||||||
1024 | \sa column() | - | ||||||||||||
1025 | */ | - | ||||||||||||
1026 | - | |||||||||||||
1027 | /*! | - | ||||||||||||
1028 | \fn int QTableWidgetItem::column() const | - | ||||||||||||
1029 | \since 4.2 | - | ||||||||||||
1030 | - | |||||||||||||
1031 | Returns the column of the item in the table. | - | ||||||||||||
1032 | If the item is not in a table, this function will return -1. | - | ||||||||||||
1033 | - | |||||||||||||
1034 | \sa row() | - | ||||||||||||
1035 | */ | - | ||||||||||||
1036 | - | |||||||||||||
1037 | /*! | - | ||||||||||||
1038 | \fn void QTableWidgetItem::setSelected(bool select) | - | ||||||||||||
1039 | \since 4.2 | - | ||||||||||||
1040 | - | |||||||||||||
1041 | Sets the selected state of the item to \a select. | - | ||||||||||||
1042 | - | |||||||||||||
1043 | \sa isSelected() | - | ||||||||||||
1044 | */ | - | ||||||||||||
1045 | - | |||||||||||||
1046 | /*! | - | ||||||||||||
1047 | \fn bool QTableWidgetItem::isSelected() const | - | ||||||||||||
1048 | \since 4.2 | - | ||||||||||||
1049 | - | |||||||||||||
1050 | Returns \c true if the item is selected, otherwise returns \c false. | - | ||||||||||||
1051 | - | |||||||||||||
1052 | \sa setSelected() | - | ||||||||||||
1053 | */ | - | ||||||||||||
1054 | - | |||||||||||||
1055 | /*! | - | ||||||||||||
1056 | \fn QSize QTableWidgetItem::sizeHint() const | - | ||||||||||||
1057 | \since 4.1 | - | ||||||||||||
1058 | - | |||||||||||||
1059 | Returns the size hint set for the table item. | - | ||||||||||||
1060 | */ | - | ||||||||||||
1061 | - | |||||||||||||
1062 | /*! | - | ||||||||||||
1063 | \fn void QTableWidgetItem::setSizeHint(const QSize &size) | - | ||||||||||||
1064 | \since 4.1 | - | ||||||||||||
1065 | - | |||||||||||||
1066 | Sets the size hint for the table item to be \a size. | - | ||||||||||||
1067 | If no size hint is set, the item delegate will compute the | - | ||||||||||||
1068 | size hint based on the item data. | - | ||||||||||||
1069 | */ | - | ||||||||||||
1070 | - | |||||||||||||
1071 | /*! | - | ||||||||||||
1072 | \fn Qt::CheckState QTableWidgetItem::checkState() const | - | ||||||||||||
1073 | - | |||||||||||||
1074 | Returns the checked state of the table item. | - | ||||||||||||
1075 | - | |||||||||||||
1076 | \sa flags() | - | ||||||||||||
1077 | */ | - | ||||||||||||
1078 | - | |||||||||||||
1079 | /*! | - | ||||||||||||
1080 | \fn void QTableWidgetItem::setCheckState(Qt::CheckState state) | - | ||||||||||||
1081 | - | |||||||||||||
1082 | Sets the check state of the table item to be \a state. | - | ||||||||||||
1083 | */ | - | ||||||||||||
1084 | - | |||||||||||||
1085 | /*! | - | ||||||||||||
1086 | \fn QTableWidget *QTableWidgetItem::tableWidget() const | - | ||||||||||||
1087 | - | |||||||||||||
1088 | Returns the table widget that contains the item. | - | ||||||||||||
1089 | */ | - | ||||||||||||
1090 | - | |||||||||||||
1091 | /*! | - | ||||||||||||
1092 | \fn Qt::ItemFlags QTableWidgetItem::flags() const | - | ||||||||||||
1093 | - | |||||||||||||
1094 | Returns the flags used to describe the item. These determine whether | - | ||||||||||||
1095 | the item can be checked, edited, and selected. | - | ||||||||||||
1096 | - | |||||||||||||
1097 | \sa setFlags() | - | ||||||||||||
1098 | */ | - | ||||||||||||
1099 | - | |||||||||||||
1100 | /*! | - | ||||||||||||
1101 | \fn void QTableWidgetItem::setFlags(Qt::ItemFlags flags) | - | ||||||||||||
1102 | - | |||||||||||||
1103 | Sets the flags for the item to the given \a flags. These determine whether | - | ||||||||||||
1104 | the item can be selected or modified. | - | ||||||||||||
1105 | - | |||||||||||||
1106 | \sa flags() | - | ||||||||||||
1107 | */ | - | ||||||||||||
1108 | void QTableWidgetItem::setFlags(Qt::ItemFlags aflags) | - | ||||||||||||
1109 | { | - | ||||||||||||
1110 | itemFlags = aflags; | - | ||||||||||||
1111 | if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0)) | - | ||||||||||||
1112 | model->itemChanged(this); | - | ||||||||||||
1113 | } | - | ||||||||||||
1114 | - | |||||||||||||
1115 | - | |||||||||||||
1116 | /*! | - | ||||||||||||
1117 | \fn QString QTableWidgetItem::text() const | - | ||||||||||||
1118 | - | |||||||||||||
1119 | Returns the item's text. | - | ||||||||||||
1120 | - | |||||||||||||
1121 | \sa setText() | - | ||||||||||||
1122 | */ | - | ||||||||||||
1123 | - | |||||||||||||
1124 | /*! | - | ||||||||||||
1125 | \fn void QTableWidgetItem::setText(const QString &text) | - | ||||||||||||
1126 | - | |||||||||||||
1127 | Sets the item's text to the \a text specified. | - | ||||||||||||
1128 | - | |||||||||||||
1129 | \sa text(), setFont(), setForeground() | - | ||||||||||||
1130 | */ | - | ||||||||||||
1131 | - | |||||||||||||
1132 | /*! | - | ||||||||||||
1133 | \fn QIcon QTableWidgetItem::icon() const | - | ||||||||||||
1134 | - | |||||||||||||
1135 | Returns the item's icon. | - | ||||||||||||
1136 | - | |||||||||||||
1137 | \sa setIcon(), {QAbstractItemView::iconSize}{iconSize} | - | ||||||||||||
1138 | */ | - | ||||||||||||
1139 | - | |||||||||||||
1140 | /*! | - | ||||||||||||
1141 | \fn void QTableWidgetItem::setIcon(const QIcon &icon) | - | ||||||||||||
1142 | - | |||||||||||||
1143 | Sets the item's icon to the \a icon specified. | - | ||||||||||||
1144 | - | |||||||||||||
1145 | \sa icon(), setText(), {QAbstractItemView::iconSize}{iconSize} | - | ||||||||||||
1146 | */ | - | ||||||||||||
1147 | - | |||||||||||||
1148 | /*! | - | ||||||||||||
1149 | \fn QString QTableWidgetItem::statusTip() const | - | ||||||||||||
1150 | - | |||||||||||||
1151 | Returns the item's status tip. | - | ||||||||||||
1152 | - | |||||||||||||
1153 | \sa setStatusTip() | - | ||||||||||||
1154 | */ | - | ||||||||||||
1155 | - | |||||||||||||
1156 | /*! | - | ||||||||||||
1157 | \fn void QTableWidgetItem::setStatusTip(const QString &statusTip) | - | ||||||||||||
1158 | - | |||||||||||||
1159 | Sets the status tip for the table item to the text specified by | - | ||||||||||||
1160 | \a statusTip. QTableWidget mouse tracking needs to be enabled for this | - | ||||||||||||
1161 | feature to work. | - | ||||||||||||
1162 | - | |||||||||||||
1163 | \sa statusTip(), setToolTip(), setWhatsThis() | - | ||||||||||||
1164 | */ | - | ||||||||||||
1165 | - | |||||||||||||
1166 | /*! | - | ||||||||||||
1167 | \fn QString QTableWidgetItem::toolTip() const | - | ||||||||||||
1168 | - | |||||||||||||
1169 | Returns the item's tooltip. | - | ||||||||||||
1170 | - | |||||||||||||
1171 | \sa setToolTip() | - | ||||||||||||
1172 | */ | - | ||||||||||||
1173 | - | |||||||||||||
1174 | /*! | - | ||||||||||||
1175 | \fn void QTableWidgetItem::setToolTip(const QString &toolTip) | - | ||||||||||||
1176 | - | |||||||||||||
1177 | Sets the item's tooltip to the string specified by \a toolTip. | - | ||||||||||||
1178 | - | |||||||||||||
1179 | \sa toolTip(), setStatusTip(), setWhatsThis() | - | ||||||||||||
1180 | */ | - | ||||||||||||
1181 | - | |||||||||||||
1182 | /*! | - | ||||||||||||
1183 | \fn QString QTableWidgetItem::whatsThis() const | - | ||||||||||||
1184 | - | |||||||||||||
1185 | Returns the item's "What's This?" help. | - | ||||||||||||
1186 | - | |||||||||||||
1187 | \sa setWhatsThis() | - | ||||||||||||
1188 | */ | - | ||||||||||||
1189 | - | |||||||||||||
1190 | /*! | - | ||||||||||||
1191 | \fn void QTableWidgetItem::setWhatsThis(const QString &whatsThis) | - | ||||||||||||
1192 | - | |||||||||||||
1193 | Sets the item's "What's This?" help to the string specified by \a whatsThis. | - | ||||||||||||
1194 | - | |||||||||||||
1195 | \sa whatsThis(), setStatusTip(), setToolTip() | - | ||||||||||||
1196 | */ | - | ||||||||||||
1197 | - | |||||||||||||
1198 | /*! | - | ||||||||||||
1199 | \fn QFont QTableWidgetItem::font() const | - | ||||||||||||
1200 | - | |||||||||||||
1201 | Returns the font used to render the item's text. | - | ||||||||||||
1202 | - | |||||||||||||
1203 | \sa setFont() | - | ||||||||||||
1204 | */ | - | ||||||||||||
1205 | - | |||||||||||||
1206 | /*! | - | ||||||||||||
1207 | \fn void QTableWidgetItem::setFont(const QFont &font) | - | ||||||||||||
1208 | - | |||||||||||||
1209 | Sets the font used to display the item's text to the given \a font. | - | ||||||||||||
1210 | - | |||||||||||||
1211 | \sa font(), setText(), setForeground() | - | ||||||||||||
1212 | */ | - | ||||||||||||
1213 | - | |||||||||||||
1214 | /*! | - | ||||||||||||
1215 | \fn QColor QTableWidgetItem::backgroundColor() const | - | ||||||||||||
1216 | \obsolete | - | ||||||||||||
1217 | - | |||||||||||||
1218 | This function is deprecated. Use background() instead. | - | ||||||||||||
1219 | */ | - | ||||||||||||
1220 | - | |||||||||||||
1221 | /*! | - | ||||||||||||
1222 | \fn void QTableWidgetItem::setBackgroundColor(const QColor &color) | - | ||||||||||||
1223 | \obsolete | - | ||||||||||||
1224 | - | |||||||||||||
1225 | This function is deprecated. Use setBackground() instead. | - | ||||||||||||
1226 | */ | - | ||||||||||||
1227 | - | |||||||||||||
1228 | /*! | - | ||||||||||||
1229 | \fn QBrush QTableWidgetItem::background() const | - | ||||||||||||
1230 | \since 4.2 | - | ||||||||||||
1231 | - | |||||||||||||
1232 | Returns the brush used to render the item's background. | - | ||||||||||||
1233 | - | |||||||||||||
1234 | \sa foreground() | - | ||||||||||||
1235 | */ | - | ||||||||||||
1236 | - | |||||||||||||
1237 | /*! | - | ||||||||||||
1238 | \fn void QTableWidgetItem::setBackground(const QBrush &brush) | - | ||||||||||||
1239 | \since 4.2 | - | ||||||||||||
1240 | - | |||||||||||||
1241 | Sets the item's background brush to the specified \a brush. | - | ||||||||||||
1242 | - | |||||||||||||
1243 | \sa setForeground() | - | ||||||||||||
1244 | */ | - | ||||||||||||
1245 | - | |||||||||||||
1246 | /*! | - | ||||||||||||
1247 | \fn QColor QTableWidgetItem::textColor() const | - | ||||||||||||
1248 | \obsolete | - | ||||||||||||
1249 | - | |||||||||||||
1250 | This function is deprecated. Use foreground() instead. | - | ||||||||||||
1251 | */ | - | ||||||||||||
1252 | - | |||||||||||||
1253 | /*! | - | ||||||||||||
1254 | \fn void QTableWidgetItem::setTextColor(const QColor &color) | - | ||||||||||||
1255 | \obsolete | - | ||||||||||||
1256 | - | |||||||||||||
1257 | This function is deprecated. Use setForeground() instead. | - | ||||||||||||
1258 | */ | - | ||||||||||||
1259 | - | |||||||||||||
1260 | /*! | - | ||||||||||||
1261 | \fn QBrush QTableWidgetItem::foreground() const | - | ||||||||||||
1262 | \since 4.2 | - | ||||||||||||
1263 | - | |||||||||||||
1264 | Returns the brush used to render the item's foreground (e.g. text). | - | ||||||||||||
1265 | - | |||||||||||||
1266 | \sa background() | - | ||||||||||||
1267 | */ | - | ||||||||||||
1268 | - | |||||||||||||
1269 | /*! | - | ||||||||||||
1270 | \fn void QTableWidgetItem::setForeground(const QBrush &brush) | - | ||||||||||||
1271 | \since 4.2 | - | ||||||||||||
1272 | - | |||||||||||||
1273 | Sets the item's foreground brush to the specified \a brush. | - | ||||||||||||
1274 | - | |||||||||||||
1275 | \sa setBackground() | - | ||||||||||||
1276 | */ | - | ||||||||||||
1277 | - | |||||||||||||
1278 | /*! | - | ||||||||||||
1279 | \fn int QTableWidgetItem::textAlignment() const | - | ||||||||||||
1280 | - | |||||||||||||
1281 | Returns the text alignment for the item's text. | - | ||||||||||||
1282 | - | |||||||||||||
1283 | \sa Qt::Alignment | - | ||||||||||||
1284 | */ | - | ||||||||||||
1285 | - | |||||||||||||
1286 | /*! | - | ||||||||||||
1287 | \fn void QTableWidgetItem::setTextAlignment(int alignment) | - | ||||||||||||
1288 | - | |||||||||||||
1289 | Sets the text alignment for the item's text to the \a alignment | - | ||||||||||||
1290 | specified. | - | ||||||||||||
1291 | - | |||||||||||||
1292 | \sa Qt::Alignment | - | ||||||||||||
1293 | */ | - | ||||||||||||
1294 | - | |||||||||||||
1295 | /*! | - | ||||||||||||
1296 | Constructs a table item of the specified \a type that does not belong | - | ||||||||||||
1297 | to any table. | - | ||||||||||||
1298 | - | |||||||||||||
1299 | \sa type() | - | ||||||||||||
1300 | */ | - | ||||||||||||
1301 | QTableWidgetItem::QTableWidgetItem(int type) | - | ||||||||||||
1302 | : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)), | - | ||||||||||||
1303 | itemFlags(Qt::ItemIsEditable | - | ||||||||||||
1304 | |Qt::ItemIsSelectable | - | ||||||||||||
1305 | |Qt::ItemIsUserCheckable | - | ||||||||||||
1306 | |Qt::ItemIsEnabled | - | ||||||||||||
1307 | |Qt::ItemIsDragEnabled | - | ||||||||||||
1308 | |Qt::ItemIsDropEnabled) | - | ||||||||||||
1309 | { | - | ||||||||||||
1310 | } | - | ||||||||||||
1311 | - | |||||||||||||
1312 | /*! | - | ||||||||||||
1313 | Constructs a table item with the given \a text. | - | ||||||||||||
1314 | - | |||||||||||||
1315 | \sa type() | - | ||||||||||||
1316 | */ | - | ||||||||||||
1317 | QTableWidgetItem::QTableWidgetItem(const QString &text, int type) | - | ||||||||||||
1318 | : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)), | - | ||||||||||||
1319 | itemFlags(Qt::ItemIsEditable | - | ||||||||||||
1320 | |Qt::ItemIsSelectable | - | ||||||||||||
1321 | |Qt::ItemIsUserCheckable | - | ||||||||||||
1322 | |Qt::ItemIsEnabled | - | ||||||||||||
1323 | |Qt::ItemIsDragEnabled | - | ||||||||||||
1324 | |Qt::ItemIsDropEnabled) | - | ||||||||||||
1325 | { | - | ||||||||||||
1326 | setData(Qt::DisplayRole, text); | - | ||||||||||||
1327 | } | - | ||||||||||||
1328 | - | |||||||||||||
1329 | /*! | - | ||||||||||||
1330 | Constructs a table item with the given \a icon and \a text. | - | ||||||||||||
1331 | - | |||||||||||||
1332 | \sa type() | - | ||||||||||||
1333 | */ | - | ||||||||||||
1334 | QTableWidgetItem::QTableWidgetItem(const QIcon &icon, const QString &text, int type) | - | ||||||||||||
1335 | : rtti(type), view(0), d(new QTableWidgetItemPrivate(this)), | - | ||||||||||||
1336 | itemFlags(Qt::ItemIsEditable | - | ||||||||||||
1337 | |Qt::ItemIsSelectable | - | ||||||||||||
1338 | |Qt::ItemIsUserCheckable | - | ||||||||||||
1339 | |Qt::ItemIsEnabled | - | ||||||||||||
1340 | |Qt::ItemIsDragEnabled | - | ||||||||||||
1341 | |Qt::ItemIsDropEnabled) | - | ||||||||||||
1342 | { | - | ||||||||||||
1343 | setData(Qt::DecorationRole, icon); | - | ||||||||||||
1344 | setData(Qt::DisplayRole, text); | - | ||||||||||||
1345 | } | - | ||||||||||||
1346 | - | |||||||||||||
1347 | /*! | - | ||||||||||||
1348 | Destroys the table item. | - | ||||||||||||
1349 | */ | - | ||||||||||||
1350 | QTableWidgetItem::~QTableWidgetItem() | - | ||||||||||||
1351 | { | - | ||||||||||||
1352 | if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0)) | - | ||||||||||||
1353 | model->removeItem(this); | - | ||||||||||||
1354 | view = 0; | - | ||||||||||||
1355 | delete d; | - | ||||||||||||
1356 | } | - | ||||||||||||
1357 | - | |||||||||||||
1358 | /*! | - | ||||||||||||
1359 | Creates a copy of the item. | - | ||||||||||||
1360 | */ | - | ||||||||||||
1361 | QTableWidgetItem *QTableWidgetItem::clone() const | - | ||||||||||||
1362 | { | - | ||||||||||||
1363 | return new QTableWidgetItem(*this); | - | ||||||||||||
1364 | } | - | ||||||||||||
1365 | - | |||||||||||||
1366 | /*! | - | ||||||||||||
1367 | Sets the item's data for the given \a role to the specified \a value. | - | ||||||||||||
1368 | - | |||||||||||||
1369 | \sa Qt::ItemDataRole, data() | - | ||||||||||||
1370 | */ | - | ||||||||||||
1371 | void QTableWidgetItem::setData(int role, const QVariant &value) | - | ||||||||||||
1372 | { | - | ||||||||||||
1373 | bool found = false; | - | ||||||||||||
1374 | role = (role == Qt::EditRole ? Qt::DisplayRole : role); | - | ||||||||||||
1375 | for (int i = 0; i < values.count(); ++i) { | - | ||||||||||||
1376 | if (values.at(i).role == role) { | - | ||||||||||||
1377 | if (values[i].value == value) | - | ||||||||||||
1378 | return; | - | ||||||||||||
1379 | - | |||||||||||||
1380 | values[i].value = value; | - | ||||||||||||
1381 | found = true; | - | ||||||||||||
1382 | break; | - | ||||||||||||
1383 | } | - | ||||||||||||
1384 | } | - | ||||||||||||
1385 | if (!found) | - | ||||||||||||
1386 | values.append(QWidgetItemData(role, value)); | - | ||||||||||||
1387 | if (QTableModel *model = (view ? qobject_cast<QTableModel*>(view->model()) : 0)) | - | ||||||||||||
1388 | model->itemChanged(this); | - | ||||||||||||
1389 | } | - | ||||||||||||
1390 | - | |||||||||||||
1391 | /*! | - | ||||||||||||
1392 | Returns the item's data for the given \a role. | - | ||||||||||||
1393 | */ | - | ||||||||||||
1394 | QVariant QTableWidgetItem::data(int role) const | - | ||||||||||||
1395 | { | - | ||||||||||||
1396 | role = (role == Qt::EditRole ? Qt::DisplayRole : role);
| 0 | ||||||||||||
1397 | for (int i = 0; i <const auto &value : values.count(); ++i) { | - | ||||||||||||
1398 | if (valuesvalue.at(i).role == role)
| 0 | ||||||||||||
1399 | return valuesvalue.at(i).value; never executed: return value.value; | 0 | ||||||||||||
1400 | } never executed: end of block | 0 | ||||||||||||
1401 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||
1402 | } | - | ||||||||||||
1403 | - | |||||||||||||
1404 | /*! | - | ||||||||||||
1405 | Returns \c true if the item is less than the \a other item; otherwise returns | - | ||||||||||||
1406 | false. | - | ||||||||||||
1407 | */ | - | ||||||||||||
1408 | bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const | - | ||||||||||||
1409 | { | - | ||||||||||||
1410 | const QVariant v1 = data(Qt::DisplayRole), v2 = other.data(Qt::DisplayRole); | - | ||||||||||||
1411 | return QAbstractItemModelPrivate::variantLessThan(v1, v2); | - | ||||||||||||
1412 | } | - | ||||||||||||
1413 | - | |||||||||||||
1414 | #ifndef QT_NO_DATASTREAM | - | ||||||||||||
1415 | - | |||||||||||||
1416 | /*! | - | ||||||||||||
1417 | Reads the item from stream \a in. | - | ||||||||||||
1418 | - | |||||||||||||
1419 | \sa write() | - | ||||||||||||
1420 | */ | - | ||||||||||||
1421 | void QTableWidgetItem::read(QDataStream &in) | - | ||||||||||||
1422 | { | - | ||||||||||||
1423 | in >> values; | - | ||||||||||||
1424 | } | - | ||||||||||||
1425 | - | |||||||||||||
1426 | /*! | - | ||||||||||||
1427 | Writes the item to stream \a out. | - | ||||||||||||
1428 | - | |||||||||||||
1429 | \sa read() | - | ||||||||||||
1430 | */ | - | ||||||||||||
1431 | void QTableWidgetItem::write(QDataStream &out) const | - | ||||||||||||
1432 | { | - | ||||||||||||
1433 | out << values; | - | ||||||||||||
1434 | } | - | ||||||||||||
1435 | - | |||||||||||||
1436 | /*! | - | ||||||||||||
1437 | \relates QTableWidgetItem | - | ||||||||||||
1438 | - | |||||||||||||
1439 | Reads a table widget item from stream \a in into \a item. | - | ||||||||||||
1440 | - | |||||||||||||
1441 | This operator uses QTableWidgetItem::read(). | - | ||||||||||||
1442 | - | |||||||||||||
1443 | \sa {Serializing Qt Data Types} | - | ||||||||||||
1444 | */ | - | ||||||||||||
1445 | QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item) | - | ||||||||||||
1446 | { | - | ||||||||||||
1447 | item.read(in); | - | ||||||||||||
1448 | return in; | - | ||||||||||||
1449 | } | - | ||||||||||||
1450 | - | |||||||||||||
1451 | /*! | - | ||||||||||||
1452 | \relates QTableWidgetItem | - | ||||||||||||
1453 | - | |||||||||||||
1454 | Writes the table widget item \a item to stream \a out. | - | ||||||||||||
1455 | - | |||||||||||||
1456 | This operator uses QTableWidgetItem::write(). | - | ||||||||||||
1457 | - | |||||||||||||
1458 | \sa {Serializing Qt Data Types} | - | ||||||||||||
1459 | */ | - | ||||||||||||
1460 | QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item) | - | ||||||||||||
1461 | { | - | ||||||||||||
1462 | item.write(out); | - | ||||||||||||
1463 | return out; | - | ||||||||||||
1464 | } | - | ||||||||||||
1465 | - | |||||||||||||
1466 | #endif // QT_NO_DATASTREAM | - | ||||||||||||
1467 | - | |||||||||||||
1468 | /*! | - | ||||||||||||
1469 | \since 4.1 | - | ||||||||||||
1470 | - | |||||||||||||
1471 | Constructs a copy of \a other. Note that type() and tableWidget() | - | ||||||||||||
1472 | are not copied. | - | ||||||||||||
1473 | - | |||||||||||||
1474 | This function is useful when reimplementing clone(). | - | ||||||||||||
1475 | - | |||||||||||||
1476 | \sa data(), flags() | - | ||||||||||||
1477 | */ | - | ||||||||||||
1478 | QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other) | - | ||||||||||||
1479 | : rtti(Type), values(other.values), view(0), | - | ||||||||||||
1480 | d(new QTableWidgetItemPrivate(this)), | - | ||||||||||||
1481 | itemFlags(other.itemFlags) | - | ||||||||||||
1482 | { | - | ||||||||||||
1483 | } | - | ||||||||||||
1484 | - | |||||||||||||
1485 | /*! | - | ||||||||||||
1486 | Assigns \a other's data and flags to this item. Note that type() | - | ||||||||||||
1487 | and tableWidget() are not copied. | - | ||||||||||||
1488 | - | |||||||||||||
1489 | This function is useful when reimplementing clone(). | - | ||||||||||||
1490 | - | |||||||||||||
1491 | \sa data(), flags() | - | ||||||||||||
1492 | */ | - | ||||||||||||
1493 | QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other) | - | ||||||||||||
1494 | { | - | ||||||||||||
1495 | values = other.values; | - | ||||||||||||
1496 | itemFlags = other.itemFlags; | - | ||||||||||||
1497 | return *this; | - | ||||||||||||
1498 | } | - | ||||||||||||
1499 | - | |||||||||||||
1500 | /*! | - | ||||||||||||
1501 | \class QTableWidget | - | ||||||||||||
1502 | \brief The QTableWidget class provides an item-based table view with a default model. | - | ||||||||||||
1503 | - | |||||||||||||
1504 | \ingroup model-view | - | ||||||||||||
1505 | \inmodule QtWidgets | - | ||||||||||||
1506 | - | |||||||||||||
1507 | Table widgets provide standard table display facilities for applications. | - | ||||||||||||
1508 | The items in a QTableWidget are provided by QTableWidgetItem. | - | ||||||||||||
1509 | - | |||||||||||||
1510 | If you want a table that uses your own data model you should | - | ||||||||||||
1511 | use QTableView rather than this class. | - | ||||||||||||
1512 | - | |||||||||||||
1513 | Table widgets can be constructed with the required numbers of rows and | - | ||||||||||||
1514 | columns: | - | ||||||||||||
1515 | - | |||||||||||||
1516 | \snippet qtablewidget-using/mainwindow.cpp 0 | - | ||||||||||||
1517 | - | |||||||||||||
1518 | Alternatively, tables can be constructed without a given size and resized | - | ||||||||||||
1519 | later: | - | ||||||||||||
1520 | - | |||||||||||||
1521 | \snippet qtablewidget-resizing/mainwindow.cpp 0 | - | ||||||||||||
1522 | \snippet qtablewidget-resizing/mainwindow.cpp 1 | - | ||||||||||||
1523 | - | |||||||||||||
1524 | Items are created ouside the table (with no parent widget) and inserted | - | ||||||||||||
1525 | into the table with setItem(): | - | ||||||||||||
1526 | - | |||||||||||||
1527 | \snippet qtablewidget-resizing/mainwindow.cpp 2 | - | ||||||||||||
1528 | - | |||||||||||||
1529 | If you want to enable sorting in your table widget, do so after you | - | ||||||||||||
1530 | have populated it with items, otherwise sorting may interfere with | - | ||||||||||||
1531 | the insertion order (see setItem() for details). | - | ||||||||||||
1532 | - | |||||||||||||
1533 | Tables can be given both horizontal and vertical headers. The simplest way | - | ||||||||||||
1534 | to create the headers is to supply a list of strings to the | - | ||||||||||||
1535 | setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These | - | ||||||||||||
1536 | will provide simple textual headers for the table's columns and rows. | - | ||||||||||||
1537 | More sophisticated headers can be created from existing table items | - | ||||||||||||
1538 | that are usually constructed outside the table. For example, we can | - | ||||||||||||
1539 | construct a table item with an icon and aligned text, and use it as the | - | ||||||||||||
1540 | header for a particular column: | - | ||||||||||||
1541 | - | |||||||||||||
1542 | \snippet qtablewidget-using/mainwindow.cpp 2 | - | ||||||||||||
1543 | - | |||||||||||||
1544 | The number of rows in the table can be found with rowCount(), and the | - | ||||||||||||
1545 | number of columns with columnCount(). The table can be cleared with the | - | ||||||||||||
1546 | clear() function. | - | ||||||||||||
1547 | - | |||||||||||||
1548 | \table 100% | - | ||||||||||||
1549 | \row \li \inlineimage windowsvista-tableview.png Screenshot of a Windows Vista style table widget | - | ||||||||||||
1550 | \li \inlineimage macintosh-tableview.png Screenshot of a Macintosh style table widget | - | ||||||||||||
1551 | \li \inlineimage fusion-tableview.png Screenshot of a Fusion style table widget | - | ||||||||||||
1552 | \row \li A \l{Windows Vista Style Widget Gallery}{Windows Vista style} table widget. | - | ||||||||||||
1553 | \li A \l{Macintosh Style Widget Gallery}{Macintosh style} table widget. | - | ||||||||||||
1554 | \li A \l{Fusion Style Widget Gallery}{Fusion style} table widget. | - | ||||||||||||
1555 | \endtable | - | ||||||||||||
1556 | - | |||||||||||||
1557 | \sa QTableWidgetItem, QTableView, {Model/View Programming} | - | ||||||||||||
1558 | */ | - | ||||||||||||
1559 | - | |||||||||||||
1560 | /*! | - | ||||||||||||
1561 | \property QTableWidget::rowCount | - | ||||||||||||
1562 | \brief the number of rows in the table | - | ||||||||||||
1563 | - | |||||||||||||
1564 | By default, for a table constructed without row and column counts, | - | ||||||||||||
1565 | this property contains a value of 0. | - | ||||||||||||
1566 | */ | - | ||||||||||||
1567 | - | |||||||||||||
1568 | /*! | - | ||||||||||||
1569 | \property QTableWidget::columnCount | - | ||||||||||||
1570 | \brief the number of columns in the table | - | ||||||||||||
1571 | - | |||||||||||||
1572 | By default, for a table constructed without row and column counts, | - | ||||||||||||
1573 | this property contains a value of 0. | - | ||||||||||||
1574 | */ | - | ||||||||||||
1575 | - | |||||||||||||
1576 | void QTableWidgetPrivate::setup() | - | ||||||||||||
1577 | { | - | ||||||||||||
1578 | Q_Q(QTableWidget); | - | ||||||||||||
1579 | // view signals | - | ||||||||||||
1580 | QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex))); | - | ||||||||||||
1581 | QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex))); | - | ||||||||||||
1582 | QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)), | - | ||||||||||||
1583 | q, SLOT(_q_emitItemDoubleClicked(QModelIndex))); | - | ||||||||||||
1584 | QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(_q_emitItemActivated(QModelIndex))); | - | ||||||||||||
1585 | QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex))); | - | ||||||||||||
1586 | // model signals | - | ||||||||||||
1587 | QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
1588 | q, SLOT(_q_emitItemChanged(QModelIndex))); | - | ||||||||||||
1589 | // selection signals | - | ||||||||||||
1590 | QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
1591 | q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex))); | - | ||||||||||||
1592 | QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), | - | ||||||||||||
1593 | q, SIGNAL(itemSelectionChanged())); | - | ||||||||||||
1594 | // sorting | - | ||||||||||||
1595 | QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), | - | ||||||||||||
1596 | q, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); | - | ||||||||||||
1597 | QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort())); | - | ||||||||||||
1598 | } | - | ||||||||||||
1599 | - | |||||||||||||
1600 | void QTableWidgetPrivate::_q_emitItemPressed(const QModelIndex &index) | - | ||||||||||||
1601 | { | - | ||||||||||||
1602 | Q_Q(QTableWidget); | - | ||||||||||||
1603 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1604 | emit q->itemPressed(item); | - | ||||||||||||
1605 | emit q->cellPressed(index.row(), index.column()); | - | ||||||||||||
1606 | } | - | ||||||||||||
1607 | - | |||||||||||||
1608 | void QTableWidgetPrivate::_q_emitItemClicked(const QModelIndex &index) | - | ||||||||||||
1609 | { | - | ||||||||||||
1610 | Q_Q(QTableWidget); | - | ||||||||||||
1611 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1612 | emit q->itemClicked(item); | - | ||||||||||||
1613 | emit q->cellClicked(index.row(), index.column()); | - | ||||||||||||
1614 | } | - | ||||||||||||
1615 | - | |||||||||||||
1616 | void QTableWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index) | - | ||||||||||||
1617 | { | - | ||||||||||||
1618 | Q_Q(QTableWidget); | - | ||||||||||||
1619 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1620 | emit q->itemDoubleClicked(item); | - | ||||||||||||
1621 | emit q->cellDoubleClicked(index.row(), index.column()); | - | ||||||||||||
1622 | } | - | ||||||||||||
1623 | - | |||||||||||||
1624 | void QTableWidgetPrivate::_q_emitItemActivated(const QModelIndex &index) | - | ||||||||||||
1625 | { | - | ||||||||||||
1626 | Q_Q(QTableWidget); | - | ||||||||||||
1627 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1628 | emit q->itemActivated(item); | - | ||||||||||||
1629 | emit q->cellActivated(index.row(), index.column()); | - | ||||||||||||
1630 | } | - | ||||||||||||
1631 | - | |||||||||||||
1632 | void QTableWidgetPrivate::_q_emitItemEntered(const QModelIndex &index) | - | ||||||||||||
1633 | { | - | ||||||||||||
1634 | Q_Q(QTableWidget); | - | ||||||||||||
1635 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1636 | emit q->itemEntered(item); | - | ||||||||||||
1637 | emit q->cellEntered(index.row(), index.column()); | - | ||||||||||||
1638 | } | - | ||||||||||||
1639 | - | |||||||||||||
1640 | void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index) | - | ||||||||||||
1641 | { | - | ||||||||||||
1642 | Q_Q(QTableWidget); | - | ||||||||||||
1643 | if (QTableWidgetItem *item = tableModel()->item(index)) | - | ||||||||||||
1644 | emit q->itemChanged(item); | - | ||||||||||||
1645 | emit q->cellChanged(index.row(), index.column()); | - | ||||||||||||
1646 | } | - | ||||||||||||
1647 | - | |||||||||||||
1648 | void QTableWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t, | - | ||||||||||||
1649 | const QModelIndex &previous) | - | ||||||||||||
1650 | { | - | ||||||||||||
1651 | Q_Q(QTableWidget); | - | ||||||||||||
1652 | QTableWidgetItem *currentItem = tableModel()->item(current); | - | ||||||||||||
1653 | QTableWidgetItem *previousItem = tableModel()->item(previous); | - | ||||||||||||
1654 | if (currentItem || previousItem) | - | ||||||||||||
1655 | emit q->currentItemChanged(currentItem, previousItem); | - | ||||||||||||
1656 | emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column()); | - | ||||||||||||
1657 | } | - | ||||||||||||
1658 | - | |||||||||||||
1659 | void QTableWidgetPrivate::_q_sort() | - | ||||||||||||
1660 | { | - | ||||||||||||
1661 | if (sortingEnabled) { | - | ||||||||||||
1662 | int column = horizontalHeader->sortIndicatorSection(); | - | ||||||||||||
1663 | Qt::SortOrder order = horizontalHeader->sortIndicatorOrder(); | - | ||||||||||||
1664 | model->sort(column, order); | - | ||||||||||||
1665 | } | - | ||||||||||||
1666 | } | - | ||||||||||||
1667 | - | |||||||||||||
1668 | void QTableWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, | - | ||||||||||||
1669 | const QModelIndex &bottomRight) | - | ||||||||||||
1670 | { | - | ||||||||||||
1671 | if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) { | - | ||||||||||||
1672 | int column = horizontalHeader->sortIndicatorSection(); | - | ||||||||||||
1673 | if (column >= topLeft.column() && column <= bottomRight.column()) { | - | ||||||||||||
1674 | Qt::SortOrder order = horizontalHeader->sortIndicatorOrder(); | - | ||||||||||||
1675 | tableModel()->ensureSorted(column, order, topLeft.row(), bottomRight.row()); | - | ||||||||||||
1676 | } | - | ||||||||||||
1677 | } | - | ||||||||||||
1678 | } | - | ||||||||||||
1679 | - | |||||||||||||
1680 | /*! | - | ||||||||||||
1681 | \fn void QTableWidget::itemPressed(QTableWidgetItem *item) | - | ||||||||||||
1682 | - | |||||||||||||
1683 | This signal is emitted whenever an item in the table is pressed. | - | ||||||||||||
1684 | The \a item specified is the item that was pressed. | - | ||||||||||||
1685 | */ | - | ||||||||||||
1686 | - | |||||||||||||
1687 | /*! | - | ||||||||||||
1688 | \fn void QTableWidget::itemClicked(QTableWidgetItem *item) | - | ||||||||||||
1689 | - | |||||||||||||
1690 | This signal is emitted whenever an item in the table is clicked. | - | ||||||||||||
1691 | The \a item specified is the item that was clicked. | - | ||||||||||||
1692 | */ | - | ||||||||||||
1693 | - | |||||||||||||
1694 | /*! | - | ||||||||||||
1695 | \fn void QTableWidget::itemDoubleClicked(QTableWidgetItem *item) | - | ||||||||||||
1696 | - | |||||||||||||
1697 | This signal is emitted whenever an item in the table is double | - | ||||||||||||
1698 | clicked. The \a item specified is the item that was double clicked. | - | ||||||||||||
1699 | */ | - | ||||||||||||
1700 | - | |||||||||||||
1701 | /*! | - | ||||||||||||
1702 | \fn void QTableWidget::itemActivated(QTableWidgetItem *item) | - | ||||||||||||
1703 | - | |||||||||||||
1704 | This signal is emitted when the specified \a item has been activated | - | ||||||||||||
1705 | */ | - | ||||||||||||
1706 | - | |||||||||||||
1707 | /*! | - | ||||||||||||
1708 | \fn void QTableWidget::itemEntered(QTableWidgetItem *item) | - | ||||||||||||
1709 | - | |||||||||||||
1710 | This signal is emitted when the mouse cursor enters an item. The | - | ||||||||||||
1711 | \a item is the item entered. | - | ||||||||||||
1712 | - | |||||||||||||
1713 | This signal is only emitted when mouseTracking is turned on, or when a | - | ||||||||||||
1714 | mouse button is pressed while moving into an item. | - | ||||||||||||
1715 | */ | - | ||||||||||||
1716 | - | |||||||||||||
1717 | /*! | - | ||||||||||||
1718 | \fn void QTableWidget::itemChanged(QTableWidgetItem *item) | - | ||||||||||||
1719 | - | |||||||||||||
1720 | This signal is emitted whenever the data of \a item has changed. | - | ||||||||||||
1721 | */ | - | ||||||||||||
1722 | - | |||||||||||||
1723 | /*! | - | ||||||||||||
1724 | \fn void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous) | - | ||||||||||||
1725 | - | |||||||||||||
1726 | This signal is emitted whenever the current item changes. The \a | - | ||||||||||||
1727 | previous item is the item that previously had the focus, \a | - | ||||||||||||
1728 | current is the new current item. | - | ||||||||||||
1729 | */ | - | ||||||||||||
1730 | - | |||||||||||||
1731 | /*! | - | ||||||||||||
1732 | \fn void QTableWidget::itemSelectionChanged() | - | ||||||||||||
1733 | - | |||||||||||||
1734 | This signal is emitted whenever the selection changes. | - | ||||||||||||
1735 | - | |||||||||||||
1736 | \sa selectedItems(), QTableWidgetItem::isSelected() | - | ||||||||||||
1737 | */ | - | ||||||||||||
1738 | - | |||||||||||||
1739 | - | |||||||||||||
1740 | /*! | - | ||||||||||||
1741 | \since 4.1 | - | ||||||||||||
1742 | \fn void QTableWidget::cellPressed(int row, int column) | - | ||||||||||||
1743 | - | |||||||||||||
1744 | This signal is emitted whenever a cell in the table is pressed. | - | ||||||||||||
1745 | The \a row and \a column specified is the cell that was pressed. | - | ||||||||||||
1746 | */ | - | ||||||||||||
1747 | - | |||||||||||||
1748 | /*! | - | ||||||||||||
1749 | \since 4.1 | - | ||||||||||||
1750 | \fn void QTableWidget::cellClicked(int row, int column) | - | ||||||||||||
1751 | - | |||||||||||||
1752 | This signal is emitted whenever a cell in the table is clicked. | - | ||||||||||||
1753 | The \a row and \a column specified is the cell that was clicked. | - | ||||||||||||
1754 | */ | - | ||||||||||||
1755 | - | |||||||||||||
1756 | /*! | - | ||||||||||||
1757 | \since 4.1 | - | ||||||||||||
1758 | \fn void QTableWidget::cellDoubleClicked(int row, int column) | - | ||||||||||||
1759 | - | |||||||||||||
1760 | This signal is emitted whenever a cell in the table is double | - | ||||||||||||
1761 | clicked. The \a row and \a column specified is the cell that was | - | ||||||||||||
1762 | double clicked. | - | ||||||||||||
1763 | */ | - | ||||||||||||
1764 | - | |||||||||||||
1765 | /*! | - | ||||||||||||
1766 | \since 4.1 | - | ||||||||||||
1767 | \fn void QTableWidget::cellActivated(int row, int column) | - | ||||||||||||
1768 | - | |||||||||||||
1769 | This signal is emitted when the cell specified by \a row and \a column | - | ||||||||||||
1770 | has been activated | - | ||||||||||||
1771 | */ | - | ||||||||||||
1772 | - | |||||||||||||
1773 | /*! | - | ||||||||||||
1774 | \since 4.1 | - | ||||||||||||
1775 | \fn void QTableWidget::cellEntered(int row, int column) | - | ||||||||||||
1776 | - | |||||||||||||
1777 | This signal is emitted when the mouse cursor enters a cell. The | - | ||||||||||||
1778 | cell is specified by \a row and \a column. | - | ||||||||||||
1779 | - | |||||||||||||
1780 | This signal is only emitted when mouseTracking is turned on, or when a | - | ||||||||||||
1781 | mouse button is pressed while moving into an item. | - | ||||||||||||
1782 | */ | - | ||||||||||||
1783 | - | |||||||||||||
1784 | /*! | - | ||||||||||||
1785 | \since 4.1 | - | ||||||||||||
1786 | \fn void QTableWidget::cellChanged(int row, int column) | - | ||||||||||||
1787 | - | |||||||||||||
1788 | This signal is emitted whenever the data of the item in the cell | - | ||||||||||||
1789 | specified by \a row and \a column has changed. | - | ||||||||||||
1790 | */ | - | ||||||||||||
1791 | - | |||||||||||||
1792 | /*! | - | ||||||||||||
1793 | \since 4.1 | - | ||||||||||||
1794 | \fn void QTableWidget::currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) | - | ||||||||||||
1795 | - | |||||||||||||
1796 | This signal is emitted whenever the current cell changes. The cell | - | ||||||||||||
1797 | specified by \a previousRow and \a previousColumn is the cell that | - | ||||||||||||
1798 | previously had the focus, the cell specified by \a currentRow and \a | - | ||||||||||||
1799 | currentColumn is the new current cell. | - | ||||||||||||
1800 | */ | - | ||||||||||||
1801 | - | |||||||||||||
1802 | /*! | - | ||||||||||||
1803 | \since 4.3 | - | ||||||||||||
1804 | \fn void QTableWidget::removeCellWidget(int row, int column) | - | ||||||||||||
1805 | - | |||||||||||||
1806 | Removes the widget set on the cell indicated by \a row and \a column. | - | ||||||||||||
1807 | */ | - | ||||||||||||
1808 | - | |||||||||||||
1809 | /*! | - | ||||||||||||
1810 | \fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const | - | ||||||||||||
1811 | - | |||||||||||||
1812 | Returns the item at the position equivalent to QPoint(\a{ax}, \a{ay}) in | - | ||||||||||||
1813 | the table widget's coordinate system, or returns 0 if the specified point | - | ||||||||||||
1814 | is not covered by an item in the table widget. | - | ||||||||||||
1815 | - | |||||||||||||
1816 | \sa item() | - | ||||||||||||
1817 | */ | - | ||||||||||||
1818 | - | |||||||||||||
1819 | /*! | - | ||||||||||||
1820 | \enum QTableWidgetItem::ItemType | - | ||||||||||||
1821 | - | |||||||||||||
1822 | This enum describes the types that are used to describe table widget items. | - | ||||||||||||
1823 | - | |||||||||||||
1824 | \value Type The default type for table widget items. | - | ||||||||||||
1825 | \value UserType The minimum value for custom types. Values below UserType are | - | ||||||||||||
1826 | reserved by Qt. | - | ||||||||||||
1827 | - | |||||||||||||
1828 | You can define new user types in QTableWidgetItem subclasses to ensure that | - | ||||||||||||
1829 | custom items are treated specially. | - | ||||||||||||
1830 | - | |||||||||||||
1831 | \sa type() | - | ||||||||||||
1832 | */ | - | ||||||||||||
1833 | - | |||||||||||||
1834 | /*! | - | ||||||||||||
1835 | \fn int QTableWidgetItem::type() const | - | ||||||||||||
1836 | - | |||||||||||||
1837 | Returns the type passed to the QTableWidgetItem constructor. | - | ||||||||||||
1838 | */ | - | ||||||||||||
1839 | - | |||||||||||||
1840 | /*! | - | ||||||||||||
1841 | Creates a new table view with the given \a parent. | - | ||||||||||||
1842 | */ | - | ||||||||||||
1843 | QTableWidget::QTableWidget(QWidget *parent) | - | ||||||||||||
1844 | : QTableView(*new QTableWidgetPrivate, parent) | - | ||||||||||||
1845 | { | - | ||||||||||||
1846 | Q_D(QTableWidget); | - | ||||||||||||
1847 | QTableView::setModel(new QTableModel(0, 0, this)); | - | ||||||||||||
1848 | d->setup(); | - | ||||||||||||
1849 | } | - | ||||||||||||
1850 | - | |||||||||||||
1851 | /*! | - | ||||||||||||
1852 | Creates a new table view with the given \a rows and \a columns, and with the given \a parent. | - | ||||||||||||
1853 | */ | - | ||||||||||||
1854 | QTableWidget::QTableWidget(int rows, int columns, QWidget *parent) | - | ||||||||||||
1855 | : QTableView(*new QTableWidgetPrivate, parent) | - | ||||||||||||
1856 | { | - | ||||||||||||
1857 | Q_D(QTableWidget); | - | ||||||||||||
1858 | QTableView::setModel(new QTableModel(rows, columns, this)); | - | ||||||||||||
1859 | d->setup(); | - | ||||||||||||
1860 | } | - | ||||||||||||
1861 | - | |||||||||||||
1862 | /*! | - | ||||||||||||
1863 | Destroys this QTableWidget. | - | ||||||||||||
1864 | */ | - | ||||||||||||
1865 | QTableWidget::~QTableWidget() | - | ||||||||||||
1866 | { | - | ||||||||||||
1867 | } | - | ||||||||||||
1868 | - | |||||||||||||
1869 | /*! | - | ||||||||||||
1870 | Sets the number of rows in this table's model to \a rows. If | - | ||||||||||||
1871 | this is less than rowCount(), the data in the unwanted rows | - | ||||||||||||
1872 | is discarded. | - | ||||||||||||
1873 | - | |||||||||||||
1874 | \sa setColumnCount() | - | ||||||||||||
1875 | */ | - | ||||||||||||
1876 | void QTableWidget::setRowCount(int rows) | - | ||||||||||||
1877 | { | - | ||||||||||||
1878 | Q_D(QTableWidget); | - | ||||||||||||
1879 | d->tableModel()->setRowCount(rows); | - | ||||||||||||
1880 | } | - | ||||||||||||
1881 | - | |||||||||||||
1882 | /*! | - | ||||||||||||
1883 | Returns the number of rows. | - | ||||||||||||
1884 | */ | - | ||||||||||||
1885 | - | |||||||||||||
1886 | int QTableWidget::rowCount() const | - | ||||||||||||
1887 | { | - | ||||||||||||
1888 | Q_D(const QTableWidget); | - | ||||||||||||
1889 | return d->model->rowCount(); | - | ||||||||||||
1890 | } | - | ||||||||||||
1891 | - | |||||||||||||
1892 | /*! | - | ||||||||||||
1893 | Sets the number of columns in this table's model to \a columns. If | - | ||||||||||||
1894 | this is less than columnCount(), the data in the unwanted columns | - | ||||||||||||
1895 | is discarded. | - | ||||||||||||
1896 | - | |||||||||||||
1897 | \sa setRowCount() | - | ||||||||||||
1898 | */ | - | ||||||||||||
1899 | void QTableWidget::setColumnCount(int columns) | - | ||||||||||||
1900 | { | - | ||||||||||||
1901 | Q_D(QTableWidget); | - | ||||||||||||
1902 | d->tableModel()->setColumnCount(columns); | - | ||||||||||||
1903 | } | - | ||||||||||||
1904 | - | |||||||||||||
1905 | /*! | - | ||||||||||||
1906 | Returns the number of columns. | - | ||||||||||||
1907 | */ | - | ||||||||||||
1908 | - | |||||||||||||
1909 | int QTableWidget::columnCount() const | - | ||||||||||||
1910 | { | - | ||||||||||||
1911 | Q_D(const QTableWidget); | - | ||||||||||||
1912 | return d->model->columnCount(); | - | ||||||||||||
1913 | } | - | ||||||||||||
1914 | - | |||||||||||||
1915 | /*! | - | ||||||||||||
1916 | Returns the row for the \a item. | - | ||||||||||||
1917 | */ | - | ||||||||||||
1918 | int QTableWidget::row(const QTableWidgetItem *item) const | - | ||||||||||||
1919 | { | - | ||||||||||||
1920 | Q_D(const QTableWidget); | - | ||||||||||||
1921 | return d->tableModel()->index(item).row(); | - | ||||||||||||
1922 | } | - | ||||||||||||
1923 | - | |||||||||||||
1924 | /*! | - | ||||||||||||
1925 | Returns the column for the \a item. | - | ||||||||||||
1926 | */ | - | ||||||||||||
1927 | int QTableWidget::column(const QTableWidgetItem *item) const | - | ||||||||||||
1928 | { | - | ||||||||||||
1929 | Q_D(const QTableWidget); | - | ||||||||||||
1930 | return d->tableModel()->index(item).column(); | - | ||||||||||||
1931 | } | - | ||||||||||||
1932 | - | |||||||||||||
1933 | - | |||||||||||||
1934 | /*! | - | ||||||||||||
1935 | Returns the item for the given \a row and \a column if one has been set; otherwise | - | ||||||||||||
1936 | returns 0. | - | ||||||||||||
1937 | - | |||||||||||||
1938 | \sa setItem() | - | ||||||||||||
1939 | */ | - | ||||||||||||
1940 | QTableWidgetItem *QTableWidget::item(int row, int column) const | - | ||||||||||||
1941 | { | - | ||||||||||||
1942 | Q_D(const QTableWidget); | - | ||||||||||||
1943 | return d->tableModel()->item(row, column); | - | ||||||||||||
1944 | } | - | ||||||||||||
1945 | - | |||||||||||||
1946 | /*! | - | ||||||||||||
1947 | Sets the item for the given \a row and \a column to \a item. | - | ||||||||||||
1948 | - | |||||||||||||
1949 | The table takes ownership of the item. | - | ||||||||||||
1950 | - | |||||||||||||
1951 | Note that if sorting is enabled (see | - | ||||||||||||
1952 | \l{QTableView::sortingEnabled} {sortingEnabled}) and \a column is | - | ||||||||||||
1953 | the current sort column, the \a row will be moved to the sorted | - | ||||||||||||
1954 | position determined by \a item. | - | ||||||||||||
1955 | - | |||||||||||||
1956 | If you want to set several items of a particular row (say, by | - | ||||||||||||
1957 | calling setItem() in a loop), you may want to turn off sorting | - | ||||||||||||
1958 | before doing so, and turn it back on afterwards; this will allow | - | ||||||||||||
1959 | you to use the same \a row argument for all items in the same row | - | ||||||||||||
1960 | (i.e. setItem() will not move the row). | - | ||||||||||||
1961 | - | |||||||||||||
1962 | \sa item(), takeItem() | - | ||||||||||||
1963 | */ | - | ||||||||||||
1964 | void QTableWidget::setItem(int row, int column, QTableWidgetItem *item) | - | ||||||||||||
1965 | { | - | ||||||||||||
1966 | Q_D(QTableWidget); | - | ||||||||||||
1967 | if (item) {
| 0 | ||||||||||||
1968 | if (Q_UNLIKELY(item->view!= 0))) {
| 0 | ||||||||||||
1969 | qWarning("QTableWidget: cannot insert an item that is already owned by another QTableWidget"); | - | ||||||||||||
1970 | } else { never executed: end of block | 0 | ||||||||||||
1971 | item->view = this; | - | ||||||||||||
1972 | d->tableModel()->setItem(row, column, item); | - | ||||||||||||
1973 | } never executed: end of block | 0 | ||||||||||||
1974 | } else { | - | ||||||||||||
1975 | delete takeItem(row, column); | - | ||||||||||||
1976 | } never executed: end of block | 0 | ||||||||||||
1977 | } | - | ||||||||||||
1978 | - | |||||||||||||
1979 | /*! | - | ||||||||||||
1980 | Removes the item at \a row and \a column from the table without deleting it. | - | ||||||||||||
1981 | */ | - | ||||||||||||
1982 | QTableWidgetItem *QTableWidget::takeItem(int row, int column) | - | ||||||||||||
1983 | { | - | ||||||||||||
1984 | Q_D(QTableWidget); | - | ||||||||||||
1985 | QTableWidgetItem *item = d->tableModel()->takeItem(row, column); | - | ||||||||||||
1986 | if (item) | - | ||||||||||||
1987 | item->view = 0; | - | ||||||||||||
1988 | return item; | - | ||||||||||||
1989 | } | - | ||||||||||||
1990 | - | |||||||||||||
1991 | /*! | - | ||||||||||||
1992 | Returns the vertical header item for row \a row. | - | ||||||||||||
1993 | */ | - | ||||||||||||
1994 | QTableWidgetItem *QTableWidget::verticalHeaderItem(int row) const | - | ||||||||||||
1995 | { | - | ||||||||||||
1996 | Q_D(const QTableWidget); | - | ||||||||||||
1997 | return d->tableModel()->verticalHeaderItem(row); | - | ||||||||||||
1998 | } | - | ||||||||||||
1999 | - | |||||||||||||
2000 | /*! | - | ||||||||||||
2001 | Sets the vertical header item for row \a row to \a item. | - | ||||||||||||
2002 | */ | - | ||||||||||||
2003 | void QTableWidget::setVerticalHeaderItem(int row, QTableWidgetItem *item) | - | ||||||||||||
2004 | { | - | ||||||||||||
2005 | Q_D(QTableWidget); | - | ||||||||||||
2006 | if (item) { | - | ||||||||||||
2007 | item->view = this; | - | ||||||||||||
2008 | d->tableModel()->setVerticalHeaderItem(row, item); | - | ||||||||||||
2009 | } else { | - | ||||||||||||
2010 | delete takeVerticalHeaderItem(row); | - | ||||||||||||
2011 | } | - | ||||||||||||
2012 | } | - | ||||||||||||
2013 | - | |||||||||||||
2014 | /*! | - | ||||||||||||
2015 | \since 4.1 | - | ||||||||||||
2016 | Removes the vertical header item at \a row from the header without deleting it. | - | ||||||||||||
2017 | */ | - | ||||||||||||
2018 | QTableWidgetItem *QTableWidget::takeVerticalHeaderItem(int row) | - | ||||||||||||
2019 | { | - | ||||||||||||
2020 | Q_D(QTableWidget); | - | ||||||||||||
2021 | QTableWidgetItem *itm = d->tableModel()->takeVerticalHeaderItem(row); | - | ||||||||||||
2022 | if (itm) | - | ||||||||||||
2023 | itm->view = 0; | - | ||||||||||||
2024 | return itm; | - | ||||||||||||
2025 | } | - | ||||||||||||
2026 | - | |||||||||||||
2027 | /*! | - | ||||||||||||
2028 | Returns the horizontal header item for column, \a column, if one has been | - | ||||||||||||
2029 | set; otherwise returns 0. | - | ||||||||||||
2030 | */ | - | ||||||||||||
2031 | QTableWidgetItem *QTableWidget::horizontalHeaderItem(int column) const | - | ||||||||||||
2032 | { | - | ||||||||||||
2033 | Q_D(const QTableWidget); | - | ||||||||||||
2034 | return d->tableModel()->horizontalHeaderItem(column); | - | ||||||||||||
2035 | } | - | ||||||||||||
2036 | - | |||||||||||||
2037 | /*! | - | ||||||||||||
2038 | Sets the horizontal header item for column \a column to \a item. | - | ||||||||||||
2039 | If necessary, the column count is increased to fit the item. | - | ||||||||||||
2040 | The previous header item (if there was one) is deleted. | - | ||||||||||||
2041 | */ | - | ||||||||||||
2042 | void QTableWidget::setHorizontalHeaderItem(int column, QTableWidgetItem *item) | - | ||||||||||||
2043 | { | - | ||||||||||||
2044 | Q_D(QTableWidget); | - | ||||||||||||
2045 | if (item) { | - | ||||||||||||
2046 | item->view = this; | - | ||||||||||||
2047 | d->tableModel()->setHorizontalHeaderItem(column, item); | - | ||||||||||||
2048 | } else { | - | ||||||||||||
2049 | delete takeHorizontalHeaderItem(column); | - | ||||||||||||
2050 | } | - | ||||||||||||
2051 | } | - | ||||||||||||
2052 | - | |||||||||||||
2053 | /*! | - | ||||||||||||
2054 | \since 4.1 | - | ||||||||||||
2055 | Removes the horizontal header item at \a column from the header without deleting it. | - | ||||||||||||
2056 | */ | - | ||||||||||||
2057 | QTableWidgetItem *QTableWidget::takeHorizontalHeaderItem(int column) | - | ||||||||||||
2058 | { | - | ||||||||||||
2059 | Q_D(QTableWidget); | - | ||||||||||||
2060 | QTableWidgetItem *itm = d->tableModel()->takeHorizontalHeaderItem(column); | - | ||||||||||||
2061 | if (itm) | - | ||||||||||||
2062 | itm->view = 0; | - | ||||||||||||
2063 | return itm; | - | ||||||||||||
2064 | } | - | ||||||||||||
2065 | - | |||||||||||||
2066 | /*! | - | ||||||||||||
2067 | Sets the vertical header labels using \a labels. | - | ||||||||||||
2068 | */ | - | ||||||||||||
2069 | void QTableWidget::setVerticalHeaderLabels(const QStringList &labels) | - | ||||||||||||
2070 | { | - | ||||||||||||
2071 | Q_D(QTableWidget); | - | ||||||||||||
2072 | QTableModel *model = d->tableModel(); | - | ||||||||||||
2073 | QTableWidgetItem *item = 0; | - | ||||||||||||
2074 | for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) { | - | ||||||||||||
2075 | item = model->verticalHeaderItem(i); | - | ||||||||||||
2076 | if (!item) { | - | ||||||||||||
2077 | item = model->createItem(); | - | ||||||||||||
2078 | setVerticalHeaderItem(i, item); | - | ||||||||||||
2079 | } | - | ||||||||||||
2080 | item->setText(labels.at(i)); | - | ||||||||||||
2081 | } | - | ||||||||||||
2082 | } | - | ||||||||||||
2083 | - | |||||||||||||
2084 | /*! | - | ||||||||||||
2085 | Sets the horizontal header labels using \a labels. | - | ||||||||||||
2086 | */ | - | ||||||||||||
2087 | void QTableWidget::setHorizontalHeaderLabels(const QStringList &labels) | - | ||||||||||||
2088 | { | - | ||||||||||||
2089 | Q_D(QTableWidget); | - | ||||||||||||
2090 | QTableModel *model = d->tableModel(); | - | ||||||||||||
2091 | QTableWidgetItem *item = 0; | - | ||||||||||||
2092 | for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) { | - | ||||||||||||
2093 | item = model->horizontalHeaderItem(i); | - | ||||||||||||
2094 | if (!item) { | - | ||||||||||||
2095 | item = model->createItem(); | - | ||||||||||||
2096 | setHorizontalHeaderItem(i, item); | - | ||||||||||||
2097 | } | - | ||||||||||||
2098 | item->setText(labels.at(i)); | - | ||||||||||||
2099 | } | - | ||||||||||||
2100 | } | - | ||||||||||||
2101 | - | |||||||||||||
2102 | /*! | - | ||||||||||||
2103 | Returns the row of the current item. | - | ||||||||||||
2104 | - | |||||||||||||
2105 | \sa currentColumn(), setCurrentCell() | - | ||||||||||||
2106 | */ | - | ||||||||||||
2107 | int QTableWidget::currentRow() const | - | ||||||||||||
2108 | { | - | ||||||||||||
2109 | return currentIndex().row(); | - | ||||||||||||
2110 | } | - | ||||||||||||
2111 | - | |||||||||||||
2112 | /*! | - | ||||||||||||
2113 | Returns the column of the current item. | - | ||||||||||||
2114 | - | |||||||||||||
2115 | \sa currentRow(), setCurrentCell() | - | ||||||||||||
2116 | */ | - | ||||||||||||
2117 | int QTableWidget::currentColumn() const | - | ||||||||||||
2118 | { | - | ||||||||||||
2119 | return currentIndex().column(); | - | ||||||||||||
2120 | } | - | ||||||||||||
2121 | - | |||||||||||||
2122 | /*! | - | ||||||||||||
2123 | Returns the current item. | - | ||||||||||||
2124 | - | |||||||||||||
2125 | \sa setCurrentItem() | - | ||||||||||||
2126 | */ | - | ||||||||||||
2127 | QTableWidgetItem *QTableWidget::currentItem() const | - | ||||||||||||
2128 | { | - | ||||||||||||
2129 | Q_D(const QTableWidget); | - | ||||||||||||
2130 | return d->tableModel()->item(currentIndex()); | - | ||||||||||||
2131 | } | - | ||||||||||||
2132 | - | |||||||||||||
2133 | /*! | - | ||||||||||||
2134 | Sets the current item to \a item. | - | ||||||||||||
2135 | - | |||||||||||||
2136 | Unless the selection mode is \l{QAbstractItemView::}{NoSelection}, | - | ||||||||||||
2137 | the item is also selected. | - | ||||||||||||
2138 | - | |||||||||||||
2139 | \sa currentItem(), setCurrentCell() | - | ||||||||||||
2140 | */ | - | ||||||||||||
2141 | void QTableWidget::setCurrentItem(QTableWidgetItem *item) | - | ||||||||||||
2142 | { | - | ||||||||||||
2143 | Q_D(QTableWidget); | - | ||||||||||||
2144 | setCurrentIndex(d->tableModel()->index(item)); | - | ||||||||||||
2145 | } | - | ||||||||||||
2146 | - | |||||||||||||
2147 | /*! | - | ||||||||||||
2148 | \since 4.4 | - | ||||||||||||
2149 | - | |||||||||||||
2150 | Sets the current item to be \a item, using the given \a command. | - | ||||||||||||
2151 | - | |||||||||||||
2152 | \sa currentItem(), setCurrentCell() | - | ||||||||||||
2153 | */ | - | ||||||||||||
2154 | void QTableWidget::setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
2155 | { | - | ||||||||||||
2156 | Q_D(QTableWidget); | - | ||||||||||||
2157 | d->selectionModel->setCurrentIndex(d->tableModel()->index(item), command); | - | ||||||||||||
2158 | } | - | ||||||||||||
2159 | - | |||||||||||||
2160 | /*! | - | ||||||||||||
2161 | \since 4.1 | - | ||||||||||||
2162 | - | |||||||||||||
2163 | Sets the current cell to be the cell at position (\a row, \a | - | ||||||||||||
2164 | column). | - | ||||||||||||
2165 | - | |||||||||||||
2166 | Depending on the current \l{QAbstractItemView::SelectionMode}{selection mode}, | - | ||||||||||||
2167 | the cell may also be selected. | - | ||||||||||||
2168 | - | |||||||||||||
2169 | \sa setCurrentItem(), currentRow(), currentColumn() | - | ||||||||||||
2170 | */ | - | ||||||||||||
2171 | void QTableWidget::setCurrentCell(int row, int column) | - | ||||||||||||
2172 | { | - | ||||||||||||
2173 | setCurrentIndex(model()->index(row, column, QModelIndex())); | - | ||||||||||||
2174 | } | - | ||||||||||||
2175 | - | |||||||||||||
2176 | /*! | - | ||||||||||||
2177 | \since 4.4 | - | ||||||||||||
2178 | - | |||||||||||||
2179 | Sets the current cell to be the cell at position (\a row, \a | - | ||||||||||||
2180 | column), using the given \a command. | - | ||||||||||||
2181 | - | |||||||||||||
2182 | \sa setCurrentItem(), currentRow(), currentColumn() | - | ||||||||||||
2183 | */ | - | ||||||||||||
2184 | void QTableWidget::setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command) | - | ||||||||||||
2185 | { | - | ||||||||||||
2186 | Q_D(QTableWidget); | - | ||||||||||||
2187 | d->selectionModel->setCurrentIndex(model()->index(row, column, QModelIndex()), command); | - | ||||||||||||
2188 | } | - | ||||||||||||
2189 | - | |||||||||||||
2190 | /*! | - | ||||||||||||
2191 | Sorts all the rows in the table widget based on \a column and \a order. | - | ||||||||||||
2192 | */ | - | ||||||||||||
2193 | void QTableWidget::sortItems(int column, Qt::SortOrder order) | - | ||||||||||||
2194 | { | - | ||||||||||||
2195 | Q_D(QTableWidget); | - | ||||||||||||
2196 | d->model->sort(column, order); | - | ||||||||||||
2197 | horizontalHeader()->setSortIndicator(column, order); | - | ||||||||||||
2198 | } | - | ||||||||||||
2199 | - | |||||||||||||
2200 | /*! | - | ||||||||||||
2201 | \internal | - | ||||||||||||
2202 | */ | - | ||||||||||||
2203 | void QTableWidget::setSortingEnabled(bool enable) | - | ||||||||||||
2204 | { | - | ||||||||||||
2205 | QTableView::setSortingEnabled(enable); | - | ||||||||||||
2206 | } | - | ||||||||||||
2207 | - | |||||||||||||
2208 | /*! | - | ||||||||||||
2209 | \internal | - | ||||||||||||
2210 | */ | - | ||||||||||||
2211 | bool QTableWidget::isSortingEnabled() const | - | ||||||||||||
2212 | { | - | ||||||||||||
2213 | return QTableView::isSortingEnabled(); | - | ||||||||||||
2214 | } | - | ||||||||||||
2215 | - | |||||||||||||
2216 | /*! | - | ||||||||||||
2217 | Starts editing the \a item if it is editable. | - | ||||||||||||
2218 | */ | - | ||||||||||||
2219 | - | |||||||||||||
2220 | void QTableWidget::editItem(QTableWidgetItem *item) | - | ||||||||||||
2221 | { | - | ||||||||||||
2222 | Q_D(QTableWidget); | - | ||||||||||||
2223 | if (!item) | - | ||||||||||||
2224 | return; | - | ||||||||||||
2225 | edit(d->tableModel()->index(item)); | - | ||||||||||||
2226 | } | - | ||||||||||||
2227 | - | |||||||||||||
2228 | /*! | - | ||||||||||||
2229 | Opens an editor for the give \a item. The editor remains open after editing. | - | ||||||||||||
2230 | - | |||||||||||||
2231 | \sa closePersistentEditor() | - | ||||||||||||
2232 | */ | - | ||||||||||||
2233 | void QTableWidget::openPersistentEditor(QTableWidgetItem *item) | - | ||||||||||||
2234 | { | - | ||||||||||||
2235 | Q_D(QTableWidget); | - | ||||||||||||
2236 | if (!item) | - | ||||||||||||
2237 | return; | - | ||||||||||||
2238 | QModelIndex index = d->tableModel()->index(item); | - | ||||||||||||
2239 | QAbstractItemView::openPersistentEditor(index); | - | ||||||||||||
2240 | } | - | ||||||||||||
2241 | - | |||||||||||||
2242 | /*! | - | ||||||||||||
2243 | Closes the persistent editor for \a item. | - | ||||||||||||
2244 | - | |||||||||||||
2245 | \sa openPersistentEditor() | - | ||||||||||||
2246 | */ | - | ||||||||||||
2247 | void QTableWidget::closePersistentEditor(QTableWidgetItem *item) | - | ||||||||||||
2248 | { | - | ||||||||||||
2249 | Q_D(QTableWidget); | - | ||||||||||||
2250 | if (!item) | - | ||||||||||||
2251 | return; | - | ||||||||||||
2252 | QModelIndex index = d->tableModel()->index(item); | - | ||||||||||||
2253 | QAbstractItemView::closePersistentEditor(index); | - | ||||||||||||
2254 | } | - | ||||||||||||
2255 | - | |||||||||||||
2256 | /*! | - | ||||||||||||
2257 | \since 4.1 | - | ||||||||||||
2258 | - | |||||||||||||
2259 | Returns the widget displayed in the cell in the given \a row and \a column. | - | ||||||||||||
2260 | - | |||||||||||||
2261 | \note The table takes ownership of the widget. | - | ||||||||||||
2262 | - | |||||||||||||
2263 | \sa setCellWidget() | - | ||||||||||||
2264 | */ | - | ||||||||||||
2265 | QWidget *QTableWidget::cellWidget(int row, int column) const | - | ||||||||||||
2266 | { | - | ||||||||||||
2267 | QModelIndex index = model()->index(row, column, QModelIndex()); | - | ||||||||||||
2268 | return QAbstractItemView::indexWidget(index); | - | ||||||||||||
2269 | } | - | ||||||||||||
2270 | - | |||||||||||||
2271 | /*! | - | ||||||||||||
2272 | \since 4.1 | - | ||||||||||||
2273 | - | |||||||||||||
2274 | Sets the given \a widget to be displayed in the cell in the given \a row | - | ||||||||||||
2275 | and \a column, passing the ownership of the widget to the table. | - | ||||||||||||
2276 | - | |||||||||||||
2277 | If cell widget A is replaced with cell widget B, cell widget A will be | - | ||||||||||||
2278 | deleted. For example, in the code snippet below, the QLineEdit object will | - | ||||||||||||
2279 | be deleted. | - | ||||||||||||
2280 | - | |||||||||||||
2281 | \snippet code/src_gui_itemviews_qtablewidget.cpp 0 | - | ||||||||||||
2282 | - | |||||||||||||
2283 | \sa cellWidget() | - | ||||||||||||
2284 | */ | - | ||||||||||||
2285 | void QTableWidget::setCellWidget(int row, int column, QWidget *widget) | - | ||||||||||||
2286 | { | - | ||||||||||||
2287 | QModelIndex index = model()->index(row, column, QModelIndex()); | - | ||||||||||||
2288 | QAbstractItemView::setIndexWidget(index, widget); | - | ||||||||||||
2289 | } | - | ||||||||||||
2290 | - | |||||||||||||
2291 | /*! | - | ||||||||||||
2292 | Returns \c true if the \a item is selected, otherwise returns \c false. | - | ||||||||||||
2293 | - | |||||||||||||
2294 | \obsolete | - | ||||||||||||
2295 | - | |||||||||||||
2296 | This function is deprecated. Use \l{QTableWidgetItem::isSelected()} instead. | - | ||||||||||||
2297 | */ | - | ||||||||||||
2298 | - | |||||||||||||
2299 | bool QTableWidget::isItemSelected(const QTableWidgetItem *item) const | - | ||||||||||||
2300 | { | - | ||||||||||||
2301 | Q_D(const QTableWidget); | - | ||||||||||||
2302 | QModelIndex index = d->tableModel()->index(item); | - | ||||||||||||
2303 | return selectionModel()->isSelected(index); | - | ||||||||||||
2304 | } | - | ||||||||||||
2305 | - | |||||||||||||
2306 | /*! | - | ||||||||||||
2307 | Selects or deselects \a item depending on \a select. | - | ||||||||||||
2308 | - | |||||||||||||
2309 | \obsolete | - | ||||||||||||
2310 | - | |||||||||||||
2311 | This function is deprecated. Use \l{QTableWidgetItem::setSelected()} instead. | - | ||||||||||||
2312 | */ | - | ||||||||||||
2313 | void QTableWidget::setItemSelected(const QTableWidgetItem *item, bool select) | - | ||||||||||||
2314 | { | - | ||||||||||||
2315 | Q_D(QTableWidget); | - | ||||||||||||
2316 | QModelIndex index = d->tableModel()->index(item); | - | ||||||||||||
2317 | selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect); | - | ||||||||||||
2318 | } | - | ||||||||||||
2319 | - | |||||||||||||
2320 | /*! | - | ||||||||||||
2321 | Selects or deselects the \a range depending on \a select. | - | ||||||||||||
2322 | */ | - | ||||||||||||
2323 | void QTableWidget::setRangeSelected(const QTableWidgetSelectionRange &range, bool select) | - | ||||||||||||
2324 | { | - | ||||||||||||
2325 | if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) || | - | ||||||||||||
2326 | !model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex())) | - | ||||||||||||
2327 | return; | - | ||||||||||||
2328 | - | |||||||||||||
2329 | QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex()); | - | ||||||||||||
2330 | QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex()); | - | ||||||||||||
2331 | - | |||||||||||||
2332 | selectionModel()->select(QItemSelection(topLeft, bottomRight), | - | ||||||||||||
2333 | select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect); | - | ||||||||||||
2334 | } | - | ||||||||||||
2335 | - | |||||||||||||
2336 | /*! | - | ||||||||||||
2337 | Returns a list of all selected ranges. | - | ||||||||||||
2338 | - | |||||||||||||
2339 | \sa QTableWidgetSelectionRange | - | ||||||||||||
2340 | */ | - | ||||||||||||
2341 | - | |||||||||||||
2342 | QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const | - | ||||||||||||
2343 | { | - | ||||||||||||
2344 | const QList<QItemSelectionRange> ranges = selectionModel()->selection(); | - | ||||||||||||
2345 | QList<QTableWidgetSelectionRange> result; | - | ||||||||||||
2346 | const int rangesCount = ranges.count(); | - | ||||||||||||
2347 | result.reserve(rangesCount); | - | ||||||||||||
2348 | for (int i = 0; i < rangesCount; ++i) | - | ||||||||||||
2349 | result.append(QTableWidgetSelectionRange(ranges.at(i).top(), | - | ||||||||||||
2350 | ranges.at(i).left(), | - | ||||||||||||
2351 | ranges.at(i).bottom(), | - | ||||||||||||
2352 | ranges.at(i).right())); | - | ||||||||||||
2353 | return result; | - | ||||||||||||
2354 | } | - | ||||||||||||
2355 | - | |||||||||||||
2356 | /*! | - | ||||||||||||
2357 | Returns a list of all selected items. | - | ||||||||||||
2358 | - | |||||||||||||
2359 | This function returns a list of pointers to the contents of the | - | ||||||||||||
2360 | selected cells. Use the selectedIndexes() function to retrieve the | - | ||||||||||||
2361 | complete selection \e including empty cells. | - | ||||||||||||
2362 | - | |||||||||||||
2363 | \sa selectedIndexes() | - | ||||||||||||
2364 | */ | - | ||||||||||||
2365 | - | |||||||||||||
2366 | QList<QTableWidgetItem*> QTableWidget::selectedItems() const | - | ||||||||||||
2367 | { | - | ||||||||||||
2368 | Q_D(const QTableWidget); | - | ||||||||||||
2369 | const QModelIndexList indexes = selectionModel()->selectedIndexes(); | - | ||||||||||||
2370 | QList<QTableWidgetItem*> items; | - | ||||||||||||
2371 | for (int i = 0; i <const auto &index : indexes.count(); ++i) { | - | ||||||||||||
2372 | QModelIndex index = indexes.at(i);if (isIndexHidden(index))
| 0 | ||||||||||||
2373 | continue; never executed: continue; | 0 | ||||||||||||
2374 | QTableWidgetItem *item = d->tableModel()->item(index); | - | ||||||||||||
2375 | if (item)
| 0 | ||||||||||||
2376 | items.append(item); never executed: items.append(item); | 0 | ||||||||||||
2377 | } never executed: end of block | 0 | ||||||||||||
2378 | return items; never executed: return items; | 0 | ||||||||||||
2379 | } | - | ||||||||||||
2380 | - | |||||||||||||
2381 | /*! | - | ||||||||||||
2382 | Finds items that matches the \a text using the given \a flags. | - | ||||||||||||
2383 | */ | - | ||||||||||||
2384 | - | |||||||||||||
2385 | QList<QTableWidgetItem*> QTableWidget::findItems(const QString &text, Qt::MatchFlags flags) const | - | ||||||||||||
2386 | { | - | ||||||||||||
2387 | Q_D(const QTableWidget); | - | ||||||||||||
2388 | QModelIndexList indexes; | - | ||||||||||||
2389 | for (int column = 0; column < columnCount(); ++column) | - | ||||||||||||
2390 | indexes += d->model->match(model()->index(0, column, QModelIndex()), | - | ||||||||||||
2391 | Qt::DisplayRole, text, -1, flags); | - | ||||||||||||
2392 | QList<QTableWidgetItem*> items; | - | ||||||||||||
2393 | const int indexCount = indexes.size(); | - | ||||||||||||
2394 | items.reserve(indexCount); | - | ||||||||||||
2395 | for (int i = 0; i < indexCount; ++i) | - | ||||||||||||
2396 | items.append(d->tableModel()->item(indexes.at(i))); | - | ||||||||||||
2397 | return items; | - | ||||||||||||
2398 | } | - | ||||||||||||
2399 | - | |||||||||||||
2400 | /*! | - | ||||||||||||
2401 | Returns the visual row of the given \a logicalRow. | - | ||||||||||||
2402 | */ | - | ||||||||||||
2403 | - | |||||||||||||
2404 | int QTableWidget::visualRow(int logicalRow) const | - | ||||||||||||
2405 | { | - | ||||||||||||
2406 | return verticalHeader()->visualIndex(logicalRow); | - | ||||||||||||
2407 | } | - | ||||||||||||
2408 | - | |||||||||||||
2409 | /*! | - | ||||||||||||
2410 | Returns the visual column of the given \a logicalColumn. | - | ||||||||||||
2411 | */ | - | ||||||||||||
2412 | - | |||||||||||||
2413 | int QTableWidget::visualColumn(int logicalColumn) const | - | ||||||||||||
2414 | { | - | ||||||||||||
2415 | return horizontalHeader()->visualIndex(logicalColumn); | - | ||||||||||||
2416 | } | - | ||||||||||||
2417 | - | |||||||||||||
2418 | /*! | - | ||||||||||||
2419 | \fn QTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const | - | ||||||||||||
2420 | - | |||||||||||||
2421 | Returns a pointer to the item at the given \a point, or returns 0 if | - | ||||||||||||
2422 | \a point is not covered by an item in the table widget. | - | ||||||||||||
2423 | - | |||||||||||||
2424 | \sa item() | - | ||||||||||||
2425 | */ | - | ||||||||||||
2426 | - | |||||||||||||
2427 | QTableWidgetItem *QTableWidget::itemAt(const QPoint &p) const | - | ||||||||||||
2428 | { | - | ||||||||||||
2429 | Q_D(const QTableWidget); | - | ||||||||||||
2430 | return d->tableModel()->item(indexAt(p)); | - | ||||||||||||
2431 | } | - | ||||||||||||
2432 | - | |||||||||||||
2433 | /*! | - | ||||||||||||
2434 | Returns the rectangle on the viewport occupied by the item at \a item. | - | ||||||||||||
2435 | */ | - | ||||||||||||
2436 | QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const | - | ||||||||||||
2437 | { | - | ||||||||||||
2438 | Q_D(const QTableWidget); | - | ||||||||||||
2439 | if (!item) | - | ||||||||||||
2440 | return QRect(); | - | ||||||||||||
2441 | QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item)); | - | ||||||||||||
2442 | Q_ASSERT(index.isValid()); | - | ||||||||||||
2443 | return visualRect(index); | - | ||||||||||||
2444 | } | - | ||||||||||||
2445 | - | |||||||||||||
2446 | /*! | - | ||||||||||||
2447 | Scrolls the view if necessary to ensure that the \a item is visible. | - | ||||||||||||
2448 | The \a hint parameter specifies more precisely where the | - | ||||||||||||
2449 | \a item should be located after the operation. | - | ||||||||||||
2450 | */ | - | ||||||||||||
2451 | - | |||||||||||||
2452 | void QTableWidget::scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint) | - | ||||||||||||
2453 | { | - | ||||||||||||
2454 | Q_D(QTableWidget); | - | ||||||||||||
2455 | if (!item) | - | ||||||||||||
2456 | return; | - | ||||||||||||
2457 | QModelIndex index = d->tableModel()->index(const_cast<QTableWidgetItem*>(item)); | - | ||||||||||||
2458 | Q_ASSERT(index.isValid()); | - | ||||||||||||
2459 | QTableView::scrollTo(index, hint); | - | ||||||||||||
2460 | } | - | ||||||||||||
2461 | - | |||||||||||||
2462 | /*! | - | ||||||||||||
2463 | Returns the item prototype used by the table. | - | ||||||||||||
2464 | - | |||||||||||||
2465 | \sa setItemPrototype() | - | ||||||||||||
2466 | */ | - | ||||||||||||
2467 | const QTableWidgetItem *QTableWidget::itemPrototype() const | - | ||||||||||||
2468 | { | - | ||||||||||||
2469 | Q_D(const QTableWidget); | - | ||||||||||||
2470 | return d->tableModel()->itemPrototype(); | - | ||||||||||||
2471 | } | - | ||||||||||||
2472 | - | |||||||||||||
2473 | /*! | - | ||||||||||||
2474 | Sets the item prototype for the table to the specified \a item. | - | ||||||||||||
2475 | - | |||||||||||||
2476 | The table widget will use the item prototype clone function when it needs | - | ||||||||||||
2477 | to create a new table item. For example when the user is editing | - | ||||||||||||
2478 | in an empty cell. This is useful when you have a QTableWidgetItem | - | ||||||||||||
2479 | subclass and want to make sure that QTableWidget creates instances of | - | ||||||||||||
2480 | your subclass. | - | ||||||||||||
2481 | - | |||||||||||||
2482 | The table takes ownership of the prototype. | - | ||||||||||||
2483 | - | |||||||||||||
2484 | \sa itemPrototype() | - | ||||||||||||
2485 | */ | - | ||||||||||||
2486 | void QTableWidget::setItemPrototype(const QTableWidgetItem *item) | - | ||||||||||||
2487 | { | - | ||||||||||||
2488 | Q_D(QTableWidget); | - | ||||||||||||
2489 | d->tableModel()->setItemPrototype(item); | - | ||||||||||||
2490 | } | - | ||||||||||||
2491 | - | |||||||||||||
2492 | /*! | - | ||||||||||||
2493 | Inserts an empty row into the table at \a row. | - | ||||||||||||
2494 | */ | - | ||||||||||||
2495 | void QTableWidget::insertRow(int row) | - | ||||||||||||
2496 | { | - | ||||||||||||
2497 | Q_D(QTableWidget); | - | ||||||||||||
2498 | d->tableModel()->insertRows(row); | - | ||||||||||||
2499 | } | - | ||||||||||||
2500 | - | |||||||||||||
2501 | /*! | - | ||||||||||||
2502 | Inserts an empty column into the table at \a column. | - | ||||||||||||
2503 | */ | - | ||||||||||||
2504 | void QTableWidget::insertColumn(int column) | - | ||||||||||||
2505 | { | - | ||||||||||||
2506 | Q_D(QTableWidget); | - | ||||||||||||
2507 | d->tableModel()->insertColumns(column); | - | ||||||||||||
2508 | } | - | ||||||||||||
2509 | - | |||||||||||||
2510 | /*! | - | ||||||||||||
2511 | Removes the row \a row and all its items from the table. | - | ||||||||||||
2512 | */ | - | ||||||||||||
2513 | void QTableWidget::removeRow(int row) | - | ||||||||||||
2514 | { | - | ||||||||||||
2515 | Q_D(QTableWidget); | - | ||||||||||||
2516 | d->tableModel()->removeRows(row); | - | ||||||||||||
2517 | } | - | ||||||||||||
2518 | - | |||||||||||||
2519 | /*! | - | ||||||||||||
2520 | Removes the column \a column and all its items from the table. | - | ||||||||||||
2521 | */ | - | ||||||||||||
2522 | void QTableWidget::removeColumn(int column) | - | ||||||||||||
2523 | { | - | ||||||||||||
2524 | Q_D(QTableWidget); | - | ||||||||||||
2525 | d->tableModel()->removeColumns(column); | - | ||||||||||||
2526 | } | - | ||||||||||||
2527 | - | |||||||||||||
2528 | /*! | - | ||||||||||||
2529 | Removes all items in the view. | - | ||||||||||||
2530 | This will also remove all selections and headers. | - | ||||||||||||
2531 | If you don't want to remove the headers, use | - | ||||||||||||
2532 | QTableWidget::clearContents(). | - | ||||||||||||
2533 | The table dimensions stay the same. | - | ||||||||||||
2534 | */ | - | ||||||||||||
2535 | - | |||||||||||||
2536 | void QTableWidget::clear() | - | ||||||||||||
2537 | { | - | ||||||||||||
2538 | Q_D(QTableWidget); | - | ||||||||||||
2539 | selectionModel()->clear(); | - | ||||||||||||
2540 | d->tableModel()->clear(); | - | ||||||||||||
2541 | } | - | ||||||||||||
2542 | - | |||||||||||||
2543 | /*! | - | ||||||||||||
2544 | \since 4.2 | - | ||||||||||||
2545 | - | |||||||||||||
2546 | Removes all items not in the headers from the view. | - | ||||||||||||
2547 | This will also remove all selections. | - | ||||||||||||
2548 | The table dimensions stay the same. | - | ||||||||||||
2549 | */ | - | ||||||||||||
2550 | void QTableWidget::clearContents() | - | ||||||||||||
2551 | { | - | ||||||||||||
2552 | Q_D(QTableWidget); | - | ||||||||||||
2553 | selectionModel()->clear(); | - | ||||||||||||
2554 | d->tableModel()->clearContents(); | - | ||||||||||||
2555 | } | - | ||||||||||||
2556 | - | |||||||||||||
2557 | /*! | - | ||||||||||||
2558 | Returns a list of MIME types that can be used to describe a list of | - | ||||||||||||
2559 | tablewidget items. | - | ||||||||||||
2560 | - | |||||||||||||
2561 | \sa mimeData() | - | ||||||||||||
2562 | */ | - | ||||||||||||
2563 | QStringList QTableWidget::mimeTypes() const | - | ||||||||||||
2564 | { | - | ||||||||||||
2565 | return d_func()->tableModel()->QAbstractTableModel::mimeTypes(); | - | ||||||||||||
2566 | } | - | ||||||||||||
2567 | - | |||||||||||||
2568 | /*! | - | ||||||||||||
2569 | Returns an object that contains a serialized description of the specified | - | ||||||||||||
2570 | \a items. The format used to describe the items is obtained from the | - | ||||||||||||
2571 | mimeTypes() function. | - | ||||||||||||
2572 | - | |||||||||||||
2573 | If the list of items is empty, 0 is returned rather than a serialized | - | ||||||||||||
2574 | empty list. | - | ||||||||||||
2575 | */ | - | ||||||||||||
2576 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) | - | ||||||||||||
2577 | QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem *> &items) const | - | ||||||||||||
2578 | #else | - | ||||||||||||
2579 | QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*> items) const | - | ||||||||||||
2580 | #endif | - | ||||||||||||
2581 | { | - | ||||||||||||
2582 | Q_D(const QTableWidget); | - | ||||||||||||
2583 | - | |||||||||||||
2584 | QModelIndexList &cachedIndexes = d->tableModel()->cachedIndexes; | - | ||||||||||||
2585 | - | |||||||||||||
2586 | // if non empty, it's called from the model's own mimeData | - | ||||||||||||
2587 | if (cachedIndexes.isEmpty()) { | - | ||||||||||||
2588 | cachedIndexes.reserve(items.count()); | - | ||||||||||||
2589 | foreach (QTableWidgetItem *item, items) | - | ||||||||||||
2590 | cachedIndexes << indexFromItem(item); | - | ||||||||||||
2591 | - | |||||||||||||
2592 | QMimeData *result = d->tableModel()->internalMimeData(); | - | ||||||||||||
2593 | - | |||||||||||||
2594 | cachedIndexes.clear(); | - | ||||||||||||
2595 | return result; | - | ||||||||||||
2596 | } | - | ||||||||||||
2597 | - | |||||||||||||
2598 | return d->tableModel()->internalMimeData(); | - | ||||||||||||
2599 | } | - | ||||||||||||
2600 | - | |||||||||||||
2601 | /*! | - | ||||||||||||
2602 | Handles the \a data supplied by a drag and drop operation that ended with | - | ||||||||||||
2603 | the given \a action in the given \a row and \a column. | - | ||||||||||||
2604 | Returns \c true if the data and action can be handled by the model; | - | ||||||||||||
2605 | otherwise returns \c false. | - | ||||||||||||
2606 | - | |||||||||||||
2607 | \sa supportedDropActions() | - | ||||||||||||
2608 | */ | - | ||||||||||||
2609 | bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action) | - | ||||||||||||
2610 | { | - | ||||||||||||
2611 | QModelIndex idx; | - | ||||||||||||
2612 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
2613 | if (dropIndicatorPosition() == QAbstractItemView::OnItem) { | - | ||||||||||||
2614 | // QAbstractTableModel::dropMimeData will overwrite on the index if row == -1 and column == -1 | - | ||||||||||||
2615 | idx = model()->index(row, column); | - | ||||||||||||
2616 | row = -1; | - | ||||||||||||
2617 | column = -1; | - | ||||||||||||
2618 | } | - | ||||||||||||
2619 | #endif | - | ||||||||||||
2620 | return d_func()->tableModel()->QAbstractTableModel::dropMimeData(data, action , row, column, idx); | - | ||||||||||||
2621 | } | - | ||||||||||||
2622 | - | |||||||||||||
2623 | /*! | - | ||||||||||||
2624 | Returns the drop actions supported by this view. | - | ||||||||||||
2625 | - | |||||||||||||
2626 | \sa Qt::DropActions | - | ||||||||||||
2627 | */ | - | ||||||||||||
2628 | Qt::DropActions QTableWidget::supportedDropActions() const | - | ||||||||||||
2629 | { | - | ||||||||||||
2630 | return d_func()->tableModel()->QAbstractTableModel::supportedDropActions() | Qt::MoveAction; | - | ||||||||||||
2631 | } | - | ||||||||||||
2632 | - | |||||||||||||
2633 | /*! | - | ||||||||||||
2634 | Returns a list of pointers to the items contained in the \a data object. | - | ||||||||||||
2635 | If the object was not created by a QTreeWidget in the same process, the list | - | ||||||||||||
2636 | is empty. | - | ||||||||||||
2637 | - | |||||||||||||
2638 | */ | - | ||||||||||||
2639 | QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const | - | ||||||||||||
2640 | { | - | ||||||||||||
2641 | const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data); | - | ||||||||||||
2642 | if (twd) | - | ||||||||||||
2643 | return twd->items; | - | ||||||||||||
2644 | return QList<QTableWidgetItem*>(); | - | ||||||||||||
2645 | } | - | ||||||||||||
2646 | - | |||||||||||||
2647 | /*! | - | ||||||||||||
2648 | Returns the QModelIndex associated with the given \a item. | - | ||||||||||||
2649 | */ | - | ||||||||||||
2650 | - | |||||||||||||
2651 | QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const | - | ||||||||||||
2652 | { | - | ||||||||||||
2653 | Q_D(const QTableWidget); | - | ||||||||||||
2654 | return d->tableModel()->index(item); | - | ||||||||||||
2655 | } | - | ||||||||||||
2656 | - | |||||||||||||
2657 | /*! | - | ||||||||||||
2658 | Returns a pointer to the QTableWidgetItem associated with the given \a index. | - | ||||||||||||
2659 | */ | - | ||||||||||||
2660 | - | |||||||||||||
2661 | QTableWidgetItem *QTableWidget::itemFromIndex(const QModelIndex &index) const | - | ||||||||||||
2662 | { | - | ||||||||||||
2663 | Q_D(const QTableWidget); | - | ||||||||||||
2664 | return d->tableModel()->item(index); | - | ||||||||||||
2665 | } | - | ||||||||||||
2666 | - | |||||||||||||
2667 | /*! | - | ||||||||||||
2668 | \internal | - | ||||||||||||
2669 | */ | - | ||||||||||||
2670 | void QTableWidget::setModel(QAbstractItemModel * /*model*/) | - | ||||||||||||
2671 | { | - | ||||||||||||
2672 | Q_ASSERT(!"QTableWidget::setModel() - Changing the model of the QTableWidget is not allowed."); | - | ||||||||||||
2673 | } | - | ||||||||||||
2674 | - | |||||||||||||
2675 | /*! \reimp */ | - | ||||||||||||
2676 | bool QTableWidget::event(QEvent *e) | - | ||||||||||||
2677 | { | - | ||||||||||||
2678 | return QTableView::event(e); | - | ||||||||||||
2679 | } | - | ||||||||||||
2680 | - | |||||||||||||
2681 | #ifndef QT_NO_DRAGANDDROP | - | ||||||||||||
2682 | /*! \reimp */ | - | ||||||||||||
2683 | void QTableWidget::dropEvent(QDropEvent *event) { | - | ||||||||||||
2684 | Q_D(QTableWidget); | - | ||||||||||||
2685 | if (event->source() == this && (event->dropAction() == Qt::MoveAction ||
| 0 | ||||||||||||
2686 | dragDropMode() == QAbstractItemView::InternalMove)) {
| 0 | ||||||||||||
2687 | QModelIndex topIndex; | - | ||||||||||||
2688 | int col = -1; | - | ||||||||||||
2689 | int row = -1; | - | ||||||||||||
2690 | if (d->dropOn(event, &row, &col, &topIndex)) {
| 0 | ||||||||||||
2691 | const QModelIndexList indexes = selectedIndexes(); | - | ||||||||||||
2692 | int top = INT_MAX; | - | ||||||||||||
2693 | int left = INT_MAX; | - | ||||||||||||
2694 | for (int i = 0; i <const auto &index : indexes.count(); ++i) { | - | ||||||||||||
2695 | top = qMin(indexesindex.at(i).row(), top); | - | ||||||||||||
2696 | left = qMin(indexesindex.at(i).column(), left); | - | ||||||||||||
2697 | } never executed: end of block | 0 | ||||||||||||
2698 | - | |||||||||||||
2699 | QList<QTableWidgetItem *> taken; | - | ||||||||||||
2700 | const int indexesCount = indexes.count(); | - | ||||||||||||
2701 | taken.reserve(indexesCount); | - | ||||||||||||
2702 | for (int i = 0; i < indexesCount; ++iconst auto &index : indexes) | - | ||||||||||||
2703 | taken.append(takeItem(indexesindex.at(i).row(), indexesindex.at(i).column())); never executed: taken.append(takeItem(index.row(), index.column())); | 0 | ||||||||||||
2704 | - | |||||||||||||
2705 | for (int i = 0; i <const auto &index : indexes.count(); ++i) {QModelIndex index = indexes.at(i); | - | ||||||||||||
2706 | int r = index.row() - top + topIndex.row(); | - | ||||||||||||
2707 | int c = index.column() - left + topIndex.column(); | - | ||||||||||||
2708 | setItem(r, c, taken.takeFirst()); | - | ||||||||||||
2709 | } never executed: end of block | 0 | ||||||||||||
2710 | - | |||||||||||||
2711 | event->accept(); | - | ||||||||||||
2712 | // Don't want QAbstractItemView to delete it because it was "moved" we already did it | - | ||||||||||||
2713 | event->setDropAction(Qt::CopyAction); | - | ||||||||||||
2714 | } never executed: end of block | 0 | ||||||||||||
2715 | } never executed: end of block | 0 | ||||||||||||
2716 | - | |||||||||||||
2717 | QTableView::dropEvent(event); | - | ||||||||||||
2718 | } never executed: end of block | 0 | ||||||||||||
2719 | #endif | - | ||||||||||||
2720 | - | |||||||||||||
2721 | QT_END_NAMESPACE | - | ||||||||||||
2722 | - | |||||||||||||
2723 | #include "moc_qtablewidget.cpp" | - | ||||||||||||
2724 | #include "moc_qtablewidget_p.cpp" | - | ||||||||||||
2725 | - | |||||||||||||
2726 | #endif // QT_NO_TABLEWIDGET | - | ||||||||||||
Source code | Switch to Preprocessed file |