QStringListModel 類

QStringListModel 類提供為視圖供給字符串的模型。 更多...

頭: #include <QStringListModel>
qmake: QT += core
繼承: QAbstractListModel

公共函數

QStringListModel (const QStringList & strings , QObject * parent = nullptr)
QStringListModel (QObject * parent = nullptr)
void setStringList (const QStringList & strings )
QStringList stringList () const

重實現公共函數

virtual QVariant data (const QModelIndex & index , int role = Qt::DisplayRole) const override
virtual Qt::ItemFlags flags (const QModelIndex & index ) const override
virtual bool insertRows (int row , int count , const QModelIndex & parent = QModelIndex()) override
virtual QMap<int, QVariant> itemData (const QModelIndex & index ) const override
virtual bool moveRows (const QModelIndex & sourceParent , int sourceRow , int count , const QModelIndex & destinationParent , int destinationChild ) override
virtual bool removeRows (int row , int count , const QModelIndex & parent = QModelIndex()) override
virtual int rowCount (const QModelIndex & parent = QModelIndex()) const override
virtual bool setData (const QModelIndex & index , const QVariant & value , int role = Qt::EditRole) override
virtual bool setItemData (const QModelIndex & index , const QMap<int, QVariant> & roles ) override
virtual QModelIndex sibling (int row , int column , const QModelIndex & idx ) const override
virtual void sort (int column , Qt::SortOrder order = Qt::AscendingOrder) override
virtual Qt::DropActions supportedDropActions () const override

詳細描述

QStringListModel 是可以用於需要在視圖 Widget 中顯示許多字符串的簡單情況的可編輯模型,譬如 QListView QComboBox .

模型提供可編輯模型的所有標準功能,將字符串列錶中的數據錶示成具有一列和行數等於列錶項數的模型。

對應項的模型索引的獲得采用 index() function, and item flags are obtained with flags ()。項數據的讀取采用 data () 函數和寫入采用 setData ()。行數 (和字符串列錶中的項數) 可以找到采用 rowCount () 函數。

模型可以采用現有字符串列錶構建,或稍後可以設置字符串采用 setStringList () 方便函數。也可以按通常方式插入字符串采用 insertRows () 函數,和移除采用 removeRows ()。可以檢索字符串列錶的內容采用 stringList () 方便函數。

QStringListModel 範例用法:

    QStringListModel *model = new QStringListModel();
    QStringList list;
    list << "a" << "b" << "c";
    model->setStringList(list);
					

另請參閱 QAbstractListModel , QAbstractItemModel ,和 模型類 .

成員函數文檔編製

QStringListModel:: QStringListModel (const QStringList & strings , QObject * parent = nullptr)

構造的字符串列錶模型包含指定 strings 采用給定 parent .

QStringListModel:: QStringListModel ( QObject * parent = nullptr)

構造字符串列錶模型采用給定 parent .

[override virtual] QVariant QStringListModel:: data (const QModelIndex & index , int role = Qt::DisplayRole) const

重實現: QAbstractItemModel::data (const QModelIndex &index, int role) const.

返迴數據為指定 role ,從項采用給定 index .

若視圖請求無效索引,則返迴無效變體。

另請參閱 setData ().

[override virtual] Qt::ItemFlags QStringListModel:: flags (const QModelIndex & index ) const

重實現: QAbstractListModel::flags (const QModelIndex &index) const.

Returns the flags for the item with the given index .

Valid items are enabled, selectable, editable, drag enabled and drop enabled.

另請參閱 QAbstractItemModel::flags ().

[override virtual] bool QStringListModel:: insertRows ( int row , int count , const QModelIndex & parent = QModelIndex())

重實現: QAbstractItemModel::insertRows (int row, int count, const QModelIndex &parent).

插入 count rows into the model, beginning at the given row .

The parent index of the rows is optional and is only used for consistency with QAbstractItemModel . By default, a null index is specified, indicating that the rows are inserted in the top level of the model.

返迴 true if the insertion was successful.

另請參閱 QAbstractItemModel::insertRows ().

[override virtual] QMap < int , QVariant > QStringListModel:: itemData (const QModelIndex & index ) const

重實現: QAbstractItemModel::itemData (const QModelIndex &index) const.

該函數在 Qt 5.13 引入。

另請參閱 setItemData ().

[override virtual] bool QStringListModel:: moveRows (const QModelIndex & sourceParent , int sourceRow , int count , const QModelIndex & destinationParent , int destinationChild )

重實現: QAbstractItemModel::moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild).

該函數在 Qt 5.13 引入。

[override virtual] bool QStringListModel:: removeRows ( int row , int count , const QModelIndex & parent = QModelIndex())

重實現: QAbstractItemModel::removeRows (int row, int count, const QModelIndex &parent).

移除 count rows from the model, beginning at the given row .

The parent index of the rows is optional and is only used for consistency with QAbstractItemModel . By default, a null index is specified, indicating that the rows are removed in the top level of the model.

返迴 true if the row removal was successful.

另請參閱 QAbstractItemModel::removeRows ().

[override virtual] int QStringListModel:: rowCount (const QModelIndex & parent = QModelIndex()) const

重實現: QAbstractItemModel::rowCount (const QModelIndex &parent) const.

Returns the number of rows in the model. This value corresponds to the number of items in the model's internal string list.

可選 parent argument is in most models used to specify the parent of the rows to be counted. Because this is a list if a valid parent is specified, the result will always be 0.

另請參閱 insertRows (), removeRows (),和 QAbstractItemModel::rowCount ().

[override virtual] bool QStringListModel:: setData (const QModelIndex & index , const QVariant & value , int role = Qt::EditRole)

重實現: QAbstractItemModel::setData (const QModelIndex &index, const QVariant &value, int role).

設置數據按指定 role 在項采用給定 index 在模型,以提供 value .

The dataChanged () 信號發射,若項改變。返迴 true 後於發射 dataChanged () 信號。

另請參閱 Qt::ItemDataRole and data ().

[override virtual] bool QStringListModel:: setItemData (const QModelIndex & index , const QMap < int , QVariant > & roles )

重實現: QAbstractItemModel::setItemData (const QModelIndex &index, const QMap<int, QVariant> &roles).

roles 包含兩者 Qt::DisplayRole and Qt::EditRole ,後者優先

該函數在 Qt 5.13 引入。

另請參閱 itemData ().

void QStringListModel:: setStringList (const QStringList & strings )

將模型的內部字符串列錶設為 strings 。模型將通知任何附加視圖其底層數據已改變。

另請參閱 stringList () 和 dataChanged ().

[override virtual] QModelIndex QStringListModel:: sibling ( int row , int column , const QModelIndex & idx ) const

重實現: QAbstractListModel::sibling (int row, int column, const QModelIndex &idx) const.

[override virtual] void QStringListModel:: sort ( int column , Qt::SortOrder order = Qt::AscendingOrder)

重實現: QAbstractItemModel::sort (int column, Qt::SortOrder order).

QStringList QStringListModel:: stringList () const

返迴模型用於存儲數據的字符串列錶。

另請參閱 setStringList ().

[override virtual] Qt::DropActions QStringListModel:: supportedDropActions () const

重實現: QAbstractItemModel::supportedDropActions () const.