Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | List of all members
QTableModel Class Reference

#include <qtablewidget_p.h>

Inheritance diagram for QTableModel:
QAbstractTableModel QAbstractItemModel QObject

Public Types

enum  ItemFlagsExtension { ItemIsHeaderItem = 128 }
 

Public Functions

void clear ()
 
void clearContents ()
 
int columnCount (const QModelIndex &parent=QModelIndex()) const
 Returns the number of columns for the children of the given parent. More...
 
QVector< QTableWidgetItem * > columnItems (int column) const
 
QTableWidgetItemcreateItem () const
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const
 Returns the data stored under the given role for the item referred to by the index. More...
 
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
 Reimplemented Function More...
 
void ensureSorted (int column, Qt::SortOrder order, int start, int end)
 
Qt::ItemFlags flags (const QModelIndex &index) const
 Returns the item flags for the given index. More...
 
QVariant headerData (int section, Qt::Orientation orientation, int role) const
 Returns the data for the given role and section in the header with the specified orientation. More...
 
QTableWidgetItemhorizontalHeaderItem (int section)
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const
 Returns the index of the data in row and column with parent. More...
 
QModelIndex index (const QTableWidgetItem *item) const
 
bool insertColumns (int column, int count=1, const QModelIndex &parent=QModelIndex())
 On models that support this, inserts count new columns into the model before the given column. More...
 
bool insertRows (int row, int count=1, const QModelIndex &parent=QModelIndex())
 On models that support this, inserts count rows into the model before the given row. More...
 
QMimeDatainternalMimeData () const
 
bool isValid (const QModelIndex &index) const
 
QTableWidgetItemitem (int row, int column) const
 
QTableWidgetItemitem (const QModelIndex &index) const
 
void itemChanged (QTableWidgetItem *item)
 
QMap< int, QVariantitemData (const QModelIndex &index) const
 Returns a map with values for all predefined roles in the model for the item at the given index. More...
 
const QTableWidgetItemitemPrototype () const
 
QMimeDatamimeData (const QModelIndexList &indexes) const
 Returns an object that contains serialized items of data corresponding to the list of indexes specified. More...
 
QStringList mimeTypes () const
 Returns a list of MIME types that can be used to describe a list of model indexes. More...
 
 QTableModel (int rows, int columns, QTableWidget *parent)
 
bool removeColumns (int column, int count=1, const QModelIndex &parent=QModelIndex())
 On models that support this, removes count columns starting with the given column under parent parent from the model. More...
 
void removeItem (QTableWidgetItem *item)
 
bool removeRows (int row, int count=1, const QModelIndex &parent=QModelIndex())
 On models that support this, removes count rows starting with the given row under parent parent from the model. More...
 
int rowCount (const QModelIndex &parent=QModelIndex()) const
 Returns the number of rows under the given parent. More...
 
void setColumnCount (int columns)
 
bool setData (const QModelIndex &index, const QVariant &value, int role)
 Sets the role data for the item at index to value. More...
 
bool setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role)
 Sets the data for the given role and section in the header with the specified orientation to the value supplied. More...
 
void setHorizontalHeaderItem (int section, QTableWidgetItem *item)
 
void setItem (int row, int column, QTableWidgetItem *item)
 
bool setItemData (const QModelIndex &index, const QMap< int, QVariant > &roles)
 Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole. More...
 
void setItemPrototype (const QTableWidgetItem *item)
 
void setRowCount (int rows)
 
void setVerticalHeaderItem (int section, QTableWidgetItem *item)
 
void sort (int column, Qt::SortOrder order)
 Sorts the model by column in the given order. More...
 
Qt::DropActions supportedDropActions () const
 Returns the drop actions supported by this model. More...
 
long tableIndex (int row, int column) const
 
QTableWidgetItemtakeHorizontalHeaderItem (int section)
 
QTableWidgetItemtakeItem (int row, int column)
 
QTableWidgetItemtakeVerticalHeaderItem (int section)
 
void updateRowIndexes (QModelIndexList &indexes, int movedFromRow, int movedToRow)
 
QTableWidgetItemverticalHeaderItem (int section)
 
 ~QTableModel ()
 
- Public Functions inherited from QAbstractTableModel
 QAbstractTableModel (QObject *parent=0)
 Constructs an abstract table model for the given parent. More...
 
 ~QAbstractTableModel ()
 Destroys the abstract table model. More...
 
- Public Functions inherited from QAbstractItemModel
virtual QModelIndex buddy (const QModelIndex &index) const
 Returns a model index for the buddy of the item represented by index. More...
 
virtual bool canFetchMore (const QModelIndex &parent) const
 Returns true if there is more data available for parent; otherwise returns false. More...
 
virtual void fetchMore (const QModelIndex &parent)
 Fetches any available data for the items with the parent specified by the parent index. More...
 
bool hasIndex (int row, int column, const QModelIndex &parent=QModelIndex()) const
 Returns true if the model returns a valid QModelIndex for row and column with parent, otherwise returns false. More...
 
bool insertColumn (int column, const QModelIndex &parent=QModelIndex())
 Inserts a single column before the given column in the child items of the parent specified. More...
 
bool insertRow (int row, const QModelIndex &parent=QModelIndex())
 Inserts a single row before the given row in the child items of the parent specified. More...
 
