Qt 4.8
Signals | Public Functions | Protected Functions | Properties | Friends | List of all members
QStandardItemModel Class Reference

The QStandardItemModel class provides a generic model for storing custom data. More...

#include <qstandarditemmodel.h>

Inheritance diagram for QStandardItemModel:
QAbstractItemModel QObject QUrlModel

Signals

void itemChanged (QStandardItem *item)
 
- 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...
 

Public Functions

void appendColumn (const QList< QStandardItem *> &items)
 Appends a column containing items. More...
 
void appendRow (const QList< QStandardItem *> &items)
 Appends a row containing items. More...
 
void appendRow (QStandardItem *item)
 
void clear ()
 Removes all items (including header items) from the model and sets the number of rows and columns to zero. More...
 
int columnCount (const QModelIndex &parent=QModelIndex()) const
 Reimplemented Function More...
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const
 Reimplemented Function More...
 
bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
 Reimplemented Function More...
 
QList< QStandardItem * > findItems (const QString &text, Qt::MatchFlags flags=Qt::MatchExactly, int column=0) const
 Returns a list of items that match the given text, using the given flags, in the given column. More...
 
Qt::ItemFlags flags (const QModelIndex &index) const
 Reimplemented Function More...
 
bool hasChildren (const QModelIndex &parent=QModelIndex()) const
 Reimplemented Function More...
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
 Reimplemented Function More...
 
QStandardItemhorizontalHeaderItem (int column) const
 Returns the horizontal header item for column if one has been set; otherwise returns 0. More...
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const
 Reimplemented Function More...
 
QModelIndex indexFromItem (const QStandardItem *item) const
 Returns the QModelIndex associated with the given item. More...
 
void insertColumn (int column, const QList< QStandardItem *> &items)
 Inserts a column at column containing items. 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 insertColumns (int column, int count, const QModelIndex &parent=QModelIndex())
 Reimplemented Function More...
 
void insertRow (int row, const QList< QStandardItem *> &items)
 Inserts a row at row containing items. More...
 
void insertRow (int row, QStandardItem *item)
 
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...
 
bool insertRows (int row, int count, const QModelIndex &parent=QModelIndex())
 Reimplemented Function More...
 
QStandardIteminvisibleRootItem () const
 Returns the model's invisible root item. More...
 
QStandardItemitem (int row, int column=0) const
 Returns the item for the given row and column if one has been set; otherwise returns 0. More...
 
QMap< int, QVariantitemData (const QModelIndex &index) const
 Reimplemented Function More...
 
QStandardItemitemFromIndex (const QModelIndex &index) const
 Returns a pointer to the QStandardItem associated with the given index. More...
 
const QStandardItemitemPrototype () const
 Returns the item prototype used by the model. More...
 
QMimeDatamimeData (const QModelIndexList &indexes) const
 Reimplemented Function More...
 
QStringList mimeTypes () const
 Reimplemented Function More...
 
QModelIndex parent (const QModelIndex &child) const
 Reimplemented Function More...
 
 QStandardItemModel (QObject *parent=0)
 Constructs a new item model with the given parent. More...
 
 QStandardItemModel (int rows, int columns, QObject *parent=0)
 Constructs a new item model that initially has rows rows and columns columns, and that has the given parent. More...
 
bool removeColumns (int column, int count, const QModelIndex &parent=QModelIndex())
 Reimplemented Function More...
 
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex())
 Reimplemented Function More...
 
int rowCount (const QModelIndex &parent=QModelIndex()) const
 Reimplemented Function More...
 
void setColumnCount (int columns)
 Sets the number of columns in this model to columns. More...
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
 Reimplemented Function More...
 
bool setHeaderData (int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
 Reimplemented Function More...
 
void setHorizontalHeaderItem (int column, QStandardItem *item)
 Sets the horizontal header item for column to item. More...
 
void setHorizontalHeaderLabels (const QStringList &labels)
 Sets the horizontal header labels using labels. More...
 
void setItem (int row, int column, QStandardItem *item)
 Sets the item for the given row and column to item. More...
 
void setItem (int row, QStandardItem *item)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool setItemData (const QModelIndex &index, const QMap< int, QVariant > &roles)
 Reimplemented Function More...
 
void setItemPrototype (const QStandardItem *item)
 Sets the item prototype for the model to the specified item. More...
 
void setRowCount (int rows)
 Sets the number of rows in this model to rows. More...
 
void setSortRole (int role)
 
void setVerticalHeaderItem (int row, QStandardItem *item)
 Sets the vertical header item for row to item. More...
 
void setVerticalHeaderLabels (const QStringList &labels)
 Sets the vertical header labels using labels. More...
 
void sort (int column, Qt::SortOrder order=Qt::AscendingOrder)
 Reimplemented Function More...
 
int sortRole () const
 
Qt::DropActions supportedDropActions () const
 QStandardItemModel supports both copy and move. More...
 
QList< QStandardItem * > takeColumn (int column)
 Removes the given column without deleting the column items, and returns a list of pointers to the removed items. More...
 
QStandardItemtakeHorizontalHeaderItem (int column)
 Removes the horizontal header item at column from the header without deleting it, and returns a pointer to the item. More...
 
QStandardItemtakeItem (int row, int column=0)
 Removes the item at (row, column) without deleting it. More...
 
QList< QStandardItem * > takeRow (int row)
 Removes the given row without deleting the row items, and returns a list of pointers to the removed items. More...
 
QStandardItemtakeVerticalHeaderItem (int row)
 Removes the vertical header item at row from the header without deleting it, and returns a pointer to the item. More...
 
QStandardItemverticalHeaderItem (int row) const
 Returns the vertical header item for row row if one has been set; otherwise returns 0. More...
 
 ~QStandardItemModel ()
 Destructs the 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...
 

Protected Functions

 QStandardItemModel (QStandardItemModelPrivate &dd, QObject *parent=0)
 
- 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...
 

Properties

int sortRole
 the item role that is used to query the model's data when sorting items More...
 

Friends

class QStandardItem
 
class QStandardItemPrivate
 

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...
 
- 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)
 
- 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 Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

The QStandardItemModel class provides a generic model for storing custom data.

QStandardItemModel can be used as a repository for standard Qt data types. It is one of the Model/View Classes and is part of Qt's Model/View Programming framework.

QStandardItemModel provides a classic item-based approach to working with the model. The items in a QStandardItemModel are provided by QStandardItem.

QStandardItemModel implements the QAbstractItemModel interface, which means that the model can be used to provide data in any view that supports that interface (such as QListView, QTableView and QTreeView, and your own custom views). For performance and flexibility, you may want to subclass QAbstractItemModel to provide support for different kinds of data repositories. For example, the QDirModel provides a model interface to the underlying file system.

