1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the item views module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qtablewidget.h"
30
#include <qheaderview.h>
31
#include <qitemdelegate.h>
33
#include <qabstractitemmodel.h>
34
#include <private/qabstractitemmodel_p.h>
35
#include <private/qtableview_p.h>
36
#include <private/qwidgetitemdata_p.h>
38
// workaround for VC++ 6.0 linker bug (?)
39
typedef bool(*LessThan)(const QTableWidgetItem *left, const QTableWidgetItem *right);
41
class QTableWidgetMimeData : public QMimeData
45
QList<QTableWidgetItem*> items;
48
class QTableModel : public QAbstractTableModel
52
QTableModel(int rows, int columns, QTableWidget *parent);
55
bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex());
56
bool insertColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex());
58
bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex());
59
bool removeColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex());
61
void setItem(int row, int column, QTableWidgetItem *item);
62
QTableWidgetItem *takeItem(int row, int column);
63
QTableWidgetItem *item(int row, int column) const;
64
QTableWidgetItem *item(const QModelIndex &index) const;
65
void removeItem(QTableWidgetItem *item);
67
void setHorizontalHeaderItem(int section, QTableWidgetItem *item);
68
void setVerticalHeaderItem(int section, QTableWidgetItem *item);
69
QTableWidgetItem *horizontalHeaderItem(int section);
70
QTableWidgetItem *verticalHeaderItem(int section);
72
QModelIndex index(const QTableWidgetItem *item) const;
73
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
75
void setRowCount(int rows);
76
void setColumnCount(int columns);
78
int rowCount(const QModelIndex &parent = QModelIndex()) const;
79
int columnCount(const QModelIndex &parent = QModelIndex()) const;
81
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
82
bool setData(const QModelIndex &index, const QVariant &value, int role);
84
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
85
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role);
87
Qt::ItemFlags flags(const QModelIndex &index) const;
89
void sort(int column, Qt::SortOrder order);
90
static bool itemLessThan(const QTableWidgetItem *left, const QTableWidgetItem *right);
91
static bool itemGreaterThan(const QTableWidgetItem *left, const QTableWidgetItem *right);
93
bool isValid(const QModelIndex &index) const;
94
inline long tableIndex(int row, int column) const
95
{ return (row * horizontal.count()) + column; }
98
void itemChanged(QTableWidgetItem *item);
100
inline QTableWidgetItem *createItem() const
101
{ return prototype ? prototype->clone() : new QTableWidgetItem(); }
102
inline const QTableWidgetItem *itemPrototype() const
103
{ return prototype; }
104
inline void setItemPrototype(const QTableWidgetItem *item)
105
{ prototype = item; }
108
QStringList mimeTypes() const;
109
QMimeData *mimeData(const QModelIndexList &indexes) const;
110
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
111
int row, int column, const QModelIndex &parent);
112
Qt::DropActions supportedDropActions() const;
114
QMimeData *internalMimeData() const;
116
const QTableWidgetItem *prototype;
117
QVector<QTableWidgetItem*> table;
118
QVector<QTableWidgetItem*> vertical;
119
QVector<QTableWidgetItem*> horizontal;
120
mutable QChar strbuf[65];
122
// A cache must be mutable if get-functions should have const modifiers
123
mutable QModelIndexList cachedIndexes;
127
#include "qtablewidget.moc"
129
QTableModel::QTableModel(int rows, int columns, QTableWidget *parent)
130
: QAbstractTableModel(parent),
132
table(rows * columns), vertical(rows), horizontal(columns)
135
QTableModel::~QTableModel()
140
bool QTableModel::insertRows(int row, int count, const QModelIndex &)
144
else if (row > vertical.count())
145
row = vertical.count();
146
beginInsertRows(QModelIndex(), row, row + count - 1);
147
int rc = vertical.count();
148
int cc = horizontal.count();
149
vertical.insert(row, count, 0);
151
table.resize(cc * count);
153
table.insert(tableIndex(row, 0), cc * count, 0);
158
bool QTableModel::insertColumns(int column, int count, const QModelIndex &)
162
else if (column > horizontal.count())
163
column = horizontal.count();
164
beginInsertColumns(QModelIndex(), column, column + count - 1);
165
int rc = vertical.count();
166
int cc = horizontal.count();
167
horizontal.insert(column, count, 0);
169
table.resize(rc * count);
171
for (int row = 0; row < rc; ++row)
172
table.insert(tableIndex(row, column), count, 0);
177
bool QTableModel::removeRows(int row, int count, const QModelIndex &)
179
if (row >= 0 && row < vertical.count()) {
180
beginRemoveRows(QModelIndex(), row, row + count - 1);
181
int i = tableIndex(row, 0);
182
int n = count * columnCount();
183
QTableWidgetItem *oldItem = 0;
184
for (int j=i; j<n+i; ++j) {
185
oldItem = table.at(j);
190
table.remove(qMax(i, 0), n);
191
for (int v=row; v<row+count; ++v) {
192
oldItem = vertical.at(v);
197
vertical.remove(row, count);
204
bool QTableModel::removeColumns(int column, int count, const QModelIndex &)
206
if (column >= 0 && column < horizontal.count()) {
207
beginRemoveColumns(QModelIndex(), column, column + count - 1);
208
QTableWidgetItem *oldItem = 0;
209
for (int row = rowCount() - 1; row >= 0; --row) {
210
int i = tableIndex(row, column);
211
for (int j=i; j<i+count; ++j) {
212
oldItem = table.at(j);
217
table.remove(i, count);
219
for (int h=column; h<column+count; ++h) {
220
oldItem = horizontal.at(h);
225
horizontal.remove(column, count);
232
void QTableModel::setItem(int row, int column, QTableWidgetItem *item)
234
int i = tableIndex(row, column);
235
if (i < 0 || i >= table.count())
237
QTableWidgetItem *oldItem = table.at(i);
250
QModelIndex idx = index(row, column);
251
emit dataChanged(idx, idx);
254
QTableWidgetItem *QTableModel::takeItem(int row, int column)
256
long i = tableIndex(row, column);
257
QTableWidgetItem *itm = table.value(i);
265
QTableWidgetItem *QTableModel::item(int row, int column) const
267
return table.value(tableIndex(row, column));
270
QTableWidgetItem *QTableModel::item(const QModelIndex &index) const
274
return table.at(tableIndex(index.row(), index.column()));
277
void QTableModel::removeItem(QTableWidgetItem *item)
279
int i = table.indexOf(item);
282
QModelIndex idx = index(item);
283
emit dataChanged(idx, idx);
287
i = vertical.indexOf(item);
291
emit headerDataChanged(Qt::Vertical, i, i);
294
i = horizontal.indexOf(item);
297
emit headerDataChanged(Qt::Horizontal, i, i);
302
void QTableModel::setHorizontalHeaderItem(int section, QTableWidgetItem *item)
304
if (section < 0 || section >= horizontal.count())
306
QTableWidgetItem *oldItem = horizontal.at(section);
316
horizontal[section] = item;
317
emit headerDataChanged(Qt::Horizontal, section, section);
320
void QTableModel::setVerticalHeaderItem(int section, QTableWidgetItem *item)
322
if (section < 0 || section >= vertical.count())
324
QTableWidgetItem *oldItem = vertical.at(section);
334
vertical[section] = item;
335
emit headerDataChanged(Qt::Vertical, section, section);
338
QTableWidgetItem *QTableModel::horizontalHeaderItem(int section)
340
return horizontal.value(section);
343
QTableWidgetItem *QTableModel::verticalHeaderItem(int section)
345
return vertical.value(section);
348
QModelIndex QTableModel::index(const QTableWidgetItem *item) const
350
int i = table.indexOf(const_cast<QTableWidgetItem*>(item));
351
int row = i / columnCount();
352
int col = i % columnCount();
353
return index(row, col);
356
QModelIndex QTableModel::index(int row, int column, const QModelIndex &parent) const
358
if (hasIndex(row, column, parent)) {
359
QTableWidgetItem *item = table.at(tableIndex(row, column));
360
return createIndex(row, column, item);
362
return QModelIndex();
365
void QTableModel::setRowCount(int rows)
367
int rc = vertical.count();
371
insertRows(qMax(rc, 0), rows - rc);
373
removeRows(qMax(rows, 0), rc - rows);
376
void QTableModel::setColumnCount(int columns)
378
int cc = horizontal.count();
382
insertColumns(qMax(cc, 0), columns - cc);
384
removeColumns(qMax(columns, 0), cc - columns);
387
int QTableModel::rowCount(const QModelIndex &) const
389
return vertical.count();
392
int QTableModel::columnCount(const QModelIndex &) const
394
return horizontal.count();
397
QVariant QTableModel::data(const QModelIndex &index, int role) const
399
QTableWidgetItem *itm = item(index);
401
return itm->data(role);
405
bool QTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
407
QTableWidgetItem *itm = item(index);
410
itm->setData(role, value);
414
QTableWidget *view = qobject_cast<QTableWidget*>(QObject::parent());
419
itm->setData(role, value);
420
view->setItem(index.row(), index.column(), itm);
424
Qt::ItemFlags QTableModel::flags(const QModelIndex &index) const
426
QTableWidgetItem *itm = item(index);
429
return Qt::ItemIsEditable
430
|Qt::ItemIsSelectable
431
|Qt::ItemIsUserCheckable
435
void QTableModel::sort(int column, Qt::SortOrder order)
437
QVector<QTableWidgetItem*> sorting(rowCount());
438
for (int i = 0; i < sorting.count(); ++i)
439
sorting[i] = item(i, column);
440
LessThan compare = order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan;
441
qSort(sorting.begin(), sorting.end(), compare);
442
for (int j = 0; j < sorting.count(); ++j)
443
table[tableIndex(j, column)] = sorting.at(j);
446
bool QTableModel::itemLessThan(const QTableWidgetItem *left, const QTableWidgetItem *right)
448
return *left < *right;
451
bool QTableModel::itemGreaterThan(const QTableWidgetItem *left, const QTableWidgetItem *right)
453
return !(*left < *right);
456
QVariant QTableModel::headerData(int section, Qt::Orientation orientation, int role) const
458
QTableWidgetItem *itm = 0;
461
if (orientation == Qt::Horizontal && section < horizontal.count())
462
itm = horizontal.at(section);
463
else if (section < vertical.count())
464
itm = vertical.at(section);
466
return itm->data(role);
467
if (role == Qt::DisplayRole)
468
return QVariant(section);
469
if (role == Qt::TextAlignmentRole)
470
return Qt::AlignVCenter;
474
bool QTableModel::setHeaderData(int section, Qt::Orientation orientation,
475
const QVariant &value, int role)
477
QTableWidgetItem *itm = 0;
478
if (orientation == Qt::Horizontal)
479
itm = horizontal.at(section);
481
itm = vertical.at(section);
483
itm->setData(role, value);
489
bool QTableModel::isValid(const QModelIndex &index) const
491
return index.isValid() && index.row() < vertical.count() && index.column() < horizontal.count();
494
void QTableModel::clear()
496
for (int i = 0; i < table.count(); ++i) {
498
table.at(i)->model = 0;
503
for (int j = 0; j < vertical.count(); ++j) {
504
if (vertical.at(j)) {
505
vertical.at(j)->model = 0;
506
delete vertical.at(j);
510
for (int k = 0; k < horizontal.count(); ++k) {
511
if (horizontal.at(k)) {
512
horizontal.at(k)->model = 0;
513
delete horizontal.at(k);
520
void QTableModel::itemChanged(QTableWidgetItem *item)
522
QModelIndex idx = index(item);
523
emit dataChanged(idx, idx);
526
QStringList QTableModel::mimeTypes() const
528
const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
529
return view->mimeTypes();
532
QMimeData *QTableModel::internalMimeData() const
534
return QAbstractItemModel::mimeData(cachedIndexes);
537
QMimeData *QTableModel::mimeData(const QModelIndexList &indexes) const
539
QList<QTableWidgetItem*> items;
540
for (int i = 0; i < indexes.count(); ++i)
541
items << item(indexes.at(i));
542
const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
544
// cachedIndexes is a little hack to avoid copying from QModelIndexList to QList<QTreeWidgetItem*> and back again in the view
545
cachedIndexes = indexes;
546
QMimeData *mimeData = view->mimeData(items);
547
cachedIndexes.clear();
551
bool QTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
552
int, int, const QModelIndex &index)
554
QTableWidget *view = ::qobject_cast<QTableWidget*>(QObject::parent());
555
return view->dropMimeData(index.row(), index.column(), data, action);
558
Qt::DropActions QTableModel::supportedDropActions() const
560
const QTableWidget *view = ::qobject_cast<const QTableWidget*>(QObject::parent());
561
return view->supportedDropActions();
565
\class QTableWidgetSelectionRange
567
\brief The QTableWidgetSelectionRange class provides a container for
568
storing a selection range in a QTableWidget.
572
The QTableWidgetSelectionRange class stores the top left and bottom
573
right rows and columns of a selection range in a table. The
574
selections in the table may consist of several selection ranges.
580
Constructs the table selection range.
582
QTableWidgetSelectionRange::QTableWidgetSelectionRange()
583
: top(-1), left(-1), bottom(-1), right(-1)
588
Constructs the table selection range from the given \a top, \a left, \a bottom and \a right table rows and columns.
590
QTableWidgetSelectionRange::QTableWidgetSelectionRange(int top, int left, int bottom, int right)
591
: top(top), left(left), bottom(bottom), right(right)
596
Constructs a the table selection range by copying the given \a other table selection range.
598
QTableWidgetSelectionRange::QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other)
599
: top(other.top), left(other.left), bottom(other.bottom), right(other.right)
604
Destroys the table selection range.
606
QTableWidgetSelectionRange::~QTableWidgetSelectionRange()
611
\fn inline int QTableWidgetSelectionRange::topRow() const
613
Returns the top row of the range.
617
\fn inline int QTableWidgetSelectionRange::bottomRow() const
619
Returns the bottom row of the range.
623
\fn inline int QTableWidgetSelectionRange::leftColumn() const
625
Returns the left column of the range.
629
\fn inline int QTableWidgetSelectionRange::rightColumn() const
631
Returns the right column of the range.
636
\class QTableWidgetItem
637
\brief The QTableWidgetItem class provides an item for use with the
642
Table items are used to hold pieces of information for table widgets.
643
Items usually contain text, icons, or checkboxes
645
The QTableWidgetItem class is a convenience class that replaces the
646
\c QTableItem class in Qt 3. It provides an item for use with
647
the QTableWidget class.
649
Items are usually constructed with a table widget as their parent then
650
inserted at a particular position specified by row and column numbers:
652
\quotefile snippets/qtablewidget-using/mainwindow.cpp
653
\skipto QTableWidgetItem *newItem
654
\printuntil tableWidget->setItem(
656
Each item can have its own background color which is set with
657
the setBackgroundColor() function. The current background color can be
658
found with backgroundColor().
659
The text label for each item can be rendered with its own font and text
660
color. These are specified with the setFont() and setTextColor() functions,
661
and read with font() and textColor().
663
Items can be made checkable by setting the appropriate flag value with the
664
setFlags() function. The current state of the item's flags can be read
671
\fn Qt::CheckState QTableWidgetItem::checkState() const
673
Returns the checked state of the list item (see \l{Qt::CheckState}).
679
\fn void QTableWidgetItem::setCheckState(Qt::CheckState state)
681
Sets the check state of the table item to be \a state.
685
\fn QTableWidget *QTableWidgetItem::tableWidget() const
687
Returns the table widget that contains the item.
691
\fn Qt::ItemFlags QTableWidgetItem::flags() const
693
Returns the flags used to describe the item. These determine whether
694
the item can be checked, edited, and selected.
700
\fn void QTableWidgetItem::setFlags(Qt::ItemFlags flags)
702
Sets the flags for the item to the given \a flags. These determine whether
703
the item can be selected or modified.
709
\fn QString QTableWidgetItem::text() const
711
Returns the item's text.
717
\fn void QTableWidgetItem::setText(const QString &text)
719
Sets the item's text to the \a text specified.
721
\sa text() setFont() setTextColor()
725
\fn QIcon QTableWidgetItem::icon() const
727
Returns the item's icon.
733
\fn void QTableWidgetItem::setIcon(const QIcon &icon)
735
Sets the item's icon to the \a icon specified.
741
\fn QString QTableWidgetItem::statusTip() const
743
Returns the item's status tip.
749
\fn void QTableWidgetItem::setStatusTip(const QString &statusTip)
751
Sets the item's status tip to the string specified by \a statusTip.
753
\sa statusTip() setToolTip() setWhatsThis()
757
\fn QString QTableWidgetItem::toolTip() const
759
Returns the item's tooltip.
765
\fn void QTableWidgetItem::setToolTip(const QString &toolTip)
767
Sets the item's tooltip to the string specified by \a toolTip.
769
\sa toolTip() setStatusTip() setWhatsThis()
773
\fn QString QTableWidgetItem::whatsThis() const
775
Returns the item's "What's This?" help.
781
\fn void QTableWidgetItem::setWhatsThis(const QString &whatsThis)
783
Sets the item's "What's This?" help to the string specified by \a whatsThis.
785
\sa whatsThis() setStatusTip() setToolTip()
789
\fn QFont QTableWidgetItem::font() const
791
Returns the font used to render the item's text.
797
\fn void QTableWidgetItem::setFont(const QFont &font)
799
Sets the font used to display the item's text to the given \a font.
801
\sa font() setText() setTextColor()
805
\fn QColor QTableWidgetItem::backgroundColor() const
807
Returns the color used to render the item's background.
809
\sa textColor() setBackgroundColor()
813
\fn void QTableWidgetItem::setBackgroundColor(const QColor &color)
815
Sets the item's background color to the specified \a color.
817
\sa backgroundColor() setTextColor()
821
\fn QColor QTableWidgetItem::textColor() const
823
Returns the color used to render the item's text.
825
\sa backgroundColor() setTextColor()
829
\fn void QTableWidgetItem::setTextColor(const QColor &color)
831
Sets the color used to display the item's text to the given \a color.
833
\sa textColor() setFont() setText()
837
\fn int QTableWidgetItem::textAlignment() const
839
Returns the text alignment for the item's text (see \l{Qt::AlignmentFlag}).
843
\fn void QTableWidgetItem::setTextAlignment(int alignment)
845
Sets the text alignment for the item's text to the \a alignment
846
specified (see \l{Qt::AlignmentFlag}).
850
Constructs a table item of the specified \a type that does not belong
855
QTableWidgetItem::QTableWidgetItem(int type)
856
: rtti(type), view(0), model(0),
857
itemFlags(Qt::ItemIsEditable
858
|Qt::ItemIsSelectable
859
|Qt::ItemIsUserCheckable
861
|Qt::ItemIsDragEnabled
862
|Qt::ItemIsDropEnabled)
867
Constructs a table item with the given \a text.
871
QTableWidgetItem::QTableWidgetItem(const QString &text, int type)
872
: rtti(type), view(0), model(0),
873
itemFlags(Qt::ItemIsEditable
874
|Qt::ItemIsSelectable
875
|Qt::ItemIsUserCheckable
877
|Qt::ItemIsDragEnabled
878
|Qt::ItemIsDropEnabled)
880
setData(Qt::DisplayRole, text);
884
Destroys the table item.
886
QTableWidgetItem::~QTableWidgetItem()
889
model->removeItem(this);
893
Creates an exact copy of the item.
895
QTableWidgetItem *QTableWidgetItem::clone() const
897
QTableWidgetItem *item = new QTableWidgetItem();
903
Sets the item's data for the given \a role to the specified \a value.
905
void QTableWidgetItem::setData(int role, const QVariant &value)
907
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
908
for (int i = 0; i < values.count(); ++i) {
909
if (values.at(i).role == role) {
910
values[i].value = value;
914
values.append(QWidgetItemData(role, value));
916
model->itemChanged(this);
920
Returns the item's data for the given \a role.
922
QVariant QTableWidgetItem::data(int role) const
924
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
925
for (int i = 0; i < values.count(); ++i)
926
if (values.at(i).role == role)
927
return values.at(i).value;
932
Returns true if the item is less than the \a other item; otherwise returns
935
bool QTableWidgetItem::operator<(const QTableWidgetItem &other) const
937
return text() < other.text();
940
#ifndef QT_NO_DATASTREAM
943
Reads the item from stream \a in.
947
void QTableWidgetItem::read(QDataStream &in)
953
Writes the item to stream \a out.
957
void QTableWidgetItem::write(QDataStream &out) const
963
\relates QTableWidgetItem
965
Reads a table widget item from stream \a in into \a item.
967
This operator uses QTableWidgetItem::read().
969
\sa {Format of the QDataStream Operators}
971
QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item)
978
\relates QTableWidgetItem
980
Writes the table widget item \a item to stream \a out.
982
This operator uses QTableWidgetItem::write().
984
\sa {Format of the QDataStream Operators}
986
QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item)
992
#endif // QT_NO_DATASTREAM
995
Assigns \a other to this object.
997
QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other)
999
values = other.values;
1001
model = other.model;
1002
itemFlags = other.itemFlags;
1008
\brief The QTableWidget class provides an item-based table view with a default model.
1013
Table widgets provide standard table display facilities for applications.
1014
The items in a QTableWidget are provided by QTableWidgetItem.
1016
If you want a table that uses your own data model you should
1017
use QTableView rather than this class.
1019
Table widgets can be constructed with the required numbers of rows and
1022
\quotefile snippets/qtablewidget-using/mainwindow.cpp
1023
\skipto tableWidget = new
1024
\printuntil tableWidget = new
1026
Alternatively, tables can be constructed without a given size and resized
1029
\quotefile snippets/qtablewidget-resizing/mainwindow.cpp
1030
\skipto tableWidget = new
1031
\printuntil tableWidget = new
1032
\skipto tableWidget->setRowCount(
1033
\printuntil tableWidget->setColumnCount(
1035
Items are created ouside the table (with no parent widget) and inserted
1036
into the table with setItem():
1038
\skipto QTableWidgetItem *newItem
1039
\printuntil tableWidget->setItem(
1041
Tables can be given both horizontal and vertical headers. The simplest way
1042
to create the headers is to supply a list of strings to the
1043
setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These
1044
will provide simple textual headers for the table's columns and rows.
1045
More sophisticated headers can be created from existing table items
1046
that are usually constructed outside the table. For example, we can
1047
construct a table item with an icon and aligned text, and use it as the
1048
header for a particular column:
1050
\quotefile snippets/qtablewidget-using/mainwindow.cpp
1051
\skipto QTableWidgetItem *cubesHeaderItem
1052
\printuntil cubesHeaderItem->setTextAlignment
1054
The number of rows in the table can be found with rowCount(), and the
1055
number of columns with columnCount(). The table can be cleared with the
1058
\sa QTableWidgetItem \link model-view-programming.html Model/View Programming\endlink
1062
\property QTableWidget::rowCount
1063
\brief the number of rows in the table
1067
\property QTableWidget::columnCount
1068
\brief the number of columns in the table
1072
\property QTableWidget::sortingEnabled
1073
\brief whether the items in the table can be sorted
1074
by clicking on the horizontal header.
1077
class QTableWidgetPrivate : public QTableViewPrivate
1079
Q_DECLARE_PUBLIC(QTableWidget)
1081
QTableWidgetPrivate() : QTableViewPrivate(), sortingEnabled(false) {}
1082
inline QTableModel *model() const { return ::qobject_cast<QTableModel*>(q_func()->model()); }
1085
void emitItemPressed(const QModelIndex &index);
1086
void emitItemClicked(const QModelIndex &index);
1087
void emitItemDoubleClicked(const QModelIndex &index);
1088
void emitItemActivated(const QModelIndex &index);
1089
void emitItemEntered(const QModelIndex &index);
1091
void emitItemChanged(const QModelIndex &index);
1092
// selection signals
1093
void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t);
1095
bool sortingEnabled;
1098
void QTableWidgetPrivate::setup()
1102
QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(emitItemPressed(QModelIndex)));
1103
QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(emitItemClicked(QModelIndex)));
1104
QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)),
1105
q, SLOT(emitItemDoubleClicked(QModelIndex)));
1106
QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(emitItemActivated(QModelIndex)));
1107
QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(emitItemEntered(QModelIndex)));
1109
QObject::connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
1110
q, SLOT(emitItemChanged(QModelIndex)));
1111
// selection signals
1112
QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
1113
q, SLOT(emitCurrentItemChanged(QModelIndex,QModelIndex)));
1114
QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
1115
q, SIGNAL(itemSelectionChanged()));
1118
void QTableWidgetPrivate::emitItemPressed(const QModelIndex &index)
1121
emit q->itemPressed(model()->item(index));
1124
void QTableWidgetPrivate::emitItemClicked(const QModelIndex &index)
1127
emit q->itemClicked(model()->item(index));
1130
void QTableWidgetPrivate::emitItemDoubleClicked(const QModelIndex &index)
1133
emit q->itemDoubleClicked(model()->item(index));
1136
void QTableWidgetPrivate::emitItemActivated(const QModelIndex &index)
1139
emit q->itemActivated(model()->item(index));
1142
void QTableWidgetPrivate::emitItemEntered(const QModelIndex &index)
1145
emit q->itemEntered(model()->item(index));
1148
void QTableWidgetPrivate::emitItemChanged(const QModelIndex &index)
1151
emit q->itemChanged(model()->item(index));
1154
void QTableWidgetPrivate::emitCurrentItemChanged(const QModelIndex ¤t,
1155
const QModelIndex &previous)
1158
emit q->currentItemChanged(model()->item(current), model()->item(previous));
1162
\fn void QTableWidget::itemActivated(QTableWidgetItem *item)
1164
This signal is emitted when the specified \a item has been activated
1168
\fn void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
1170
This signal is emitted whenever the current item changes. The \a
1171
previous item is the item that previously had the focus, \a
1172
current is the new current item.
1176
\fn void QTableWidget::itemSelectionChanged()
1178
This signal is emitted whenever the selection changes.
1180
\sa selectedItems() isItemSelected()
1184
\fn void QTableWidget::itemClicked(QTableWidgetItem *item)
1186
This signal is emitted whenever an item in the table is clicked.
1187
The \a item specified is the item that was clicked.
1191
\fn void QTableWidget::itemDoubleClicked(QTableWidgetItem *item)
1193
This signal is emitted whenever an item in the table is double
1194
clicked. The \a item specified is the item that was double clicked.
1198
\fn void QTableWidget::itemEntered(QTableWidgetItem *item)
1200
This signal is emitted when the mouse cursor enters an item. The
1201
\a item is the item entered.
1203
This signal is only emitted when mouseTracking is turned on, or when a
1204
mouse button is pressed while moving into an item.
1208
\fn void QTableWidget::itemPressed(QTableWidgetItem *item)
1210
This signal is emitted whenever an item in the table is pressed.
1211
The \a item specified is the item that was pressed.
1215
\fn void QTableWidget::itemChanged(QTableWidgetItem *item)
1217
This signal is emitted whenever the data of \a item has changed.
1221
\fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const
1223
Returns the item at the position (\a{ax}, \a{ay}) in the table's
1228
\variable QTableWidgetItem::Type
1230
The default type for table widget items.
1232
\sa UserType, type()
1236
\variable QTableWidgetItem::UserType
1238
The minimum value for custom types. Values below UserType are
1245
\fn int QTableWidgetItem::type() const
1247
Returns the type passed to the QTableWidgetItem constructor.
1251
Creates a new table view with the given \a parent.
1253
QTableWidget::QTableWidget(QWidget *parent)
1254
: QTableView(*new QTableWidgetPrivate, parent)
1257
setModel(new QTableModel(0, 0, this));
1262
Creates a new table view with the given \a rows and \a columns, and with the given \a parent.
1264
QTableWidget::QTableWidget(int rows, int columns, QWidget *parent)
1265
: QTableView(*new QTableWidgetPrivate, parent)
1268
setModel(new QTableModel(rows, columns, this));
1273
Destroys this QTableWidget.
1275
QTableWidget::~QTableWidget()
1280
Sets the number of rows in this table's model to \a rows. If
1281
this is less than rowCount(), the data in the unwanted rows
1284
\sa setColumnCount()
1286
void QTableWidget::setRowCount(int rows)
1289
d->model()->setRowCount(rows);
1293
Returns the number of rows.
1296
int QTableWidget::rowCount() const
1298
Q_D(const QTableWidget);
1299
return d->model()->rowCount();
1303
Sets the number of columns in this table's model to \a columns. If
1304
this is less than columnCount(), the data in the unwanted columns
1309
void QTableWidget::setColumnCount(int columns)
1312
d->model()->setColumnCount(columns);
1316
Returns the number of columns.
1319
int QTableWidget::columnCount() const
1321
Q_D(const QTableWidget);
1322
return d->model()->columnCount();
1326
Returns the row for the \a item.
1328
int QTableWidget::row(const QTableWidgetItem *item) const
1331
Q_D(const QTableWidget);
1332
return d->model()->index(item).row();
1336
Returns the column for the \a item.
1338
int QTableWidget::column(const QTableWidgetItem *item) const
1341
Q_D(const QTableWidget);
1342
return d->model()->index(item).column();
1347
Returns the item for the given \a row and \a column.
1351
QTableWidgetItem *QTableWidget::item(int row, int column) const
1353
Q_D(const QTableWidget);
1354
return d->model()->item(row, column);
1358
Sets the item for the given \a row and \a column to \a item.
1362
void QTableWidget::setItem(int row, int column, QTableWidgetItem *item)
1367
d->model()->setItem(row, column, item);
1371
Removes the item at \a row and \a column from the table without deleting it.
1373
QTableWidgetItem *QTableWidget::takeItem(int row, int column)
1376
QTableWidgetItem *item = d->model()->takeItem(row, column);
1382
Returns the vertical header item for row \a row.
1384
QTableWidgetItem *QTableWidget::verticalHeaderItem(int row) const
1386
Q_D(const QTableWidget);
1387
return d->model()->verticalHeaderItem(row);
1391
Sets the vertical header item for row \a row to \a item.
1393
void QTableWidget::setVerticalHeaderItem(int row, QTableWidgetItem *item)
1397
d->model()->setVerticalHeaderItem(row, item);
1401
Returns the horizontal header item for column \a column.
1403
QTableWidgetItem *QTableWidget::horizontalHeaderItem(int column) const
1405
Q_D(const QTableWidget);
1406
return d->model()->horizontalHeaderItem(column);
1410
Sets the horizontal header item for column \a column to \a item.
1412
void QTableWidget::setHorizontalHeaderItem(int column, QTableWidgetItem *item)
1416
d->model()->setHorizontalHeaderItem(column, item);
1420
Sets the vertical header labels using \a labels.
1422
void QTableWidget::setVerticalHeaderLabels(const QStringList &labels)
1425
QTableModel *model = d->model();
1426
QTableWidgetItem *item = 0;
1427
for (int i = 0; i < model->rowCount() && i < labels.count(); ++i) {
1428
item = model->verticalHeaderItem(i);
1430
item = model->createItem();
1431
setVerticalHeaderItem(i, item);
1433
item->setText(labels.at(i));
1438
Sets the horizontal header labels using \a labels.
1440
void QTableWidget::setHorizontalHeaderLabels(const QStringList &labels)
1443
QTableModel *model = d->model();
1444
QTableWidgetItem *item = 0;
1445
for (int i = 0; i < model->columnCount() && i < labels.count(); ++i) {
1446
item = model->horizontalHeaderItem(i);
1448
item = model->createItem();
1449
setHorizontalHeaderItem(i, item);
1451
item->setText(labels.at(i));
1456
Returns the row of the current item.
1458
int QTableWidget::currentRow() const
1460
return currentIndex().row();
1464
Returns the column of the current item.
1466
int QTableWidget::currentColumn() const
1468
return currentIndex().column();
1472
Returns the current item.
1474
QTableWidgetItem *QTableWidget::currentItem() const
1476
Q_D(const QTableWidget);
1477
return d->model()->item(currentIndex());
1481
Sets the current item to \a item.
1483
void QTableWidget::setCurrentItem(QTableWidgetItem *item)
1486
setCurrentIndex(d->model()->index(item));
1490
Sorts all the rows in the table widget based on \a column and \a order.
1492
void QTableWidget::sortItems(int column, Qt::SortOrder order)
1495
d->model()->sort(column, order);
1496
horizontalHeader()->setSortIndicator(column, order);
1500
If \a enable is true, the items in the widget will be sorted if the user
1501
clicks on a horizontal header section; otherwise sorting is disabled.
1504
void QTableWidget::setSortingEnabled(bool enable)
1507
d->sortingEnabled = enable;
1508
if (!enable && horizontalHeader()->isSortIndicatorShown())
1509
horizontalHeader()->setSortIndicatorShown(false);
1513
Returns if sorting is enabled; otherwise returns false.
1514
Sorting is enabled when the user clicks on a horizontal header section.
1517
bool QTableWidget::isSortingEnabled() const
1519
Q_D(const QTableWidget);
1520
return d->sortingEnabled;
1524
Starts editing the \a item if it is editable.
1527
void QTableWidget::editItem(QTableWidgetItem *item)
1531
edit(d->model()->index(item));
1535
Opens an editor for the give \a item. The editor remains open after editing.
1537
\sa closePersistentEditor()
1539
void QTableWidget::openPersistentEditor(QTableWidgetItem *item)
1543
QModelIndex index = d->model()->index(item);
1544
QAbstractItemView::openPersistentEditor(index);
1548
Closes the persistent editor for \a item.
1550
\sa openPersistentEditor()
1552
void QTableWidget::closePersistentEditor(QTableWidgetItem *item)
1556
QModelIndex index = d->model()->index(item);
1557
QAbstractItemView::closePersistentEditor(index);
1561
Returns true if the \a item is selected, otherwise returns false.
1564
bool QTableWidget::isItemSelected(const QTableWidgetItem *item) const
1566
Q_D(const QTableWidget);
1567
QModelIndex index = d->model()->index(item);
1568
return selectionModel()->isSelected(index) && !isIndexHidden(index);
1572
Selects or deselects \a item depending on \a select.
1574
void QTableWidget::setItemSelected(const QTableWidgetItem *item, bool select)
1577
QModelIndex index = d->model()->index(item);
1578
selectionModel()->select(index, select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
1582
Selects or deselects the \a range depending on \a select.
1584
void QTableWidget::setRangeSelected(const QTableWidgetSelectionRange &range, bool select)
1586
if (!model()->hasIndex(range.topRow(), range.leftColumn(), rootIndex()) ||
1587
!model()->hasIndex(range.bottomRow(), range.rightColumn(), rootIndex()))
1590
QModelIndex topLeft = model()->index(range.topRow(), range.leftColumn(), rootIndex());
1591
QModelIndex bottomRight = model()->index(range.bottomRow(), range.rightColumn(), rootIndex());
1593
selectionModel()->select(QItemSelection(topLeft, bottomRight),
1594
select ? QItemSelectionModel::Select : QItemSelectionModel::Deselect);
1598
Returns a list of all selected ranges.
1600
\sa QTableWidgetSelectionRange
1603
QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
1605
const QList<QItemSelectionRange> ranges = selectionModel()->selection();
1606
QList<QTableWidgetSelectionRange> result;
1607
for (int i = 0; i < ranges.count(); ++i)
1608
result.append(QTableWidgetSelectionRange(ranges.at(i).top(),
1609
ranges.at(i).left(),
1610
ranges.at(i).bottom(),
1611
ranges.at(i).right()));
1616
Returns a list of all selected items.
1619
QList<QTableWidgetItem*> QTableWidget::selectedItems()
1622
QModelIndexList indexes = selectedIndexes();
1623
QList<QTableWidgetItem*> items;
1624
for (int i = 0; i < indexes.count(); ++i) {
1625
QModelIndex index = indexes.at(i);
1626
QTableWidgetItem *item = d->model()->item(index);
1634
Finds items that matches the \a text using the given \a flags.
1637
QList<QTableWidgetItem*> QTableWidget::findItems(const QString &text, Qt::MatchFlags flags) const
1639
Q_D(const QTableWidget);
1640
QModelIndexList indexes;
1641
for (int column = 0; column < columnCount(); ++column)
1642
indexes += d->model()->match(model()->index(0, column, QModelIndex()),
1643
Qt::DisplayRole, text, -1, flags);
1644
QList<QTableWidgetItem*> items;
1645
for (int i = 0; i < indexes.size(); ++i)
1646
items.append(d->model()->item(indexes.at(i)));
1651
Returns the visual row of the given \a logicalRow.
1654
int QTableWidget::visualRow(int logicalRow) const
1656
return verticalHeader()->visualIndex(logicalRow);
1660
Returns the visual column of the given \a logicalColumn.
1663
int QTableWidget::visualColumn(int logicalColumn) const
1665
return horizontalHeader()->visualIndex(logicalColumn);
1669
Returns a pointer to the item at the coordinates \a p.
1672
QTableWidgetItem *QTableWidget::itemAt(const QPoint &p) const
1674
Q_D(const QTableWidget);
1675
return d->model()->item(indexAt(p));
1679
Returns the rectangle on the viewport occupied by the item at \a item.
1681
QRect QTableWidget::visualItemRect(const QTableWidgetItem *item) const
1684
Q_D(const QTableWidget);
1685
QModelIndex index = d->model()->index(const_cast<QTableWidgetItem*>(item));
1686
Q_ASSERT(index.isValid());
1687
return visualRect(index);
1691
Scrolls the view if necessary to ensure that the \a item is visible.
1692
The \a hint parameter specifies more precisely where the
1693
\a item should be located after the operation.
1696
void QTableWidget::scrollToItem(const QTableWidgetItem *item, ScrollHint hint)
1700
QModelIndex index = d->model()->index(const_cast<QTableWidgetItem*>(item));
1701
Q_ASSERT(index.isValid());
1702
QTableView::scrollTo(index, hint);
1706
Returns the item prototype used by the table.
1708
Copies of the item prototype are returned by the createItem()
1711
\sa setItemPrototype()
1713
const QTableWidgetItem *QTableWidget::itemPrototype() const
1715
Q_D(const QTableWidget);
1716
return d->model()->itemPrototype();
1720
Sets the item prototype for the table to the specified \a item.
1724
void QTableWidget::setItemPrototype(const QTableWidgetItem *item)
1727
d->model()->setItemPrototype(item);
1731
Inserts an empty row into the table at \a row.
1733
void QTableWidget::insertRow(int row)
1736
d->model()->insertRows(row);
1740
Inserts an empty column into the table at \a column.
1742
void QTableWidget::insertColumn(int column)
1745
d->model()->insertColumns(column);
1749
Removes the row \a row and all its items from the table.
1751
void QTableWidget::removeRow(int row)
1754
d->model()->removeRows(row);
1758
Removes the column \a column and all its items from the table.
1760
void QTableWidget::removeColumn(int column)
1763
d->model()->removeColumns(column);
1767
Removes all items and selections in the view.
1770
void QTableWidget::clear()
1773
selectionModel()->clear();
1774
d->model()->clear();
1778
Returns a list of MIME types that can be used to describe a list of
1783
QStringList QTableWidget::mimeTypes() const
1785
return model()->QAbstractItemModel::mimeTypes();
1789
Returns an object that contains a serialized description of the specified
1790
\a items. The format used to describe the items is obtained from the
1791
mimeTypes() function.
1793
If the list of items is empty, 0 is returned rather than a serialized
1796
QMimeData *QTableWidget::mimeData(const QList<QTableWidgetItem*>) const
1798
return d_func()->model()->internalMimeData();
1802
Handles the \a data supplied by a drag and drop operation that ended with
1803
the given \a action in the given \a row and \a column.
1805
\sa supportedDropActions()
1807
bool QTableWidget::dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
1809
return model()->QAbstractItemModel::dropMimeData(data, action , row, column, QModelIndex());
1813
Returns the drop actions supported by this view.
1817
Qt::DropActions QTableWidget::supportedDropActions() const
1819
return model()->QAbstractItemModel::supportedDropActions();
1823
Returns a list of pointers to the items contained in the \a data object.
1824
If the object was not created by a QTreeWidget in the same process, the list
1828
QList<QTableWidgetItem*> QTableWidget::items(const QMimeData *data) const
1830
const QTableWidgetMimeData *twd = qobject_cast<const QTableWidgetMimeData*>(data);
1833
return QList<QTableWidgetItem*>();
1837
Returns the QModelIndex assocated with the given \a item.
1840
QModelIndex QTableWidget::indexFromItem(QTableWidgetItem *item) const
1842
Q_D(const QTableWidget);
1844
return d->model()->index(item);
1848
Returns a pointer to the QTableWidgetItem assocated with the given \a index.
1851
QTableWidgetItem *QTableWidget::itemFromIndex(const QModelIndex &index) const
1853
Q_D(const QTableWidget);
1854
Q_ASSERT(index.isValid());
1855
return d->model()->item(index);
1861
void QTableWidget::setModel(QAbstractItemModel *model)
1863
QTableView::setModel(model);
1866
#include "moc_qtablewidget.cpp"