virtual QModelIndexList match (const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
 Returns a list of indexes for the items in the column of the start index where data stored under the given role matches the specified value. More...
 
 QAbstractItemModel (QObject *parent=0)
 Constructs an abstract item model with the given parent. More...
 
bool removeColumn (int column, const QModelIndex &parent=QModelIndex())
 Removes the given column from the child items of the parent specified. More...
 
bool removeRow (int row, const QModelIndex &parent=QModelIndex())
 Removes the given row from the child items of the parent specified. More...
 
const QHash< int, QByteArray > & roleNames () const
 Returns the model's role names. More...
 
void setSupportedDragActions (Qt::DropActions)
 Sets the supported drag actions for the items in the model. More...
 
QModelIndex sibling (int row, int column, const QModelIndex &idx) const
 Returns the sibling at row and column for the item at index, or an invalid QModelIndex if there is no sibling at that location. More...
 
virtual QSize span (const QModelIndex &index) const
 Returns the row and column span of the item represented by index. More...
 
Qt::DropActions supportedDragActions () const
 Returns the actions supported by the data in this model. More...
 
virtual ~QAbstractItemModel ()
 Destroys the abstract item model. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Static Public Functions

static bool itemGreaterThan (const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
 
static bool itemLessThan (const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
 
static QVector< QTableWidgetItem * >::iterator sortedInsertionIterator (const QVector< QTableWidgetItem *>::iterator &begin, const QVector< QTableWidgetItem *>::iterator &end, Qt::SortOrder order, QTableWidgetItem *item)
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 

Properties

QModelIndexList cachedIndexes
 
QVector< QTableWidgetItem * > horizontalHeaderItems
 
const QTableWidgetItemprototype
 
QVector< QTableWidgetItem * > tableItems
 
QVector< QTableWidgetItem * > verticalHeaderItems
 

Additional Inherited Members

- Public Slots inherited from QAbstractItemModel
virtual void revert ()
 Lets the model know that it should discard cached information. More...
 
virtual bool submit ()
 Lets the model know that it should submit cached information to permanent storage. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QAbstractItemModel
void columnsAboutToBeInserted (const QModelIndex &parent, int first, int last)
 This signal is emitted just before columns are inserted into the model. More...
 
void columnsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationColumn)
 This signal is emitted just before columns are moved within the model. More...
 
void columnsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 This signal is emitted just before columns are removed from the model. More...
 
void columnsInserted (const QModelIndex &parent, int first, int last)
 This signal is emitted after columns have been inserted into the model. More...
 
void columnsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int column)
 This signal is emitted after columns have been moved within the model. More...
 
void columnsRemoved (const QModelIndex &parent, int first, int last)
 This signal is emitted after columns have been removed from the model. More...
 
void dataChanged (const QModelIndex &topLeft, const QModelIndex &bottomRight)
 This signal is emitted whenever the data in an existing item changes. More...
 
void headerDataChanged (Qt::Orientation orientation, int first, int last)
 This signal is emitted whenever a header is changed. More...
 
void layoutAboutToBeChanged ()
 This signal is emitted just before the layout of a model is changed. More...
 
void layoutChanged ()
 This signal is emitted whenever the layout of items exposed by the model has changed; for example, when the model has been sorted. More...
 
void modelAboutToBeReset ()
 This signal is emitted when reset() is called, before the model's internal state (e. More...
 
void modelReset ()
 This signal is emitted when reset() is called, after the model's internal state (e. More...
 
void rowsAboutToBeInserted (const QModelIndex &parent, int first, int last)
 This signal is emitted just before rows are inserted into the model. More...
 
void rowsAboutToBeMoved (const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destinationParent, int destinationRow)
 This signal is emitted just before rows are moved within the model. More...
 
void rowsAboutToBeRemoved (const QModelIndex &parent, int first, int last)
 This signal is emitted just before rows are removed from the model. More...
 
void rowsInserted (const QModelIndex &parent, int first, int last)
 This signal is emitted after rows have been inserted into the model. More...
 
void rowsMoved (const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row)
 This signal is emitted after rows have been moved within the model. More...
 
void rowsRemoved (const QModelIndex &parent, int first, int last)
 This signal is emitted after rows have been removed from the model. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Slots inherited from QAbstractItemModel
void resetInternalData ()
 This slot is called just after the internal data of a model is cleared while it is being reset. More...
 
- Protected Functions inherited from QAbstractTableModel
 QAbstractTableModel (QAbstractItemModelPrivate &dd, QObject *parent)
 Constructs an abstract table model with dd and the given parent. More...
 
- Protected Functions inherited from QAbstractItemModel
void beginInsertColumns (const QModelIndex &parent, int first, int last)
 Begins a column insertion operation. More...
 
void beginInsertRows (const QModelIndex &parent, int first, int last)
 Begins a row insertion operation. More...
 
bool beginMoveColumns (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationColumn)
 Begins a column move operation. More...
 
bool beginMoveRows (const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destinationParent, int destinationRow)
 Begins a row move operation. More...
 
void beginRemoveColumns (const QModelIndex &parent, int first, int last)
 Begins a column removal operation. More...
 
void beginRemoveRows (const QModelIndex &parent, int first, int last)
 Begins a row removal operation. More...
 
void beginResetModel ()
 Begins a model reset operation. More...
 
void changePersistentIndex (const QModelIndex &from, const QModelIndex &to)
 Changes the QPersistentModelIndex that is equal to the given from model index to the given to model index. More...
 
void changePersistentIndexList (const QModelIndexList &from, const QModelIndexList &to)
 Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list to the given to model index list. More...
 
QModelIndex createIndex (int row, int column, void *data=0) const
 Creates a model index for the given row and column with the internal pointer ptr. More...
 
QModelIndex createIndex (int row, int column, int id) const
 Use QModelIndex QAbstractItemModel::createIndex(int row, int column, quint32 id) instead. More...
 
QModelIndex createIndex (int row, int column, quint32 id) const
 Creates a model index for the given row and column with the internal identifier, id. More...
 
bool decodeData (int row, int column, const QModelIndex &parent, QDataStream &stream)
 
void encodeData (const QModelIndexList &indexes, QDataStream &stream) const
 
void endInsertColumns ()
 Ends a column insertion operation. More...
 
void endInsertRows ()
 Ends a row insertion operation. More...
 
void endMoveColumns ()
 Ends a column move operation. More...
 
void endMoveRows ()
 Ends a row move operation. More...
 
void endRemoveColumns ()
 Ends a column removal operation. More...
 
void endRemoveRows ()
 Ends a row removal operation. More...
 
void endResetModel ()
 Completes a model reset operation. More...
 
QModelIndexList persistentIndexList () const
 Returns the list of indexes stored as persistent indexes in the model. More...
 
 QAbstractItemModel (QAbstractItemModelPrivate &dd, QObject *parent=0)
 
void reset ()
 Resets the model to its original state in any attached views. More...
 
void setRoleNames (const QHash< int, QByteArray > &roleNames)
 Sets the model's role names to roleNames. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 90 of file qtablewidget_p.h.

Enumerations

◆ ItemFlagsExtension

Enumerator
ItemIsHeaderItem 

Definition at line 94 of file qtablewidget_p.h.

94  {
95  ItemIsHeaderItem = 128
96  }; // we need this to separate header items from other items

Constructors and Destructors

◆ QTableModel()

QTableModel::QTableModel ( int  rows,
int  columns,
QTableWidget parent 
)

Definition at line 51 of file qtablewidget.cpp.

52  : QAbstractTableModel(parent),
53  prototype(0),
54  tableItems(rows * columns, 0),
55  verticalHeaderItems(rows, 0),
56  horizontalHeaderItems(columns, 0)
57 {}
QVector< QTableWidgetItem * > verticalHeaderItems
const QTableWidgetItem * prototype
QVector< QTableWidgetItem * > horizontalHeaderItems
QAbstractTableModel(QObject *parent=0)
Constructs an abstract table model for the given parent.
QVector< QTableWidgetItem * > tableItems

◆ ~QTableModel()

QTableModel::~QTableModel ( )

Definition at line 59 of file qtablewidget.cpp.

60 {
61  clear();
62  delete prototype;
63 }
const QTableWidgetItem * prototype

Functions

◆ clear()

void QTableModel::clear ( )

Definition at line 737 of file qtablewidget.cpp.

Referenced by ~QTableModel().

738 {
739  for (int j = 0; j < verticalHeaderItems.count(); ++j) {
740  if (verticalHeaderItems.at(j)) {
741  verticalHeaderItems.at(j)->view = 0;
742  delete verticalHeaderItems.at(j);
743  verticalHeaderItems[j] = 0;
744  }
745  }
746  for (int k = 0; k < horizontalHeaderItems.count(); ++k) {
747  if (horizontalHeaderItems.at(k)) {
748  horizontalHeaderItems.at(k)->view = 0;
749  delete horizontalHeaderItems.at(k);
750  horizontalHeaderItems[k] = 0;
751  }
752  }
753  clearContents();
754 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
QTableWidget * view
Definition: qtablewidget.h:186
void clearContents()
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350

◆ clearContents()

void QTableModel::clearContents ( )

Definition at line 756 of file qtablewidget.cpp.

Referenced by clear().

757 {
758  for (int i = 0; i < tableItems.count(); ++i) {
759  if (tableItems.at(i)) {
760  tableItems.at(i)->view = 0;
761  delete tableItems.at(i);
762  tableItems[i] = 0;
763  }
764  }
765  reset();
766 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QTableWidget * view
Definition: qtablewidget.h:186
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< QTableWidgetItem * > tableItems
void reset()
Resets the model to its original state in any attached views.

◆ columnCount()

int QTableModel::columnCount ( const QModelIndex parent = QModelIndex()) const
virtual

Returns the number of columns for the children of the given parent.

In most subclasses, the number of columns is independent of the parent.

For example:

int DomModel::columnCount(const QModelIndex &/*parent*/) const
{
return 3;
}
Note
When implementing a table based model, columnCount() should return 0 when the parent is valid.
See also
rowCount()

Implements QAbstractItemModel.

Definition at line 397 of file qtablewidget.cpp.

Referenced by ensureSorted(), index(), removeRows(), QTableWidget::setHorizontalHeaderLabels(), setItem(), and sort().

398 {
399  return parent.isValid() ? 0 : horizontalHeaderItems.count();
400 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QVector< QTableWidgetItem * > horizontalHeaderItems

◆ columnItems()

QVector< QTableWidgetItem * > QTableModel::columnItems ( int  column) const

Definition at line 619 of file qtablewidget.cpp.

Referenced by ensureSorted(), and setItem().

620 {
622  int rc = rowCount();
623  items.reserve(rc);
624  for (int row = 0; row < rc; ++row) {
625  QTableWidgetItem *itm = item(row, column);
626  if (itm == 0) {
627  // no more sortable items (all 0-items are
628  // at the end of the table when it is sorted)
629  break;
630  }
631  items.append(itm);
632  }
633  return items;
634 }
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
QTableWidgetItem * item(int row, int column) const
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ createItem()

QTableWidgetItem * QTableModel::createItem ( ) const

Definition at line 788 of file qtablewidget.cpp.

Referenced by setData(), QTableWidget::setHorizontalHeaderLabels(), setItemData(), and QTableWidget::setVerticalHeaderLabels().

789 {
790  return prototype ? prototype->clone() : new QTableWidgetItem;
791 }
const QTableWidgetItem * prototype
virtual QTableWidgetItem * clone() const
Creates a copy of the item.
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ data()

QVariant QTableModel::data ( const QModelIndex index,
int  role = Qt::DisplayRole 
) const
virtual

Returns the data stored under the given role for the item referred to by the index.

Note
If you do not have a value to return, return an invalid QVariant instead of returning 0.
See also
Qt::ItemDataRole, setData(), headerData()

Implements QAbstractItemModel.

Definition at line 402 of file qtablewidget.cpp.

403 {
404  QTableWidgetItem *itm = item(index);
405  if (itm)
406  return itm->data(role);
407  return QVariant();
408 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QTableWidgetItem * item(int row, int column) const
virtual QVariant data(int role) const
Returns the item&#39;s data for the given role.
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ dropMimeData()

bool QTableModel::dropMimeData ( const QMimeData data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex parent 
)
virtual

Reimplemented Function

Reimplemented from QAbstractTableModel.

Definition at line 832 of file qtablewidget.cpp.

834 {
835  if (index.isValid()) {
836  row = index.row();
837  column = index.column();
838  }else if (row == -1 || column == -1) { // The user dropped outside the table.
839  row = rowCount();
840  column = 0;
841  }
842 
844  return (view ? view->dropMimeData(row, column, data, action) : false);
845 }
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
int row() const
Returns the row this model index refers to.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
virtual bool dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action)
Handles the data supplied by a drag and drop operation that ended with the given action in the given ...
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
int column() const
Returns the column this model index refers to.

◆ ensureSorted()

void QTableModel::ensureSorted ( int  column,
Qt::SortOrder  order,
int  start,
int  end 
)

Definition at line 542 of file qtablewidget.cpp.

544 {
545  int count = end - start + 1;
547  sorting.reserve(count);
548  for (int row = start; row <= end; ++row) {
549  QTableWidgetItem *itm = item(row, column);
550  if (itm == 0) {
551  // no more sortable items (all 0-items are
552  // at the end of the table when it is sorted)
553  break;
554  }
555  sorting.append(QPair<QTableWidgetItem*,int>(itm, row));
556  }
557 
559  qStableSort(sorting.begin(), sorting.end(), compare);
560 
561  QModelIndexList oldPersistentIndexes = persistentIndexList();
562  QModelIndexList newPersistentIndexes = oldPersistentIndexes;
565  QVector<QTableWidgetItem*> colItems = columnItems(column);
567  bool changed = false;
568  for (int i = 0; i < sorting.count(); ++i) {
569  int oldRow = sorting.at(i).second;
570  QTableWidgetItem *item = colItems.at(oldRow);
571  colItems.remove(oldRow);
572  vit = sortedInsertionIterator(vit, colItems.end(), order, item);
573  int newRow = qMax((int)(vit - colItems.begin()), 0);
574  if ((newRow < oldRow) && !(*item < *colItems.at(oldRow - 1)) && !(*colItems.at(oldRow - 1) < *item))
575  newRow = oldRow;
576  vit = colItems.insert(vit, item);
577  if (newRow != oldRow) {
578  changed = true;
579  // move the items @ oldRow to newRow
580  int cc = columnCount();
581  QVector<QTableWidgetItem*> rowItems(cc);
582  for (int j = 0; j < cc; ++j)
583  rowItems[j] = newTable.at(tableIndex(oldRow, j));
584  newTable.remove(tableIndex(oldRow, 0), cc);
585  newTable.insert(tableIndex(newRow, 0), cc, 0);
586  for (int j = 0; j < cc; ++j)
587  newTable[tableIndex(newRow, j)] = rowItems.at(j);
588  QTableWidgetItem *header = newVertical.at(oldRow);
589  newVertical.remove(oldRow);
590  newVertical.insert(newRow, header);
591  // update persistent indexes
592  updateRowIndexes(newPersistentIndexes, oldRow, newRow);
593  // the index of the remaining rows may have changed
594  for (int j = i + 1; j < sorting.count(); ++j) {
595  int otherRow = sorting.at(j).second;
596  if (oldRow < otherRow && newRow >= otherRow)
597  --sorting[j].second;
598  else if (oldRow > otherRow && newRow <= otherRow)
599  ++sorting[j].second;
600  }
601  }
602  }
603 
604  if (changed) {
606  tableItems = newTable;
607  verticalHeaderItems = newVertical;
608  changePersistentIndexList(oldPersistentIndexes,
609  newPersistentIndexes);
611  }
612 }
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
static bool itemGreaterThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
long tableIndex(int row, int column) const
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:250
QTableWidgetItem * item(int row, int column) const
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
#define emit
Definition: qobjectdefs.h:76
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
static bool itemLessThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< QTableWidgetItem * > columnItems(int column) const
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
Definition: qtestcase.cpp:2183
bool(* LessThan)(const QPair< QListWidgetItem *, int > &, const QPair< QListWidgetItem *, int > &)
Definition: qlistwidget.cpp:53
static const KeyPair *const end
static QVector< QTableWidgetItem * >::iterator sortedInsertionIterator(const QVector< QTableWidgetItem *>::iterator &begin, const QVector< QTableWidgetItem *>::iterator &end, Qt::SortOrder order, QTableWidgetItem *item)
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ flags()

Qt::ItemFlags QTableModel::flags ( const QModelIndex index) const
virtual

Returns the item flags for the given index.

The base class implementation returns a combination of flags that enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable).

See also
Qt::ItemFlags

Reimplemented from QAbstractItemModel.

Definition at line 481 of file qtablewidget.cpp.

Referenced by QTableWidgetItem::flags().

482 {
483  if (!index.isValid())
484  return Qt::ItemIsDropEnabled;
485  if (QTableWidgetItem *itm = item(index))
486  return itm->flags();
487  return (Qt::ItemIsEditable
493 }
QTableWidgetItem * item(int row, int column) const
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ headerData()

QVariant QTableModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role 
) const
virtual

Returns the data for the given role and section in the header with the specified orientation.

For horizontal headers, the section number corresponds to the column number. Similarly, for vertical headers, the section number corresponds to the row number.

See also
Qt::ItemDataRole, setHeaderData(), QHeaderView

Reimplemented from QAbstractItemModel.

Definition at line 690 of file qtablewidget.cpp.

691 {
692  if (section < 0)
693  return QVariant();
694 
695  QTableWidgetItem *itm = 0;
696  if (orientation == Qt::Horizontal && section < horizontalHeaderItems.count())
697  itm = horizontalHeaderItems.at(section);
698  else if (orientation == Qt::Vertical && section < verticalHeaderItems.count())
699  itm = verticalHeaderItems.at(section);
700  else
701  return QVariant(); // section is out of bounds
702 
703  if (itm)
704  return itm->data(role);
705  if (role == Qt::DisplayRole)
706  return section + 1;
707  return QVariant();
708 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
virtual QVariant data(int role) const
Returns the item&#39;s data for the given role.
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ horizontalHeaderItem()

QTableWidgetItem * QTableModel::horizontalHeaderItem ( int  section)

Definition at line 342 of file qtablewidget.cpp.

Referenced by QTableWidget::setHorizontalHeaderLabels().

343 {
344  return horizontalHeaderItems.value(section);
345 }
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
QVector< QTableWidgetItem * > horizontalHeaderItems

◆ index() [1/2]

QModelIndex QTableModel::index ( int  row,
int  column,
const QModelIndex parent = QModelIndex() 
) const
inlinevirtual

Returns the index of the data in row and column with parent.

See also
parent()

Reimplemented from QAbstractTableModel.

Definition at line 120 of file qtablewidget_p.h.

Referenced by itemChanged(), removeItem(), takeItem(), and updateRowIndexes().

121  { return QAbstractTableModel::index(row, column, parent); }
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.

◆ index() [2/2]

QModelIndex QTableModel::index ( const QTableWidgetItem item) const

Definition at line 352 of file qtablewidget.cpp.

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
QTableWidgetItemPrivate * d
Definition: qtablewidget.h:187
QTableWidgetItem * item(int row, int column) const
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
The QModelIndex class is used to locate data in a data model.
QVector< QTableWidgetItem * > tableItems

◆ insertColumns()

bool QTableModel::insertColumns ( int  column,
int  count = 1,
const QModelIndex parent = QModelIndex() 
)
virtual

On models that support this, inserts count new columns into the model before the given column.

The items in each new column will be children of the item represented by the parent model index.

If column is 0, the columns are prepended to any existing columns.

If column is columnCount(), the columns are appended to any existing columns.

If parent has no children, a single row with count columns is inserted.

Returns true if the columns were successfully inserted; otherwise returns false.

The base class implementation does nothing and returns false.

If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data.

See also
insertRows(), removeColumns(), beginInsertColumns(), endInsertColumns()

Reimplemented from QAbstractItemModel.

Definition at line 82 of file qtablewidget.cpp.

Referenced by setColumnCount().

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);
97  return true;
98 }
void beginInsertColumns(const QModelIndex &parent, int first, int last)
Begins a column insertion operation.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
long tableIndex(int row, int column) const
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QVector< QTableWidgetItem * > horizontalHeaderItems
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
The QModelIndex class is used to locate data in a data model.
void endInsertColumns()
Ends a column insertion operation.
QVector< QTableWidgetItem * > tableItems

◆ insertRows()

bool QTableModel::insertRows ( int  row,
int  count = 1,
const QModelIndex parent = QModelIndex() 
)
virtual

On models that support this, inserts count rows into the model before the given row.

Note
The base class implementation of this function does nothing and returns false.

Items in the new row will be children of the item represented by the parent model index.

If row is 0, the rows are prepended to any existing rows in the parent.

If row is rowCount(), the rows are appended to any existing rows in the parent.

If parent has no children, a single column with count rows is inserted.

Returns true if the rows were successfully inserted; otherwise returns false.

If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data. In either case, you will need to call beginInsertRows() and endInsertRows() to notify other components that the model has changed.

See also
insertColumns(), removeRows(), beginInsertRows(), endInsertRows()

Reimplemented from QAbstractItemModel.

Definition at line 65 of file qtablewidget.cpp.

Referenced by setRowCount().

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
long tableIndex(int row, int column) const
void endInsertRows()
Ends a row insertion operation.
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QVector< QTableWidgetItem * > horizontalHeaderItems
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
The QModelIndex class is used to locate data in a data model.
QVector< QTableWidgetItem * > tableItems
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.

◆ internalMimeData()

QMimeData * QTableModel::internalMimeData ( ) const

Definition at line 812 of file qtablewidget.cpp.

813 {
815 }
QModelIndexList cachedIndexes
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...

◆ isValid()

bool QTableModel::isValid ( const QModelIndex index) const

Definition at line 730 of file qtablewidget.cpp.

Referenced by item().

731 {
732  return (index.isValid()
733  && index.row() < verticalHeaderItems.count()
734  && index.column() < horizontalHeaderItems.count());
735 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
int row() const
Returns the row this model index refers to.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QVector< QTableWidgetItem * > horizontalHeaderItems
int column() const
Returns the column this model index refers to.

◆ item() [1/2]

QTableWidgetItem * QTableModel::item ( int  row,
int  column 
) const

Definition at line 235 of file qtablewidget.cpp.

Referenced by columnItems(), data(), ensureSorted(), flags(), index(), itemData(), mimeData(), setData(), QTableWidgetItem::setFont(), setHorizontalHeaderItem(), setItem(), setItemData(), setItemPrototype(), setVerticalHeaderItem(), and sort().

236 {
237  return tableItems.value(tableIndex(row, column));
238 }
long tableIndex(int row, int column) const
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
QVector< QTableWidgetItem * > tableItems

◆ item() [2/2]

QTableWidgetItem * QTableModel::item ( const QModelIndex index) const

Definition at line 240 of file qtablewidget.cpp.

241 {
242  if (!isValid(index))
243  return 0;
244  return tableItems.at(tableIndex(index.row(), index.column()));
245 }
long tableIndex(int row, int column) const
int row() const
Returns the row this model index refers to.
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool isValid(const QModelIndex &index) const
QVector< QTableWidgetItem * > tableItems
int column() const
Returns the column this model index refers to.

◆ itemChanged()

void QTableModel::itemChanged ( QTableWidgetItem item)

Definition at line 768 of file qtablewidget.cpp.

Referenced by setItemData().

769 {
770  if (!item)
771  return;
772  if (item->flags() & ItemIsHeaderItem) {
773  int row = verticalHeaderItems.indexOf(item);
774  if (row >= 0) {
776  } else {
777  int column = horizontalHeaderItems.indexOf(item);
778  if (column >= 0)
779  emit headerDataChanged(Qt::Horizontal, column, column);
780  }
781  } else {
782  QModelIndex idx = index(item);
783  if (idx.isValid())
784  emit dataChanged(idx, idx);
785  }
786 }
QVector< QTableWidgetItem * > verticalHeaderItems
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
#define emit
Definition: qobjectdefs.h:76
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QVector< QTableWidgetItem * > horizontalHeaderItems
Qt::ItemFlags flags() const
Returns the flags used to describe the item.
Definition: qtablewidget.h:104
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
The QModelIndex class is used to locate data in a data model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.

◆ itemData()

QMap< int, QVariant > QTableModel::itemData ( const QModelIndex index) const
virtual

Returns a map with values for all predefined roles in the model for the item at the given index.

Reimplement this function if you want to extend the default behavior of this function to include custom roles in the map.

See also
Qt::ItemDataRole, data()

Reimplemented from QAbstractItemModel.

Definition at line 435 of file qtablewidget.cpp.

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QWidgetItemData > values
Definition: qtablewidget.h:185
QTableWidgetItem * item(int row, int column) const
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
Definition: qmap.h:559
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ itemGreaterThan()

bool QTableModel::itemGreaterThan ( const QPair< QTableWidgetItem *, int > &  left,
const QPair< QTableWidgetItem *, int > &  right 
)
static

Definition at line 684 of file qtablewidget.cpp.

Referenced by ensureSorted(), and sort().

686 {
687  return (*(right.first) < *(left .first));
688 }
T1 first
Definition: qpair.h:65

◆ itemLessThan()

bool QTableModel::itemLessThan ( const QPair< QTableWidgetItem *, int > &  left,
const QPair< QTableWidgetItem *, int > &  right 
)
static

Definition at line 678 of file qtablewidget.cpp.

Referenced by ensureSorted(), and sort().

680 {
681  return *(left.first) < *(right.first);
682 }
T1 first
Definition: qpair.h:65

◆ itemPrototype()

const QTableWidgetItem * QTableModel::itemPrototype ( ) const

Definition at line 793 of file qtablewidget.cpp.

794 {
795  return prototype;
796 }
const QTableWidgetItem * prototype

◆ mimeData()

QMimeData * QTableModel::mimeData ( const QModelIndexList indexes) const
virtual

Returns an object that contains serialized items of data corresponding to the list of indexes specified.

The formats used to describe the encoded data is obtained from the mimeTypes() function.

If the list of indexes is empty, or there are no supported MIME types, 0 is returned rather than a serialized empty list.

See also
mimeTypes(), dropMimeData()

Reimplemented from QAbstractItemModel.

Definition at line 817 of file qtablewidget.cpp.

818 {
820  for (int i = 0; i < indexes.count(); ++i)
821  items << item(indexes.at(i));
822  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
823 
824  // cachedIndexes is a little hack to avoid copying from QModelIndexList to
825  // QList<QTreeWidgetItem*> and back again in the view
826  cachedIndexes = indexes;
827  QMimeData *mimeData = (view ? view->mimeData(items) : 0);
828  cachedIndexes.clear();
829  return mimeData;
830 }
void clear()
Removes all the MIME type and data entries in the object.
Definition: qmimedata.cpp:613
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTableWidgetItem * item(int row, int column) const
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
virtual QMimeData * mimeData(const QList< QTableWidgetItem *> items) const
Returns an object that contains a serialized description of the specified items.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QModelIndexList cachedIndexes
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ mimeTypes()

QStringList QTableModel::mimeTypes ( ) const
virtual

Returns a list of MIME types that can be used to describe a list of model indexes.

See also
mimeData()

Reimplemented from QAbstractItemModel.

Definition at line 806 of file qtablewidget.cpp.

807 {
808  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
809  return (view ? view->mimeTypes() : QStringList());
810 }
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of tablewidget items.

◆ removeColumns()

bool QTableModel::removeColumns ( int  column,
int  count = 1,
const QModelIndex parent = QModelIndex() 
)
virtual

On models that support this, removes count columns starting with the given column under parent parent from the model.

Returns true if the columns were successfully removed; otherwise returns false.

The base class implementation does nothing and returns false.

If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.

See also
removeColumn(), removeRows(), insertColumns(), beginRemoveColumns(), endRemoveColumns()

Reimplemented from QAbstractItemModel.

Definition at line 127 of file qtablewidget.cpp.

Referenced by setColumnCount().

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);
152  return true;
153 }
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
void beginRemoveColumns(const QModelIndex &parent, int first, int last)
Begins a column removal operation.
QTableWidget * view
Definition: qtablewidget.h:186
long tableIndex(int row, int column) const
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QModelIndex class is used to locate data in a data model.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
void endRemoveColumns()
Ends a column removal operation.

◆ removeItem()

void QTableModel::removeItem ( QTableWidgetItem item)

Definition at line 247 of file qtablewidget.cpp.

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;
262  return;
263  }
264  i = horizontalHeaderItems.indexOf(item);
265  if (i != -1) {
266  horizontalHeaderItems[i] = 0;
268  return;
269  }
270 }
QVector< QTableWidgetItem * > verticalHeaderItems
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
#define emit
Definition: qobjectdefs.h:76
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698
QVector< QTableWidgetItem * > horizontalHeaderItems
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
The QModelIndex class is used to locate data in a data model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
QVector< QTableWidgetItem * > tableItems