When you want a list or tree, you typically create an empty QStandardItemModel and use appendRow() to add items to the model, and item() to access an item. If your model represents a table, you typically pass the dimensions of the table to the QStandardItemModel constructor and use setItem() to position items into the table. You can also use setRowCount() and setColumnCount() to alter the dimensions of the model. To insert items, use insertRow() or insertColumn(), and to remove items, use removeRow() or removeColumn().

You can set the header labels of your model with setHorizontalHeaderLabels() and setVerticalHeaderLabels().

You can search for items in the model with findItems(), and sort the model by calling sort().

Call clear() to remove all items from the model.

An example usage of QStandardItemModel to create a table:

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

An example usage of QStandardItemModel to create a tree:

QStandardItem *parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i) {
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
parentItem->appendRow(item);
parentItem = item;
}

After setting the model on a view, you typically want to react to user actions, such as an item being clicked. Since a QAbstractItemView provides QModelIndex-based signals and functions, you need a way to obtain the QStandardItem that corresponds to a given QModelIndex, and vice versa. itemFromIndex() and indexFromItem() provide this mapping. Typical usage of itemFromIndex() includes obtaining the item at the current index in a view, and obtaining the item that corresponds to an index carried by a QAbstractItemView signal, such as QAbstractItemView::clicked(). First you connect the view's signal to a slot in your class:

QTreeView *treeView = new QTreeView(this);
treeView->setModel(myStandardItemModel);
connect(treeView, SIGNAL(clicked(QModelIndex)),
this, SLOT(clicked(QModelIndex)));

When you receive the signal, you call itemFromIndex() on the given model index to get a pointer to the item:

void MyWidget::clicked(const QModelIndex &index)
{
QStandardItem *item = myStandardItemModel->itemFromIndex(index);
// Do stuff with the item ...
}

Conversely, you must obtain the QModelIndex of an item when you want to invoke a model/view function that takes an index as argument. You can obtain the index either by using the model's indexFromItem() function, or, equivalently, by calling QStandardItem::index():

treeView->scrollTo(item->index());

You are, of course, not required to use the item-based approach; you could instead rely entirely on the QAbstractItemModel interface when working with the model, or use a combination of the two as appropriate.

See also
QStandardItem, {Model/View Programming}, QAbstractItemModel, {itemviews/simpletreemodel}{Simple Tree Model example}, {Item View Convenience Classes}

Definition at line 318 of file qstandarditemmodel.h.

Constructors and Destructors

◆ QStandardItemModel() [1/3]

QStandardItemModel::QStandardItemModel ( QObject parent = 0)
explicit

Constructs a new item model with the given parent.

Definition at line 2162 of file qstandarditemmodel.cpp.

