2016-01-04 20 views
1

_inputfileModelQStandardItemModel類型的指針,我想使用它的成員函數children()讓孩子們的物品。但在隨後的代碼, int childrenNum = _inputfileModel->children().size();childrenNum的結果不是1,但0。但是當我使用hasChildren(),返回值是真實的。誰能解釋爲什麼?並且功能children()返回給孩子或所有的孩子?爲什麼QStandardItemModel的成員函數的孩子()。大小()返回0,而功能hasChildren()返回true?

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(); 
} 

回答

3

只要閱讀文檔:

布爾化QAbstractItemModel :: hasChildren(常量QModelIndex &父= QModelIndex())const的

返回true,如果家長有任何子女;否則返回false。

使用rowCount時(),找出孩子的數量。

又是怎麼回事兒:

常量QObjectList &的QObject ::兒童()const的

返回子對象的列表。

這不是你真正想要的。

所以,你應該用QStandardItemModel::rowCount代替children();

相關問題