◆ removeRows()

bool QTableModel::removeRows ( int  row,
int  count = 1,
const QModelIndex parent = QModelIndex() 
)
virtual

On models that support this, removes count rows starting with the given row under parent parent from the model.

Returns true if the rows were successfully removed; otherwise returns false.

The base class implementation does nothing and returns false.

If you implement your own model, you can reimplement this function if you want to support removing. Alternatively, you can provide your own API for altering the data.

See also
removeRow(), removeColumns(), insertColumns(), beginRemoveRows(), endRemoveRows()

Reimplemented from QAbstractItemModel.

Definition at line 100 of file qtablewidget.cpp.

Referenced by setRowCount().

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 }
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
QTableWidget * view
Definition: qtablewidget.h:186
long tableIndex(int row, int column) const
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void endRemoveRows()
Ends a row removal operation.
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
Begins a row removal operation.
The QModelIndex class is used to locate data in a data model.
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ rowCount()

int QTableModel::rowCount ( const QModelIndex parent = QModelIndex()) const
virtual

Returns the number of rows under the given parent.

When the parent is valid it means that rowCount is returning the number of children of parent.

Note
When implementing a table based model, rowCount() should return 0 when the parent is valid.
See also
columnCount()

Implements QAbstractItemModel.

Definition at line 392 of file qtablewidget.cpp.

