QStandardItemModel Class Reference

[ QtGui module]

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

继承 QAbstractItemModel .

方法

Qt Signals


详细描述

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 类 且属于 Qt 的 模型/视图 框架。

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 (obsolete) 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 () 到 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 () 和 setColumnCount () to alter the dimensions of the model. To insert items, use insertRow () 或 insertColumn (), and to remove items, use removeRow () 或 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 ().

调用 clear () 到 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:

 QStandardItemModel model;
 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 提供 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.


方法文档编制

QStandardItemModel.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a new item model with the given parent .

QStandardItemModel.__init__ ( self , int  rows , int  columns , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

QStandardItemModel.appendColumn ( self , list-of-QStandardItem  items )

items argument has it's ownership transferred to Qt.

Appends a column containing items . If necessary, the row count is increased to the size of items .

该函数在 Qt 4.2 引入。

另请参阅 insertColumn () 和 appendRow ().

QStandardItemModel.appendRow ( self , list-of-QStandardItem  items )

items argument has it's ownership transferred to Qt.

Appends a row containing items . If necessary, the column count is increased to the size of items .

该函数在 Qt 4.2 引入。

另请参阅 insertRow () 和 appendColumn ().

QStandardItemModel.appendRow ( self , QStandardItem   aitem )

aitem argument has it's ownership transferred to Qt.

这是重载函数。

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

该函数在 Qt 4.2 引入。

QStandardItemModel.clear ( self )

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

另请参阅 removeColumns () 和 removeRows ().

int QStandardItemModel.columnCount ( self , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.columnCount ().

另请参阅 setColumnCount ().

QVariant QStandardItemModel.data ( self , QModelIndex   index , int  role  = Qt.DisplayRole)

重实现自 QAbstractItemModel.data ().

另请参阅 setData ().

bool QStandardItemModel.dropMimeData ( self , QMimeData   data , Qt.DropAction   action , int  row , int  column , QModelIndex   parent )

重实现自 QAbstractItemModel.dropMimeData ().

list-of-QStandardItem QStandardItemModel.findItems ( self , QString  text , Qt.MatchFlags   flags  = Qt.MatchExactly, int  column  = 0)

返回的项列表匹配给定 text ,使用 the given flags ,在给定 column .

该函数在 Qt 4.2 引入。

Qt.ItemFlags QStandardItemModel.flags ( self , QModelIndex   index )

重实现自 QAbstractItemModel.flags ().

bool QStandardItemModel.hasChildren ( self , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.hasChildren ().

QVariant QStandardItemModel.headerData ( self , int  section , Qt.Orientation   orientation , int  role  = Qt.DisplayRole)

重实现自 QAbstractItemModel.headerData ().

另请参阅 setHeaderData ().

QStandardItem QStandardItemModel.horizontalHeaderItem ( self , int  column )

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

该函数在 Qt 4.2 引入。

另请参阅 setHorizontalHeaderItem () and verticalHeaderItem ().

QModelIndex QStandardItemModel.index ( self , int  row , int  column , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.index ().

QModelIndex QStandardItemModel.indexFromItem ( self , QStandardItem   item )

返回 QModelIndex 关联给定 item .

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

该函数在 Qt 4.2 引入。

另请参阅 itemFromIndex () 和 QStandardItem.index ().

QStandardItemModel.insertColumn ( self , int  column , list-of-QStandardItem  items )

items argument has it's ownership transferred to Qt.

Inserts a column at column containing items 。若 necessary, the row count is increased to the size of items .

该函数在 Qt 4.2 引入。

另请参阅 takeColumn (), appendColumn (),和 insertRow ().

bool QStandardItemModel.insertColumn ( self , int  column , QModelIndex   parent  = QModelIndex())

Inserts a single column before the given column 在 child items of the parent specified. Returns true if the column is inserted; otherwise returns false.

另请参阅 insertColumns (), insertRow (),和 removeColumn ().

bool QStandardItemModel.insertColumns ( self , int  column , int  count , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.insertColumns ().

QStandardItemModel.insertRow ( self , int  row , list-of-QStandardItem  items )

items argument has it's ownership transferred to Qt.

Inserts a row at row containing items 。若 necessary, the column count is increased to the size of items .

该函数在 Qt 4.2 引入。

另请参阅 takeRow (), appendRow (),和 insertColumn ().

QStandardItemModel.insertRow ( self , int  arow , QStandardItem   aitem )

aitem argument has it's ownership transferred to Qt.

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.

另请参阅 insertRows (), insertColumn (),和 removeRow ().

bool QStandardItemModel.insertRow ( self , int  row , QModelIndex   parent  = QModelIndex())

这是重载函数。

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.

该函数在 Qt 4.2 引入。

bool QStandardItemModel.insertRows ( self , int  row , int  count , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.insertRows ().

QStandardItem QStandardItemModel.invisibleRootItem ( self )

Returns the model's invisible root item.

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.

注意: 调用 index() QStandardItem object retrieved from this function is not valid.

该函数在 Qt 4.2 引入。

QStandardItem QStandardItemModel.item ( self , int  row , int  column  = 0)

返回项为给定 row and column if one has been set; otherwise returns 0.

该函数在 Qt 4.2 引入。

另请参阅 setItem (), takeItem (),和 itemFromIndex ().

dict-of-int-QVariant QStandardItemModel.itemData ( self , QModelIndex   index )

重实现自 QAbstractItemModel.itemData ().

另请参阅 setItemData ().

QStandardItem QStandardItemModel.itemFromIndex ( self , QModelIndex   index )

返回指针指向 QStandardItem 关联给定 index .

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.

index is an invalid index, this function returns 0.

该函数在 Qt 4.2 引入。

另请参阅 indexFromItem ().

QStandardItem QStandardItemModel.itemPrototype ( self )

Returns the item prototype used by the model. 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 ()).

该函数在 Qt 4.2 引入。

另请参阅 setItemPrototype ().

QMimeData QStandardItemModel.mimeData ( self , list-of-QModelIndex  indexes )

QMimeData result

重实现自 QAbstractItemModel.mimeData ().

QStringList QStandardItemModel.mimeTypes ( self )

重实现自 QAbstractItemModel.mimeTypes ().

QModelIndex QStandardItemModel.parent ( self , QModelIndex   child )

重实现自 QAbstractItemModel.parent ().

QObject QStandardItemModel.parent ( self )

bool QStandardItemModel.removeColumns ( self , int  column , int  count , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.removeColumns ().

bool QStandardItemModel.removeRows ( self , int  row , int  count , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.removeRows ().

int QStandardItemModel.rowCount ( self , QModelIndex   parent  = QModelIndex())

重实现自 QAbstractItemModel.rowCount ().

另请参阅 setRowCount ().

QStandardItemModel.setColumnCount ( self , int  columns )

Sets the number of columns in this model to columns 。若 this is less than columnCount (), the data in the unwanted columns is discarded.

该函数在 Qt 4.2 引入。

另请参阅 columnCount () 和 setRowCount ().

bool QStandardItemModel.setData ( self , QModelIndex   index , QVariant  value , int  role  = Qt.EditRole)

重实现自 QAbstractItemModel.setData ().

另请参阅 data ().

bool QStandardItemModel.setHeaderData ( self , int  section , Qt.Orientation   orientation , QVariant  value , int  role  = Qt.EditRole)

重实现自 QAbstractItemModel.setHeaderData ().

另请参阅 headerData ().

QStandardItemModel.setHorizontalHeaderItem ( self , int  column , QStandardItem   item )

item argument has it's ownership transferred to Qt.

Sets the horizontal header item for column to item . 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.

该函数在 Qt 4.2 引入。

另请参阅 horizontalHeaderItem (), setHorizontalHeaderLabels (), and setVerticalHeaderItem ().

QStandardItemModel.setHorizontalHeaderLabels ( self , QStringList  labels )

设置水平 Header 头标签,使用 labels 。若 necessary, the column count is increased to the size of labels .

该函数在 Qt 4.2 引入。

另请参阅 setHorizontalHeaderItem ().

QStandardItemModel.setItem ( self , int  row , int  column , QStandardItem   item )

item argument has it's ownership transferred to Qt.

Sets the item for the given row and column to item . 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.

该函数在 Qt 4.2 引入。

另请参阅 item ().

QStandardItemModel.setItem ( self , int  arow , QStandardItem   aitem )

aitem argument has it's ownership transferred to Qt.

这是重载函数。

bool QStandardItemModel.setItemData ( self , QModelIndex   index , dict-of-int-QVariant  roles )

重实现自 QAbstractItemModel.setItemData ().

另请参阅 itemData ().

QStandardItemModel.setItemPrototype ( self , QStandardItem   item )

item argument has it's ownership transferred to Qt.

Sets the item prototype for the model to the specified item . 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.

该函数在 Qt 4.2 引入。

另请参阅 itemPrototype () 和 QStandardItem.clone ().

QStandardItemModel.setRowCount ( self , int  rows )

Sets the number of rows in this model to rows . If this is less than rowCount (), the data in the unwanted rows is discarded.

该函数在 Qt 4.2 引入。

另请参阅 rowCount () 和 setColumnCount ().

QStandardItemModel.setSortRole ( self , int  role )

QStandardItemModel.setVerticalHeaderItem ( self , int  row , QStandardItem   item )

item argument has it's ownership transferred to Qt.

Sets the vertical header item for row to item 。 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.

该函数在 Qt 4.2 引入。

另请参阅 verticalHeaderItem (), setVerticalHeaderLabels (), and setHorizontalHeaderItem ().

QStandardItemModel.setVerticalHeaderLabels ( self , QStringList  labels )

Sets the vertical header labels using labels 。若 necessary, the row count is increased to the size of labels .

该函数在 Qt 4.2 引入。

另请参阅 setVerticalHeaderItem ().

QStandardItemModel.sort ( self , int  column , Qt.SortOrder   order  = Qt.AscendingOrder)

重实现自 QAbstractItemModel.sort ().

int QStandardItemModel.sortRole ( self )

Qt.DropActions QStandardItemModel.supportedDropActions ( self )

重实现自 QAbstractItemModel.supportedDropActions ().

QStandardItemModel supports both copy and move.

list-of-QStandardItem QStandardItemModel.takeColumn ( self , int  column )

list-of-QStandardItem result

移除给定 column without deleting the column items, and returns a list of pointers to the removed items. 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.

该函数在 Qt 4.2 引入。

另请参阅 takeRow ().

QStandardItem QStandardItemModel.takeHorizontalHeaderItem ( self , int  column )

QStandardItem result

Removes the horizontal header item at column 从 header without deleting it, and returns a pointer to the item. The model releases ownership of the item.

该函数在 Qt 4.2 引入。

另请参阅 horizontalHeaderItem () and takeVerticalHeaderItem ().

QStandardItem QStandardItemModel.takeItem ( self , int  row , int  column  = 0)

QStandardItem result

Removes the item at ( row , column ) without deleting it. The model releases ownership of the item.

该函数在 Qt 4.2 引入。

另请参阅 item (), takeRow (),和 takeColumn ().

list-of-QStandardItem QStandardItemModel.takeRow ( self , int  row )

list-of-QStandardItem result

移除给定 row without deleting the row items, and returns a list of pointers to the removed items. 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.

该函数在 Qt 4.2 引入。

另请参阅 takeColumn ().

QStandardItem QStandardItemModel.takeVerticalHeaderItem ( self , int  row )

QStandardItem result

移除垂直 Header 头部项在 row from the header without deleting it, and returns a pointer to the item. The model releases ownership of the item.

该函数在 Qt 4.2 引入。

另请参阅 verticalHeaderItem () and takeHorizontalHeaderItem ().

QStandardItem QStandardItemModel.verticalHeaderItem ( self , int  row )

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

该函数在 Qt 4.2 引入。

另请参阅 setVerticalHeaderItem () and horizontalHeaderItem ().


Qt Signal Documentation

void itemChanged (QStandardItem*)

This is the default overload of this signal.

This signal is emitted whenever the data of item has 改变。

该函数在 Qt 4.2 引入。