gpt4 book ai didi

c++ - 为什么 QStandardItemModel 的成员函数 children().size() 返回 0,而函数 hasChildren() 返回 true?

转载 作者:行者123 更新时间:2023-11-30 02:34:22 25 4
gpt4 key购买 nike

_inputfileModel 是一个QStandardItemModel 类型的指针,我想使用它的成员函数children() 来获取子项。但是在下面的代码中,int childrenNum = _inputfileModel->children().size(); childrenNum 的结果不是 1 而是 0。但是当我使用 hasChildren(),返回值为真。谁能解释为什么?函数 children() 返回顶级子级还是所有子级?

void InputTree::addTreeNode(TreeNode &node){  

QStringList inputImgList = node.picturePathList;
int num = inputImgList.size();
if( num < 1){ return ;}
QStandardItem *fatherItem = new QStandardItem;
fatherItem->setIcon(node.fatherIcon);
fatherItem->setText(node.fatherNodeName);
fatherItem->setData(FOLDER,ItemTypeRole);

for( int i = 0; i < num; ++i)
{
QStandardItem *pictureItem = new QStandardItem;
pictureItem->setText(node.imageNodeName.at(i));
pictureItem->setIcon(node.imgIcon);
pictureItem->setData(PICTURE,ItemTypeRole);
fatherItem->appendRow(pictureItem);
}
_inputfileModel->appendRow(fatherItem);
bool has_child = false;
has_child = _inputfileModel->hasChildren();
int childrenNum = _inputfileModel->children().size();
}

最佳答案

只需阅读文档:

bool QAbstractItemModel::hasChildren(const QModelIndex & parent = QModelIndex()) const

Returns true if parent has any children; otherwise returns false.

Use rowCount() on the parent to find out the number of children.

那么 child 呢:

const QObjectList & QObject::children() const

Returns a list of child objects.

这不是你真正想要的。

所以你应该使用 QStandardItemModel::rowCount 而不是 children();

关于c++ - 为什么 QStandardItemModel 的成员函数 children().size() 返回 0,而函数 hasChildren() 返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34583727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com