Referenced by columnItems(), dropMimeData(), removeColumns(), QTableWidget::setVerticalHeaderLabels(), and sort().

393 {
394  return parent.isValid() ? 0 : verticalHeaderItems.count();
395 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
bool isValid() const
Returns true if this model index is valid; otherwise returns false.

◆ setColumnCount()

void QTableModel::setColumnCount ( int  columns)

Definition at line 381 of file qtablewidget.cpp.

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
bool removeColumns(int column, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, removes count columns starting with the given column under parent parent...
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QVector< QTableWidgetItem * > horizontalHeaderItems
bool insertColumns(int column, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count new columns into the model before the given column...

◆ setData()

bool QTableModel::setData ( const QModelIndex index,
const QVariant value,
int  role 
)
virtual

Sets the role data for the item at index to value.

Returns true if successful; otherwise returns false.

The dataChanged() signal should be emitted if the data was successfully set.

The base class implementation returns false. This function and data() must be reimplemented for editable models.

See also
Qt::ItemDataRole, data(), itemData()

Reimplemented from QAbstractItemModel.

Definition at line 410 of file qtablewidget.cpp.

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 
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 }
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTableWidgetItem * item(int row, int column) const
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
int row() const
Returns the row this model index refers to.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QTableWidgetItem * createItem() const
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
virtual void setData(int role, const QVariant &value)
Sets the item&#39;s data for the given role to the specified value.
int column() const
Returns the column this model index refers to.

◆ setHeaderData()

bool QTableModel::setHeaderData ( int  section,
Qt::Orientation  orientation,
const QVariant value,
int  role 
)
virtual

Sets the data for the given role and section in the header with the specified orientation to the value supplied.

Returns true if the header's data was updated; otherwise returns false.

When reimplementing this function, the headerDataChanged() signal must be emitted explicitly.

See also
Qt::ItemDataRole, headerData()

Reimplemented from QAbstractItemModel.

Definition at line 710 of file qtablewidget.cpp.

712 {
713  if (section < 0 ||
714  (orientation == Qt::Horizontal && horizontalHeaderItems.size() <= section) ||
715  (orientation == Qt::Vertical && verticalHeaderItems.size() <= section))
716  return false;
717 
718  QTableWidgetItem *itm = 0;
719  if (orientation == Qt::Horizontal)
720  itm = horizontalHeaderItems.at(section);
721  else
722  itm = verticalHeaderItems.at(section);
723  if (itm) {
724  itm->setData(role, value);
725  return true;
726  }
727  return false;
728 }
QVector< QTableWidgetItem * > verticalHeaderItems
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
virtual void setData(int role, const QVariant &value)
Sets the item&#39;s data for the given role to the specified value.

◆ setHorizontalHeaderItem()

void QTableModel::setHorizontalHeaderItem ( int  section,
QTableWidgetItem item 
)

Definition at line 272 of file qtablewidget.cpp.

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 
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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
QTableWidget * view
Definition: qtablewidget.h:186
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTableWidgetItem * item(int row, int column) const
#define emit
Definition: qobjectdefs.h:76
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::ItemFlags itemFlags
Definition: qtablewidget.h:188

◆ setItem()

void QTableModel::setItem ( int  row,
int  column,
QTableWidgetItem item 
)

Definition at line 155 of file qtablewidget.cpp.

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 
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
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 {
189  it = sortedInsertionIterator(colItems.begin(), colItems.end(), order, item);
190  sortedRow = qMax((int)(it - colItems.begin()), 0);
191  }
192  if (sortedRow != row) {
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);
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 
214  return;
215  }
216  }
217  QModelIndex idx = QAbstractTableModel::index(row, column);
218  emit dataChanged(idx, idx);
219 }
bool isSortingEnabled() const
QModelIndexList persistentIndexList() const
Returns the list of indexes stored as persistent indexes in the model.
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
#define it(className, varName)
int sortIndicatorSection() const
Returns the logical index of the section that has a sort indicator.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
QHeaderView * horizontalHeader() const
Returns the table view&#39;s horizontal header.
QTableWidget * view
Definition: qtablewidget.h:186
long tableIndex(int row, int column) const
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:250
QTableWidgetItemPrivate * d
Definition: qtablewidget.h:187
QTableWidgetItem * item(int row, int column) const
SortOrder
Definition: qnamespace.h:189
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
#define emit
Definition: qobjectdefs.h:76
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
QVector< QTableWidgetItem * > columnItems(int column) const
void insert(int i, const T &t)
Inserts value at index position i in the vector.
Definition: qvector.h:362
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
The QModelIndex class is used to locate data in a data model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
static QVector< QTableWidgetItem * >::iterator sortedInsertionIterator(const QVector< QTableWidgetItem *>::iterator &begin, const QVector< QTableWidgetItem *>::iterator &end, Qt::SortOrder order, QTableWidgetItem *item)
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::SortOrder sortIndicatorOrder() const
Returns the order for the sort indicator.