2164 {
2166  d->init();
2167  d->root->d_func()->setModel(this);
2168 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QAbstractItemModel(QObject *parent=0)
Constructs an abstract item model with the given parent.

◆ QStandardItemModel() [2/3]

QStandardItemModel::QStandardItemModel ( int  rows,
int  columns,
QObject parent = 0 
)

Constructs a new item model that initially has rows rows and columns columns, and that has the given parent.

Definition at line 2174 of file qstandarditemmodel.cpp.

2176 {
2178  d->init();
2179  d->root->insertColumns(0, columns);
2180  d->columnHeaderItems.insert(0, columns, 0);
2181  d->root->insertRows(0, rows);
2182  d->rowHeaderItems.insert(0, rows, 0);
2183  d->root->d_func()->setModel(this);
2184 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QAbstractItemModel(QObject *parent=0)
Constructs an abstract item model with the given parent.

◆ ~QStandardItemModel()

QStandardItemModel::~QStandardItemModel ( )

Destructs the model.

The model destroys all its items.

Definition at line 2199 of file qstandarditemmodel.cpp.

2200 {
2201 }

◆ QStandardItemModel() [3/3]

QStandardItemModel::QStandardItemModel ( QStandardItemModelPrivate dd,
QObject parent = 0 
)
protected
Warning
This function is not part of the public interface.

Definition at line 2189 of file qstandarditemmodel.cpp.

2190  : QAbstractItemModel(dd, parent)
2191 {
2193  d->init();
2194 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QAbstractItemModel(QObject *parent=0)
Constructs an abstract item model with the given parent.

Functions

◆ appendColumn()

void QStandardItemModel::appendColumn ( const QList< QStandardItem *> &  items)

Appends a column containing items.

Since
4.2

If necessary, the row count is increased to the size of items.

See also
insertColumn(), appendRow()

Definition at line 2651 of file qstandarditemmodel.cpp.

2652 {
2653  invisibleRootItem()->appendColumn(items);
2654 }
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
void appendColumn(const QList< QStandardItem *> &items)
Appends a column containing items.

◆ appendRow() [1/2]

void QStandardItemModel::appendRow ( const QList< QStandardItem *> &  items)

Appends a row containing items.

Since
4.2

If necessary, the column count is increased to the size of items.

See also
insertRow(), appendColumn()

Definition at line 2635 of file qstandarditemmodel.cpp.

2636 {
2637  invisibleRootItem()->appendRow(items);
2638 }
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
void appendRow(const QList< QStandardItem *> &items)
Appends a row containing items.

◆ appendRow() [2/2]

void QStandardItemModel::appendRow ( QStandardItem item)
inline
Since
4.2

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

When building a list or a tree that has only one column, this function provides a convenient way to append a single new item.

Definition at line 434 of file qstandarditemmodel.h.

435 { appendRow(QList<QStandardItem*>() << aitem); }
void appendRow(const QList< QStandardItem *> &items)
Appends a row containing items.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ clear()

void QStandardItemModel::clear ( )

Removes all items (including header items) from the model and sets the number of rows and columns to zero.

See also
removeColumns(), removeRows()

Definition at line 2209 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::setFileSystemModel().

2210 {
2212  d->root.reset(new QStandardItem);
2213  d->root->d_func()->setModel(this);
2214  qDeleteAll(d->columnHeaderItems);
2215  d->columnHeaderItems.clear();
2216  qDeleteAll(d->rowHeaderItems);
2217  d->rowHeaderItems.clear();
2218  reset();
2219 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
The QStandardItem class provides an item for use with the QStandardItemModel class.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
void reset()
Resets the model to its original state in any attached views.

◆ columnCount()

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

Reimplemented Function

Implements QAbstractItemModel.

Definition at line 2841 of file qstandarditemmodel.cpp.

Referenced by dropMimeData(), headerData(), horizontalHeaderItem(), setHeaderData(), setHorizontalHeaderItem(), setHorizontalHeaderLabels(), and takeHorizontalHeaderItem().

2842 {
2843  Q_D(const QStandardItemModel);
2844  QStandardItem *item = d->itemFromIndex(parent);
2845  return item ? item->columnCount() : 0;
2846 }
double d
Definition: qnumeric_p.h:62
int columnCount() const
Returns the number of child item columns that the item has.
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ data()

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

Reimplemented Function

Implements QAbstractItemModel.

Definition at line 2851 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::addUrls(), QUrlModel::mimeData(), mimeData(), and QUrlModel::urls().

2852 {
2853  Q_D(const QStandardItemModel);
2854  QStandardItem *item = d->itemFromIndex(index);
2855  return item ? item->data(role) : QVariant();
2856 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QVariant data(int role=Qt::UserRole+1) const
Returns the item&#39;s data for the given role, or an invalid QVariant if there is no data for the role...
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ dropMimeData()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3199 of file qstandarditemmodel.cpp.

3201 {
3203  // check if the action is supported
3204  if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
3205  return false;
3206  // check if the format is supported
3207  QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
3208  if (!data->hasFormat(format))
3209  return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
3210 
3211  if (row > rowCount(parent))
3212  row = rowCount(parent);
3213  if (row == -1)
3214  row = rowCount(parent);
3215  if (column == -1)
3216  column = 0;
3217 
3218  // decode and insert
3219  QByteArray encoded = data->data(format);
3221 
3222 
3223  //code based on QAbstractItemModel::decodeData
3224  // adapted to work with QStandardItem
3225  int top = INT_MAX;
3226  int left = INT_MAX;
3227  int bottom = 0;
3228  int right = 0;
3229  QVector<int> rows, columns;
3231 
3232  while (!stream.atEnd()) {
3233  int r, c;
3234  QStandardItem *item = d->createItem();
3235  stream >> r >> c;
3236  d->decodeDataRecursive(stream, item);
3237 
3238  rows.append(r);
3239  columns.append(c);
3240  items.append(item);
3241  top = qMin(r, top);
3242  left = qMin(c, left);
3243  bottom = qMax(r, bottom);
3244  right = qMax(c, right);
3245  }
3246 
3247  // insert the dragged items into the table, use a bit array to avoid overwriting items,
3248  // since items from different tables can have the same row and column
3249  int dragRowCount = 0;
3250  int dragColumnCount = right - left + 1;
3251 
3252  // Compute the number of continuous rows upon insertion and modify the rows to match
3253  QVector<int> rowsToInsert(bottom + 1);
3254  for (int i = 0; i < rows.count(); ++i)
3255  rowsToInsert[rows.at(i)] = 1;
3256  for (int i = 0; i < rowsToInsert.count(); ++i) {
3257  if (rowsToInsert[i] == 1){
3258  rowsToInsert[i] = dragRowCount;
3259  ++dragRowCount;
3260  }
3261  }
3262  for (int i = 0; i < rows.count(); ++i)
3263  rows[i] = top + rowsToInsert[rows[i]];
3264 
3265  QBitArray isWrittenTo(dragRowCount * dragColumnCount);
3266 
3267  // make space in the table for the dropped data
3268  int colCount = columnCount(parent);
3269  if (colCount < dragColumnCount + column) {
3270  insertColumns(colCount, dragColumnCount + column - colCount, parent);
3271  colCount = columnCount(parent);
3272  }
3273  insertRows(row, dragRowCount, parent);
3274 
3275  row = qMax(0, row);
3276  column = qMax(0, column);
3277 
3278  QStandardItem *parentItem = itemFromIndex (parent);
3279  if (!parentItem)
3280  parentItem = invisibleRootItem();
3281 
3282  QVector<QPersistentModelIndex> newIndexes(items.size());
3283  // set the data in the table
3284  for (int j = 0; j < items.size(); ++j) {
3285  int relativeRow = rows.at(j) - top;
3286  int relativeColumn = columns.at(j) - left;
3287  int destinationRow = relativeRow + row;
3288  int destinationColumn = relativeColumn + column;
3289  int flat = (relativeRow * dragColumnCount) + relativeColumn;
3290  // if the item was already written to, or we just can't fit it in the table, create a new row
3291  if (destinationColumn >= colCount || isWrittenTo.testBit(flat)) {
3292  destinationColumn = qBound(column, destinationColumn, colCount - 1);
3293  destinationRow = row + dragRowCount;
3294  insertRows(row + dragRowCount, 1, parent);
3295  flat = (dragRowCount * dragColumnCount) + relativeColumn;
3296  isWrittenTo.resize(++dragRowCount * dragColumnCount);
3297  }
3298  if (!isWrittenTo.testBit(flat)) {
3299  newIndexes[j] = index(destinationRow, destinationColumn, parentItem->index());
3300  isWrittenTo.setBit(flat);
3301  }
3302  }
3303 
3304  for(int k = 0; k < newIndexes.size(); k++) {
3305  if (newIndexes.at(k).isValid()) {
3306  parentItem->setChild(newIndexes.at(k).row(), newIndexes.at(k).column(), items.at(k));
3307  } else {
3308  delete items.at(k);
3309  }
3310  }
3311 
3312  return true;
3313 }
double d
Definition: qnumeric_p.h:62
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
QByteArray data(const QString &mimetype) const
Returns the data stored in the object in the format described by the MIME type specified by mimeType...
Definition: qmimedata.cpp:524
The QStandardItemModel class provides a generic model for storing custom data.
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setChild(int row, int column, QStandardItem *item)
Sets the child item at (row, column) to item.
#define Q_D(Class)
Definition: qglobal.h:2482
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
static FILE * stream
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
QModelIndex index() const
Returns the QModelIndex associated with this item.
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
The QBitArray class provides an array of bits.
Definition: qbitarray.h:54
virtual bool hasFormat(const QString &mimetype) const
Returns true if the object can return data for the MIME type specified by mimeType; otherwise returns...
Definition: qmimedata.cpp:563
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Reimplemented Function
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
The QStandardItem class provides an item for use with the QStandardItemModel class.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
#define INT_MAX

◆ findItems()

QList< QStandardItem * > QStandardItemModel::findItems ( const QString text,
Qt::MatchFlags  flags = Qt::MatchExactly,
int  column = 0 
) const

Returns a list of items that match the given text, using the given flags, in the given column.

Since
4.2

Definition at line 2613 of file qstandarditemmodel.cpp.

2615 {
2616  QModelIndexList indexes = match(index(0, column, QModelIndex()),
2617  Qt::DisplayRole, text, -1, flags);
2618  QList<QStandardItem*> items;
2619  for (int i = 0; i < indexes.size(); ++i)
2620  items.append(itemFromIndex(indexes.at(i)));
2621  return items;
2622 }
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 ...
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented Function
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
The QModelIndex class is used to locate data in a data model.
int size() const
Returns the number of items in the list.
Definition: qlist.h:137

◆ flags()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2861 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::flags(), and QFileDialogComboBox::showPopup().

2862 {
2863  Q_D(const QStandardItemModel);
2864  if (!d->indexValid(index))
2865  return d->root->flags();
2866  QStandardItem *item = d->itemFromIndex(index);
2867  if (item)
2868  return item->flags();
2869  return Qt::ItemIsSelectable
2874 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
Qt::ItemFlags flags() const
Returns the item flags for the item.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ hasChildren()

bool QStandardItemModel::hasChildren ( const QModelIndex parent = QModelIndex()) const
virtual

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2879 of file qstandarditemmodel.cpp.

2880 {
2881  Q_D(const QStandardItemModel);
2882  QStandardItem *item = d->itemFromIndex(parent);
2883  return item ? item->hasChildren() : false;
2884 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
bool hasChildren() const
Returns true if this item has any children; otherwise returns false.
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ headerData()

QVariant QStandardItemModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
virtual

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2889 of file qstandarditemmodel.cpp.

2890 {
2891  Q_D(const QStandardItemModel);
2892  if ((section < 0)
2893  || ((orientation == Qt::Horizontal) && (section >= columnCount()))
2894  || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
2895  return QVariant();
2896  }
2897  QStandardItem *headerItem = 0;
2898  if (orientation == Qt::Horizontal)
2899  headerItem = d->columnHeaderItems.at(section);
2900  else if (orientation == Qt::Vertical)
2901  headerItem = d->rowHeaderItems.at(section);
2902  return headerItem ? headerItem->data(role)
2903  : QAbstractItemModel::headerData(section, orientation, role);
2904 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the data for the given role and section in the header with the specified orientation.
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
virtual QVariant data(int role=Qt::UserRole+1) const
Returns the item&#39;s data for the given role, or an invalid QVariant if there is no data for the role...
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ horizontalHeaderItem()

QStandardItem * QStandardItemModel::horizontalHeaderItem ( int  column) const

Returns the horizontal header item for column if one has been set; otherwise returns 0.

Since
4.2
See also
setHorizontalHeaderItem(), verticalHeaderItem()

Definition at line 2437 of file qstandarditemmodel.cpp.

Referenced by setHorizontalHeaderLabels().

2438 {
2439  Q_D(const QStandardItemModel);
2440  if ((column < 0) || (column >= columnCount()))
2441  return 0;
2442  return d->columnHeaderItems.at(column);
2443 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function

◆ index()

QModelIndex QStandardItemModel::index ( int  row,
int  column,
const QModelIndex parent = QModelIndex() 
) const
virtual

Reimplemented Function

Implements QAbstractItemModel.

Definition at line 2922 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::addUrls(), QUrlModel::changed(), QUrlModel::dataChanged(), dropMimeData(), findItems(), and QUrlModel::urls().

2923 {
2924  Q_D(const QStandardItemModel);
2925  QStandardItem *parentItem = d->itemFromIndex(parent);
2926  if ((parentItem == 0)
2927  || (row < 0)
2928  || (column < 0)
2929  || (row >= parentItem->rowCount())
2930  || (column >= parentItem->columnCount())) {
2931  return QModelIndex();
2932  }
2933  return createIndex(row, column, parentItem);
2934 }
double d
Definition: qnumeric_p.h:62
int columnCount() const
Returns the number of child item columns that the item has.
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. ...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int rowCount() const
Returns the number of child item rows that the item has.
The QModelIndex class is used to locate data in a data model.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ indexFromItem()

QModelIndex QStandardItemModel::indexFromItem ( const QStandardItem item) const

Returns the QModelIndex associated with the given item.

Since
4.2

Use this function when you want to perform an operation that requires the QModelIndex of the item, such as QAbstractItemView::scrollTo(). QStandardItem::index() is provided as convenience; it is equivalent to calling this function.

See also
itemFromIndex(), QStandardItem::index()

Definition at line 2275 of file qstandarditemmodel.cpp.

Referenced by parent(), and QStandardItemPrivate::setModel().

2276 {
2277  if (item && item->d_func()->parent) {
2278  QPair<int, int> pos = item->d_func()->position();
2279  return createIndex(pos.first, pos.second, item->d_func()->parent);
2280  }
2281  return QModelIndex();
2282 }
T1 first
Definition: qpair.h:65
T2 second
Definition: qpair.h:66
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. ...
The QModelIndex class is used to locate data in a data model.
QStandardItem * parent() const
Returns the item&#39;s parent item, or 0 if the item has no parent.

◆ insertColumn() [1/2]

void QStandardItemModel::insertColumn ( int  column,
const QList< QStandardItem *> &  items 
)

Inserts a column at column containing items.

Since
4.2

If necessary, the row count is increased to the size of items.

See also
takeColumn(), appendColumn(), insertRow()

Definition at line 2704 of file qstandarditemmodel.cpp.

2705 {
2706  invisibleRootItem()->insertColumn(column, items);
2707 }
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
void insertColumn(int column, const QList< QStandardItem *> &items)
Inserts a column at column containing items.

◆ insertColumn() [2/2]

bool QStandardItemModel::insertColumn ( int  column,
const QModelIndex parent = QModelIndex() 
)
inline

Inserts a single column before the given column in the child items of the parent specified.

Returns true if the column is inserted; otherwise returns false.

See also
insertColumns(), insertRow(), removeColumn()

Definition at line 442 of file qstandarditemmodel.h.

443 { return QAbstractItemModel::insertColumn(acolumn, aparent); }
bool insertColumn(int column, const QModelIndex &parent=QModelIndex())
Inserts a single column before the given column in the child items of the parent specified.

◆ insertColumns()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2939 of file qstandarditemmodel.cpp.

Referenced by dropMimeData(), and QUrlModel::setFileSystemModel().

2940 {
2942  QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2943  if (item == 0)
2944  return false;
2945  return item->d_func()->insertColumns(column, count, QList<QStandardItem*>());
2946 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QStandardItem class provides an item for use with the QStandardItemModel class.
void insertColumns(int column, int count)
Inserts count columns of child items at column column.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ insertRow() [1/3]

void QStandardItemModel::insertRow ( int  row,
const QList< QStandardItem *> &  items 
)

Inserts a row at row containing items.

Since
4.2

If necessary, the column count is increased to the size of items.

See also
takeRow(), appendRow(), insertColumn()

Definition at line 2676 of file qstandarditemmodel.cpp.

2677 {
2678  invisibleRootItem()->insertRow(row, items);
2679 }
QStandardItem * invisibleRootItem() const
Returns the model&#39;s invisible root item.
void insertRow(int row, const QList< QStandardItem *> &items)
Inserts a row at row containing items.

◆ insertRow() [2/3]

void QStandardItemModel::insertRow ( int  row,
QStandardItem item 
)
inline
Since
4.2

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Inserts a row at row containing item.

When building a list or a tree that has only one column, this function provides a convenient way to append a single new item.

Definition at line 437 of file qstandarditemmodel.h.

438 { insertRow(arow, QList<QStandardItem*>() << aitem); }
void insertRow(int row, const QList< QStandardItem *> &items)
Inserts a row at row containing items.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ insertRow() [3/3]

bool QStandardItemModel::insertRow ( int  row,
const QModelIndex parent = QModelIndex() 
)
inline

Inserts a single row before the given row in the child items of the parent specified.

Returns true if the row is inserted; otherwise returns false.

See also
insertRows(), insertColumn(), removeRow()

Definition at line 440 of file qstandarditemmodel.h.

441 { return QAbstractItemModel::insertRow(arow, aparent); }
bool insertRow(int row, const QModelIndex &parent=QModelIndex())
Inserts a single row before the given row in the child items of the parent specified.

◆ insertRows()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2951 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::addUrls(), and dropMimeData().

2952 {
2954  QStandardItem *item = parent.isValid() ? itemFromIndex(parent) : d->root.data();
2955  if (item == 0)
2956  return false;
2957  return item->d_func()->insertRows(row, count, QList<QStandardItem*>());
2958 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QStandardItem class provides an item for use with the QStandardItemModel class.
void insertRows(int row, const QList< QStandardItem *> &items)
Inserts items at row.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62

◆ invisibleRootItem()

QStandardItem * QStandardItemModel::invisibleRootItem ( ) const

Returns the model's invisible root item.

Since
4.2

The invisible root item provides access to the model's top-level items through the QStandardItem API, making it possible to write functions that can treat top-level items and their children in a uniform way; for example, recursive functions involving a tree model.

Note
Calling index() on the QStandardItem object retrieved from this function is not valid.

Definition at line 2377 of file qstandarditemmodel.cpp.

Referenced by appendColumn(), appendRow(), dropMimeData(), insertColumn(), and insertRow().

2378 {
2379  Q_D(const QStandardItemModel);
2380  return d->root.data();
2381 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ item()

QStandardItem * QStandardItemModel::item ( int  row,
int  column = 0 
) const

Returns the item for the given row and column if one has been set; otherwise returns 0.

Since
4.2
See also
setItem(), takeItem(), itemFromIndex()

Definition at line 2355 of file qstandarditemmodel.cpp.

Referenced by columnCount(), data(), QStandardItemModelPrivate::decodeDataRecursive(), dropMimeData(), flags(), hasChildren(), insertColumns(), insertRows(), itemData(), itemFromIndex(), mimeData(), removeColumns(), removeRows(), rowCount(), setData(), setHorizontalHeaderLabels(), setItemData(), setItemPrototype(), setVerticalHeaderLabels(), and QFileDialogComboBox::showPopup().

2356 {
2357  Q_D(const QStandardItemModel);
2358  return d->root->child(row, column);
2359 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ itemChanged

void QStandardItemModel::itemChanged ( QStandardItem item)
signal
Since
4.2

This signal is emitted whenever the data of item has changed.

◆ itemData()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2963 of file qstandarditemmodel.cpp.

2964 {
2965  Q_D(const QStandardItemModel);
2966  QStandardItem *item = d->itemFromIndex(index);
2967  return item ? item->d_func()->itemData() : QMap<int, QVariant>();
2968 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
The QStandardItem class provides an item for use with the QStandardItemModel class.
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67

◆ itemFromIndex()

QStandardItem * QStandardItemModel::itemFromIndex ( const QModelIndex index) const

Returns a pointer to the QStandardItem associated with the given index.

Since
4.2

Calling this function is typically the initial step when processing QModelIndex-based signals from a view, such as QAbstractItemView::activated(). In your slot, you call itemFromIndex(), with the QModelIndex carried by the signal as argument, to obtain a pointer to the corresponding QStandardItem.

Note that this function will lazily create an item for the index (using itemPrototype()), and set it in the parent item's child table, if no item already exists at that index.

If index is an invalid index, this function returns 0.

See also
indexFromItem()

Definition at line 2243 of file qstandarditemmodel.cpp.

Referenced by dropMimeData(), findItems(), insertColumns(), insertRows(), mimeData(), setData(), and setItemData().

2244 {
2245  Q_D(const QStandardItemModel);
2246  if ((index.row() < 0) || (index.column() < 0) || (index.model() != this))
2247  return 0;
2248  QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer());
2249  if (parent == 0)
2250  return 0;
2251  QStandardItem *item = parent->child(index.row(), index.column());
2252  // lazy part
2253  if (item == 0) {
2254  item = d->createItem();
2255  parent->d_func()->setChild(index.row(), index.column(), item);
2256  }
2257  return item;
2258 }
double d
Definition: qnumeric_p.h:62
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
void setChild(int row, int column, QStandardItem *item)
Sets the child item at (row, column) to item.
#define Q_D(Class)
Definition: qglobal.h:2482
int row() const
Returns the row this model index refers to.
const QAbstractItemModel * model() const
Returns a pointer to the model containing the item that this index refers to.
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
The QStandardItem class provides an item for use with the QStandardItemModel class.
int column() const
Returns the column this model index refers to.
QStandardItem * child(int row, int column=0) const
Returns the child item at (row, column) if one has been set; otherwise returns 0. ...

◆ itemPrototype()

const QStandardItem * QStandardItemModel::itemPrototype ( ) const

Returns the item prototype used by the model.

Since
4.2

The model uses the item prototype as an item factory when it needs to construct new items on demand (for instance, when a view or item delegate calls setData()).

See also
setItemPrototype()

Definition at line 2598 of file qstandarditemmodel.cpp.

2599 {
2600  Q_D(const QStandardItemModel);
2601  return d->itemPrototype;
2602 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ mimeData()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3104 of file qstandarditemmodel.cpp.

3105 {
3107  if(!data)
3108  return 0;
3109 
3110  QString format = QLatin1String("application/x-qstandarditemmodeldatalist");
3111  if (!mimeTypes().contains(format))
3112  return data;
3113  QByteArray encoded;
3115 
3116  QSet<QStandardItem*> itemsSet;
3117  QStack<QStandardItem*> stack;
3118  itemsSet.reserve(indexes.count());
3119  stack.reserve(indexes.count());
3120  for (int i = 0; i < indexes.count(); ++i) {
3121  QStandardItem *item = itemFromIndex(indexes.at(i));
3122  itemsSet << item;
3123  stack.push(item);
3124  }
3125 
3126  //remove duplicates childrens
3127  {
3128  QSet<QStandardItem *> seen;
3129  while (!stack.isEmpty()) {
3130  QStandardItem *itm = stack.pop();
3131  if (seen.contains(itm))
3132  continue;
3133  seen.insert(itm);
3134 
3135  const QVector<QStandardItem*> &childList = itm->d_func()->children;
3136  for (int i = 0; i < childList.count(); ++i) {
3137  QStandardItem *chi = childList.at(i);
3138  if (chi) {
3139  QSet<QStandardItem *>::iterator it = itemsSet.find(chi);
3140  if (it != itemsSet.end()) {
3141  itemsSet.erase(it);
3142  }
3143  stack.push(chi);
3144  }
3145  }
3146  }
3147  }
3148 
3149  stack.reserve(itemsSet.count());
3150  foreach (QStandardItem *item, itemsSet) {
3151  stack.push(item);
3152  }
3153 
3154  //stream everything recursively
3155  while (!stack.isEmpty()) {
3156  QStandardItem *item = stack.pop();
3157  if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
3158  stream << item->row() << item->column();
3159  }
3160  if(item) {
3161  stream << *item << item->columnCount() << item->d_ptr->children.count();
3162  stack += item->d_ptr->children;
3163  } else {
3164  QStandardItem dummy;
3165  stream << dummy << 0 << 0;
3166  }
3167  }
3168 
3169  data->setData(format, encoded);
3170  return data;
3171 }
void setData(const QString &mimetype, const QByteArray &data)
Sets the data associated with the MIME type given by mimeType to the specified data.
Definition: qmimedata.cpp:547
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
int columnCount() const
Returns the number of child item columns that the item has.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
int row() const
Returns the row where the item is located in its parent&#39;s child table, or -1 if the item has no paren...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QVector< QStandardItem * > children
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
static FILE * stream
int column() const
Returns the column where the item is located in its parent&#39;s child table, or -1 if the item has no pa...
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Reimplemented Function
bool contains(const T &value) const
Definition: qset.h:91
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
iterator end()
Definition: qset.h:169
const_iterator insert(const T &value)
Definition: qset.h:179
int count() const
Definition: qset.h:178
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
void push(const T &t)
Adds element t to the top of the stack.
Definition: qstack.h:60
QStringList mimeTypes() const
Reimplemented Function
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
void reserve(int size)
Attempts to allocate memory for at least size elements.
Definition: qvector.h:339
The QStandardItem class provides an item for use with the QStandardItemModel class.
iterator erase(iterator i)
Definition: qset.h:172
QScopedPointer< QStandardItemPrivate > d_ptr
iterator find(const T &value)
Definition: qset.h:182
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
void reserve(int size)
Definition: qset.h:241

◆ mimeTypes()

QStringList QStandardItemModel::mimeTypes ( ) const
virtual

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3096 of file qstandarditemmodel.cpp.

Referenced by mimeData().

3097 {
3098  return QAbstractItemModel::mimeTypes() << QLatin1String("application/x-qstandarditemmodeldatalist");
3099 }
virtual QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ parent()

QObject * QStandardItemModel::parent ( const QModelIndex child) const
virtual

Reimplemented Function

Warning
This function is not part of the public interface.

Implements QAbstractItemModel.

Definition at line 2973 of file qstandarditemmodel.cpp.

2974 {
2975  Q_D(const QStandardItemModel);
2976  if (!d->indexValid(child))
2977  return QModelIndex();
2978  QStandardItem *parentItem = static_cast<QStandardItem*>(child.internalPointer());
2979  return indexFromItem(parentItem);
2980 }
double d
Definition: qnumeric_p.h:62
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
QModelIndex indexFromItem(const QStandardItem *item) const
Returns the QModelIndex associated with the given item.
The QModelIndex class is used to locate data in a data model.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ removeColumns()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2985 of file qstandarditemmodel.cpp.

2986 {
2988  QStandardItem *item = d->itemFromIndex(parent);
2989  if ((item == 0) || (count < 1) || (column < 0) || ((column + count) > item->columnCount()))
2990  return false;
2991  item->removeColumns(column, count);
2992  return true;
2993 }
double d
Definition: qnumeric_p.h:62
void removeColumns(int column, int count)
Removes count columns at column column.
int columnCount() const
Returns the number of child item columns that the item has.
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ removeRows()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2998 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::setUrls().

2999 {
3001  QStandardItem *item = d->itemFromIndex(parent);
3002  if ((item == 0) || (count < 1) || (row < 0) || ((row + count) > item->rowCount()))
3003  return false;
3004  item->removeRows(row, count);
3005  return true;
3006 }
double d
Definition: qnumeric_p.h:62
void removeRows(int row, int count)
Removes count rows at row row.
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int rowCount() const
Returns the number of child item rows that the item has.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ rowCount()

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

Reimplemented Function

Implements QAbstractItemModel.

Definition at line 3011 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::addUrls(), QUrlModel::changed(), dropMimeData(), headerData(), setHeaderData(), QUrlModel::setUrls(), setVerticalHeaderItem(), setVerticalHeaderLabels(), takeVerticalHeaderItem(), QUrlModel::urls(), and verticalHeaderItem().

3012 {
3013  Q_D(const QStandardItemModel);
3014  QStandardItem *item = d->itemFromIndex(parent);
3015  return item ? item->rowCount() : 0;
3016 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int rowCount() const
Returns the number of child item rows that the item has.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ setColumnCount()

void QStandardItemModel::setColumnCount ( int  columns)

Sets the number of columns in this model to columns.

Since
4.2

If this is less than columnCount(), the data in the unwanted columns is discarded.

See also
setRowCount()

Definition at line 2314 of file qstandarditemmodel.cpp.

Referenced by setHorizontalHeaderItem(), and setHorizontalHeaderLabels().

2315 {
2317  d->root->setColumnCount(columns);
2318 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setData()

bool QStandardItemModel::setData ( const QModelIndex index,
const QVariant value,
int  role = Qt::EditRole 
)
virtual

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3021 of file qstandarditemmodel.cpp.

Referenced by QUrlModel::setData().

3022 {
3023  if (!index.isValid())
3024  return false;
3025  QStandardItem *item = itemFromIndex(index);
3026  if (item == 0)
3027  return false;
3028  item->setData(value, role);
3029  return true;
3030 }
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
The QStandardItem class provides an item for use with the QStandardItemModel class.
virtual void setData(const QVariant &value, int role=Qt::UserRole+1)
Sets the item&#39;s data for the given role to the specified value.

◆ setHeaderData()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3035 of file qstandarditemmodel.cpp.

3036 {
3038  if ((section < 0)
3039  || ((orientation == Qt::Horizontal) && (section >= columnCount()))
3040  || ((orientation == Qt::Vertical) && (section >= rowCount()))) {
3041  return false;
3042  }
3043  QStandardItem *headerItem = 0;
3044  if (orientation == Qt::Horizontal) {
3045  headerItem = d->columnHeaderItems.at(section);
3046  if (headerItem == 0) {
3047  headerItem = d->createItem();
3048  headerItem->d_func()->setModel(this);
3049  d->columnHeaderItems.replace(section, headerItem);
3050  }
3051  } else if (orientation == Qt::Vertical) {
3052  headerItem = d->rowHeaderItems.at(section);
3053  if (headerItem == 0) {
3054  headerItem = d->createItem();
3055  headerItem->d_func()->setModel(this);
3056  d->rowHeaderItems.replace(section, headerItem);
3057  }
3058  }
3059  if (headerItem) {
3060  headerItem->setData(value, role);
3061  return true;
3062  }
3063  return false;
3064 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
The QStandardItem class provides an item for use with the QStandardItemModel class.
virtual void setData(const QVariant &value, int role=Qt::UserRole+1)
Sets the item&#39;s data for the given role to the specified value.

◆ setHorizontalHeaderItem()

void QStandardItemModel::setHorizontalHeaderItem ( int  column,
QStandardItem item 
)

Sets the horizontal header item for column to item.

Since
4.2

The model takes ownership of the item. If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted.

See also
horizontalHeaderItem(), setHorizontalHeaderLabels(), setVerticalHeaderItem()

Definition at line 2396 of file qstandarditemmodel.cpp.

Referenced by setHorizontalHeaderLabels().

2397 {
2399  if (column < 0)
2400  return;
2401  if (columnCount() <= column)
2402  setColumnCount(column + 1);
2403 
2404  QStandardItem *oldItem = d->columnHeaderItems.at(column);
2405  if (item == oldItem)
2406  return;
2407 
2408  if (item) {
2409  if (item->model() == 0) {
2410  item->d_func()->setModel(this);
2411  } else {
2412  qWarning("QStandardItem::setHorizontalHeaderItem: Ignoring duplicate insertion of item %p",
2413  item);
2414  return;
2415  }
2416  }
2417 
2418  if (oldItem)
2419  oldItem->d_func()->setModel(0);
2420  delete oldItem;
2421 
2422  d->columnHeaderItems.replace(column, item);
2423  emit headerDataChanged(Qt::Horizontal, column, column);
2424 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
QStandardItemModel * model() const
Returns the QStandardItemModel that this item belongs to.
void setColumnCount(int columns)
Sets the number of columns in this model to columns.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ setHorizontalHeaderLabels()

void QStandardItemModel::setHorizontalHeaderLabels ( const QStringList labels)

Sets the horizontal header labels using labels.

Since
4.2

If necessary, the column count is increased to the size of labels.

See also
setHorizontalHeaderItem()

Definition at line 2518 of file qstandarditemmodel.cpp.

2519 {
2521  if (columnCount() < labels.count())
2522  setColumnCount(labels.count());
2523  for (int i = 0; i < labels.count(); ++i) {
2525  if (!item) {
2526  item = d->createItem();
2527  setHorizontalHeaderItem(i, item);
2528  }
2529  item->setText(labels.at(i));
2530  }
2531 }
double d
Definition: qnumeric_p.h:62
QStandardItem * horizontalHeaderItem(int column) const
Returns the horizontal header item for column if one has been set; otherwise returns 0...
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
void setHorizontalHeaderItem(int column, QStandardItem *item)
Sets the horizontal header item for column to item.
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
void setColumnCount(int columns)
Sets the number of columns in this model to columns.
The QStandardItem class provides an item for use with the QStandardItemModel class.
void setText(const QString &text)
Sets the item&#39;s text to the text specified.

◆ setItem() [1/2]

void QStandardItemModel::setItem ( int  row,
int  column,
QStandardItem item 
)

Sets the item for the given row and column to item.

Since
4.2

The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted.

See also
item()

Definition at line 2333 of file qstandarditemmodel.cpp.

2334 {
2336  d->root->d_func()->setChild(row, column, item, true);
2337 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setItem() [2/2]

void QStandardItemModel::setItem ( int  row,
QStandardItem item 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 431 of file qstandarditemmodel.h.

432 { setItem(arow, 0, aitem); }
void setItem(int row, int column, QStandardItem *item)
Sets the item for the given row and column to item.

◆ setItemData()

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

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3069 of file qstandarditemmodel.cpp.

3070 {
3071  QStandardItem *item = itemFromIndex(index);
3072  if (item == 0)
3073  return false;
3074  item->d_func()->setItemData(roles);
3075  return true;
3076 }
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
QStandardItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the QStandardItem associated with the given index.
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ setItemPrototype()

void QStandardItemModel::setItemPrototype ( const QStandardItem item)

Sets the item prototype for the model to the specified item.

Since
4.2

The model takes ownership of the prototype.

The item prototype acts as a QStandardItem factory, by relying on the QStandardItem::clone() function. To provide your own prototype, subclass QStandardItem, reimplement QStandardItem::clone() and set the prototype to be an instance of your custom class. Whenever QStandardItemModel needs to create an item on demand (for instance, when a view or item delegate calls setData())), the new items will be instances of your custom class.

See also
itemPrototype(), QStandardItem::clone()

Definition at line 2577 of file qstandarditemmodel.cpp.

2578 {
2580  if (d->itemPrototype != item) {
2581  delete d->itemPrototype;
2582  d->itemPrototype = item;
2583  }
2584 }
double d
Definition: qnumeric_p.h:62
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setRowCount()

void QStandardItemModel::setRowCount ( int  rows)

Sets the number of rows in this model to rows.

Since
4.2

If this is less than rowCount(), the data in the unwanted rows is discarded.

See also
setColumnCount()

Definition at line 2296 of file qstandarditemmodel.cpp.

Referenced by setVerticalHeaderItem(), and setVerticalHeaderLabels().

2297 {
2299  d->root->setRowCount(rows);
2300 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setSortRole()

void QStandardItemModel::setSortRole ( int  role)

Definition at line 2832 of file qstandarditemmodel.cpp.

2833 {
2835  d->sortRole = role;
2836 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ setVerticalHeaderItem()

void QStandardItemModel::setVerticalHeaderItem ( int  row,
QStandardItem item 
)

Sets the vertical header item for row to item.

Since
4.2

The model takes ownership of the item. If necessary, the row count is increased to fit the item. The previous header item (if there was one) is deleted.

See also
verticalHeaderItem(), setVerticalHeaderLabels(), setHorizontalHeaderItem()

Definition at line 2458 of file qstandarditemmodel.cpp.

Referenced by setVerticalHeaderLabels().

2459 {
2461  if (row < 0)
2462  return;
2463  if (rowCount() <= row)
2464  setRowCount(row + 1);
2465 
2466  QStandardItem *oldItem = d->rowHeaderItems.at(row);
2467  if (item == oldItem)
2468  return;
2469 
2470  if (item) {
2471  if (item->model() == 0) {
2472  item->d_func()->setModel(this);
2473  } else {
2474  qWarning("QStandardItem::setVerticalHeaderItem: Ignoring duplicate insertion of item %p",
2475  item);
2476  return;
2477  }
2478  }
2479 
2480  if (oldItem)
2481  oldItem->d_func()->setModel(0);
2482  delete oldItem;
2483 
2484  d->rowHeaderItems.replace(row, item);
2485  emit headerDataChanged(Qt::Vertical, row, row);
2486 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
QStandardItemModel * model() const
Returns the QStandardItemModel that this item belongs to.
The QStandardItem class provides an item for use with the QStandardItemModel class.
void setRowCount(int rows)
Sets the number of rows in this model to rows.

◆ setVerticalHeaderLabels()

void QStandardItemModel::setVerticalHeaderLabels ( const QStringList labels)

Sets the vertical header labels using labels.

Since
4.2

If necessary, the row count is increased to the size of labels.

See also
setVerticalHeaderItem()

Definition at line 2544 of file qstandarditemmodel.cpp.

2545 {
2547  if (rowCount() < labels.count())
2548  setRowCount(labels.count());
2549  for (int i = 0; i < labels.count(); ++i) {
2551  if (!item) {
2552  item = d->createItem();
2553  setVerticalHeaderItem(i, item);
2554  }
2555  item->setText(labels.at(i));
2556  }
2557 }
QStandardItem * verticalHeaderItem(int row) const
Returns the vertical header item for row row if one has been set; otherwise returns 0...
double d
Definition: qnumeric_p.h:62
void setVerticalHeaderItem(int row, QStandardItem *item)
Sets the vertical header item for row to item.
QStandardItem * item(int row, int column=0) const
Returns the item for the given row and column if one has been set; otherwise returns 0...
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
The QStandardItem class provides an item for use with the QStandardItemModel class.
void setText(const QString &text)
Sets the item&#39;s text to the text specified.
void setRowCount(int rows)
Sets the number of rows in this model to rows.

◆ sort()

void QStandardItemModel::sort ( int  column,
Qt::SortOrder  order = Qt::AscendingOrder 
)
virtual

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 3081 of file qstandarditemmodel.cpp.

3082 {
3084  d->root->sortChildren(column, order);
3085 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ sortRole()

int QStandardItemModel::sortRole ( ) const

Referenced by takeVerticalHeaderItem().

◆ supportedDropActions()

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

QStandardItemModel supports both copy and move.

Reimplemented Function

Reimplemented from QAbstractItemModel.

Definition at line 2914 of file qstandarditemmodel.cpp.

2915 {
2916  return Qt::CopyAction | Qt::MoveAction;
2917 }

◆ takeColumn()

QList< QStandardItem * > QStandardItemModel::takeColumn ( int  column)

Removes the given column without deleting the column items, and returns a list of pointers to the removed items.

Since
4.2

The model releases ownership of the items. For items in the column that have not been set, the corresponding pointers in the list will be 0.

See also
takeRow()

Definition at line 2758 of file qstandarditemmodel.cpp.

2759 {
2761  return d->root->takeColumn(column);
2762 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ takeHorizontalHeaderItem()

QStandardItem * QStandardItemModel::takeHorizontalHeaderItem ( int  column)

Removes the horizontal header item at column from the header without deleting it, and returns a pointer to the item.

Since
4.2

The model releases ownership of the item.

See also
horizontalHeaderItem(), takeVerticalHeaderItem()

Definition at line 2776 of file qstandarditemmodel.cpp.

2777 {
2779  if ((column < 0) || (column >= columnCount()))
2780  return 0;
2781  QStandardItem *headerItem = d->columnHeaderItems.at(column);
2782  if (headerItem) {
2783  headerItem->d_func()->setParentAndModel(0, 0);
2784  d->columnHeaderItems.replace(column, 0);
2785  }
2786  return headerItem;
2787 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ takeItem()

QStandardItem * QStandardItemModel::takeItem ( int  row,
int  column = 0 
)

Removes the item at (row, column) without deleting it.

Since
4.2

The model releases ownership of the item.

See also
item(), takeRow(), takeColumn()

Definition at line 2720 of file qstandarditemmodel.cpp.

2721 {
2723  return d->root->takeChild(row, column);
2724 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ takeRow()

QList< QStandardItem * > QStandardItemModel::takeRow ( int  row)

Removes the given row without deleting the row items, and returns a list of pointers to the removed items.

Since
4.2

The model releases ownership of the items. For items in the row that have not been set, the corresponding pointers in the list will be 0.

See also
takeColumn()

Definition at line 2739 of file qstandarditemmodel.cpp.

2740 {
2742  return d->root->takeRow(row);
2743 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482

◆ takeVerticalHeaderItem()

QStandardItem * QStandardItemModel::takeVerticalHeaderItem ( int  row)

Removes the vertical header item at row from the header without deleting it, and returns a pointer to the item.

Since
4.2

The model releases ownership of the item.

See also
verticalHeaderItem(), takeHorizontalHeaderItem()

Definition at line 2801 of file qstandarditemmodel.cpp.

2802 {
2804  if ((row < 0) || (row >= rowCount()))
2805  return 0;
2806  QStandardItem *headerItem = d->rowHeaderItems.at(row);
2807  if (headerItem) {
2808  headerItem->d_func()->setParentAndModel(0, 0);
2809  d->rowHeaderItems.replace(row, 0);
2810  }
2811  return headerItem;
2812 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
The QStandardItem class provides an item for use with the QStandardItemModel class.

◆ verticalHeaderItem()

QStandardItem * QStandardItemModel::verticalHeaderItem ( int  row) const

Returns the vertical header item for row row if one has been set; otherwise returns 0.

Since
4.2
See also
setVerticalHeaderItem(), horizontalHeaderItem()

Definition at line 2499 of file qstandarditemmodel.cpp.

Referenced by setVerticalHeaderLabels().

2500 {
2501  Q_D(const QStandardItemModel);
2502  if ((row < 0) || (row >= rowCount()))
2503  return 0;
2504  return d->rowHeaderItems.at(row);
2505 }
double d
Definition: qnumeric_p.h:62
The QStandardItemModel class provides a generic model for storing custom data.
#define Q_D(Class)
Definition: qglobal.h:2482
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function

Friends and Related Functions

◆ QStandardItem

friend class QStandardItem
friend

Definition at line 423 of file qstandarditemmodel.h.

◆ QStandardItemPrivate

friend class QStandardItemPrivate
friend

Definition at line 422 of file qstandarditemmodel.h.

Properties

◆ sortRole

int QStandardItemModel::sortRole
private

the item role that is used to query the model's data when sorting items

Since
4.2

The default value is Qt::DisplayRole.

See also
sort(), QStandardItem::sortChildren()

Definition at line 321 of file qstandarditemmodel.h.

Referenced by QStandardItem::operator<().


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