重要成员函数
1、QBrush background() / void setBackground(const QBrush &brush)
背景画刷。
2、Qt::CheckState checkState() / void setCheckState(Qt::CheckState state)
设置按下。
3、void clearData()
从先前设置的所有角色中删除所有数据。
4、QStandardItem * clone()
返回此项目的副本。不复制项目的子项。
5、int column() / int row()
返回该项在其父项的子表中所在的列/行,如果该项没有父项,则返回 -1。
6、int columnCount() / int rowCount()
返回项目具有的子项目列/行的数量。
7、QVariant data(int role = Qt::UserRole + 1)
void setData(const QVariant &value, int role = Qt::UserRole + 1)
返回给定角色的项目数据,如果没有该角色的数据,则返回无效的 QVariant。
8、QBrush foreground() / void setForeground(const QBrush &brush)
前景画刷。
9、bool hasChildren()
是否有子项。
10、QModelIndex index()
返回与此项关联的 QModelIndex。
11、bool isAutoTristate()
返回项是否为三态并由 QTreeWidget 控制。默认值为false。
12、QStandardItemModel * model()
返回该项目所属的 QStandardItemModel。
13、QStandardItem * parent()
返回该项的父项,如果该项没有父项,则返回 nullptr。
注意:对于顶级项目 parent() 返回 nullptr。 要接收顶级项目的父项,请改用 QStandardItemModel::invisibleRootItem()。
遍历某一项的子结点(如QTreeView)
QModelIndex curIndex = TreeView->model()->currentIndex();
QStandardItem* curItem = TreeView->model()->itemFromIndex(curIndex);
if(!curItem->hasChildren())
return;
int child_index = 0;
while (curItem->child(child_index))
{
qDebug()<<"子结点"<<curItem->child(child_index)->text();
++child_index;
}