◆ setItemData()

bool QTableModel::setItemData ( const QModelIndex index,
const QMap< int, QVariant > &  roles 
)
virtual

Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole.

Returns true if successful; otherwise returns false.

Roles that are not in roles will not be modified.

See also
setData() data() itemData()

Reimplemented from QAbstractItemModel.

Definition at line 449 of file qtablewidget.cpp.

450 {
451  if (!index.isValid())
452  return false;
453 
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 }
#define it(className, varName)
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTableWidgetItem * item(int row, int column) const
void setItem(int row, int column, QTableWidgetItem *item)
Sets the item for the given row and column to item.
int row() const
Returns the row this model index refers to.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the map.
Definition: qmap.h:374
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
Definition: qmap.h:301
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the map...
Definition: qmap.h:380
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QTableWidgetItem * createItem() const
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
int column() const
Returns the column this model index refers to.
void itemChanged(QTableWidgetItem *item)

◆ setItemPrototype()

void QTableModel::setItemPrototype ( const QTableWidgetItem item)

Definition at line 798 of file qtablewidget.cpp.

799 {
800  if (prototype != item) {
801  delete prototype;
802  prototype = item;
803  }
804 }
QTableWidgetItem * item(int row, int column) const
const QTableWidgetItem * prototype

◆ setRowCount()

void QTableModel::setRowCount ( int  rows)

Definition at line 370 of file qtablewidget.cpp.

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 }
bool insertRows(int row, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, inserts count rows into the model before the given row.
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
bool removeRows(int row, int count=1, const QModelIndex &parent=QModelIndex())
On models that support this, removes count rows starting with the given row under parent parent from ...
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217

◆ setVerticalHeaderItem()

void QTableModel::setVerticalHeaderItem ( int  section,
QTableWidgetItem item 
)

Definition at line 294 of file qtablewidget.cpp.

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 
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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
QTableWidget * view
Definition: qtablewidget.h:186
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTableWidgetItem * item(int row, int column) const
#define emit
Definition: qobjectdefs.h:76
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::ItemFlags itemFlags
Definition: qtablewidget.h:188

◆ sort()

void QTableModel::sort ( int  column,
Qt::SortOrder  order 
)
virtual

Sorts the model by column in the given order.

The base class implementation does nothing.

Reimplemented from QAbstractItemModel.

Definition at line 495 of file qtablewidget.cpp.

496 {
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 
511  qStableSort(sortable.begin(), sortable.end(), compare);
512 
514  QModelIndexList from;
515  QModelIndexList to;
516  for (int i = 0; i < rowCount(); ++i) {
517  int r = (i < sortable.count()
518  ? sortable.at(i).second
519  : unsortable.at(i - sortable.count()));
520  for (int c = 0; c < columnCount(); ++c) {
521  sorted_table[tableIndex(i, c)] = item(r, c);
522  from.append(createIndex(r, c, 0));
523  to.append(createIndex(i, c, 0));
524  }
525  }
526 
528 
529  tableItems = sorted_table;
530  changePersistentIndexList(from, to); // ### slow
531 
533 }
unsigned char c[8]
Definition: qnumeric_p.h:62
static bool itemGreaterThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
long tableIndex(int row, int column) const
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
void changePersistentIndexList(const QModelIndexList &from, const QModelIndexList &to)
Changes the QPersistentModelIndexes that is equal to the indexes in the given from model index list t...
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:250
QTableWidgetItem * item(int row, int column) const
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
void layoutAboutToBeChanged()
This signal is emitted just before the layout of a model is changed.
#define emit
Definition: qobjectdefs.h:76
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
static bool itemLessThan(const QPair< QTableWidgetItem *, int > &left, const QPair< QTableWidgetItem *, int > &right)
void layoutChanged()
This signal is emitted whenever the layout of items exposed by the model has changed; for example...
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
void qStableSort(RandomAccessIterator start, RandomAccessIterator end)
Definition: qalgorithms.h:202
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns for the children of the given parent.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of rows under the given parent.
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
bool(* LessThan)(const QPair< QListWidgetItem *, int > &, const QPair< QListWidgetItem *, int > &)
Definition: qlistwidget.cpp:53
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ sortedInsertionIterator()

QVector< QTableWidgetItem * >::iterator QTableModel::sortedInsertionIterator ( const QVector< QTableWidgetItem *>::iterator &  begin,
const QVector< QTableWidgetItem *>::iterator &  end,
Qt::SortOrder  order,
QTableWidgetItem item 
)
static

Definition at line 668 of file qtablewidget.cpp.

Referenced by ensureSorted(), and setItem().

672 {
673  if (order == Qt::AscendingOrder)
674  return qLowerBound(begin, end, item, QTableModelLessThan());
675  return qLowerBound(begin, end, item, QTableModelGreaterThan());
676 }
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qLowerBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
Definition: qalgorithms.h:227

◆ supportedDropActions()

Qt::DropActions QTableModel::supportedDropActions ( ) const
virtual

Returns the drop actions supported by this model.

Since
4.2

The default implementation returns Qt::CopyAction. Reimplement this function if you wish to support additional actions. You must also reimplement the dropMimeData() function to handle the additional operations.

See also
dropMimeData(), Qt::DropActions, {Using drag and drop with item views}

Reimplemented from QAbstractItemModel.

Definition at line 847 of file qtablewidget.cpp.

848 {
849  const QTableWidget *view = qobject_cast<const QTableWidget*>(QObject::parent());
850  return (view ? view->supportedDropActions() : Qt::DropActions(Qt::IgnoreAction));
851 }
virtual Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this view.
The QTableWidget class provides an item-based table view with a default model.
Definition: qtablewidget.h:220
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273

◆ tableIndex()

long QTableModel::tableIndex ( int  row,
int  column 
) const
inline

Definition at line 157 of file qtablewidget_p.h.

Referenced by ensureSorted(), insertColumns(), insertRows(), item(), removeColumns(), removeRows(), setItem(), sort(), and takeItem().

158  { return (row * horizontalHeaderItems.count()) + column; }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > horizontalHeaderItems

◆ takeHorizontalHeaderItem()

QTableWidgetItem * QTableModel::takeHorizontalHeaderItem ( int  section)

Definition at line 316 of file qtablewidget.cpp.

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QTableWidget * view
Definition: qtablewidget.h:186
QVector< QTableWidgetItem * > horizontalHeaderItems
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::ItemFlags itemFlags
Definition: qtablewidget.h:188

◆ takeItem()

QTableWidgetItem * QTableModel::takeItem ( int  row,
int  column 
)

Definition at line 221 of file qtablewidget.cpp.

222 {
223  long i = tableIndex(row, column);
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 }
QTableWidget * view
Definition: qtablewidget.h:186
long tableIndex(int row, int column) const
QTableWidgetItemPrivate * d
Definition: qtablewidget.h:187
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559
#define emit
Definition: qobjectdefs.h:76
The QModelIndex class is used to locate data in a data model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
QVector< QTableWidgetItem * > tableItems
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82

◆ takeVerticalHeaderItem()

QTableWidgetItem * QTableModel::takeVerticalHeaderItem ( int  section)

Definition at line 329 of file qtablewidget.cpp.

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 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QTableWidgetItem * > verticalHeaderItems
QTableWidget * view
Definition: qtablewidget.h:186
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
The QTableWidgetItem class provides an item for use with the QTableWidget class.
Definition: qtablewidget.h:82
Qt::ItemFlags itemFlags
Definition: qtablewidget.h:188

◆ updateRowIndexes()

void QTableModel::updateRowIndexes ( QModelIndexList indexes,
int  movedFromRow,
int  movedToRow 
)

Definition at line 643 of file qtablewidget.cpp.

Referenced by ensureSorted(), and setItem().

645 {
647  for (it = indexes.begin(); it != indexes.end(); ++it) {
648  int oldRow = (*it).row();
649  int newRow = oldRow;
650  if (oldRow == movedFromRow)
651  newRow = movedToRow;
652  else if (movedFromRow < oldRow && movedToRow >= oldRow)
653  newRow = oldRow - 1;
654  else if (movedFromRow > oldRow && movedToRow <= oldRow)
655  newRow = oldRow + 1;
656  if (newRow != oldRow)
657  *it = index(newRow, (*it).column(), (*it).parent());
658  }
659 }
#define it(className, varName)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
friend class iterator
Definition: qlist.h:226
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
Definition: qtestcase.cpp:2183

◆ verticalHeaderItem()

QTableWidgetItem * QTableModel::verticalHeaderItem ( int  section)

Definition at line 347 of file qtablewidget.cpp.

Referenced by QTableWidget::setVerticalHeaderLabels().

348 {
349  return verticalHeaderItems.value(section);
350 }
QVector< QTableWidgetItem * > verticalHeaderItems
T value(int i) const
Returns the value at index position i in the vector.
Definition: qvector.h:559

Properties

◆ cachedIndexes

QModelIndexList QTableModel::cachedIndexes
mutableprivate

Definition at line 184 of file qtablewidget_p.h.

Referenced by internalMimeData(), and mimeData().

◆ horizontalHeaderItems

QVector<QTableWidgetItem*> QTableModel::horizontalHeaderItems
private

◆ prototype

const QTableWidgetItem* QTableModel::prototype
private

Definition at line 178 of file qtablewidget_p.h.

Referenced by createItem(), itemPrototype(), setItemPrototype(), and ~QTableModel().

◆ tableItems

QVector<QTableWidgetItem*> QTableModel::tableItems
private

◆ verticalHeaderItems

QVector<QTableWidgetItem*> QTableModel::verticalHeaderItems
private

The documentation for this class was generated from the following files: