• 4532阅读
  • 0回复

Simple Tree Model Example的问题 [复制链接]

上一主题 下一主题
离线jery16
 

只看楼主 倒序阅读 楼主  发表于: 2010-12-21
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
    QList<TreeItem*> parents;
    QList<int> indentations;
    parents << parent;
    indentations << 0;

    int number = 0;

    while (number < lines.count()) {
        int position = 0;
        while (position < lines[number].length()) {
            if (lines[number].mid(position, 1) != " ")
                break;
            position++;
        }

        QString lineData = lines[number].mid(position).trimmed();

        if (!lineData.isEmpty()) {
            // Read the column data from the rest of the line.
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
            QList<QVariant> columnData;
            for (int column = 0; column < columnStrings.count(); ++column)
                columnData << columnStrings[column];

            if (position > indentations.last()) {
                // The last child of the current parent is now the new parent
                // unless the current parent has no children.

                if (parents.last()->childCount() > 0) {                                                                      //1.子项出现的时候,会把最后一个父亲的最后一个孩子加到父亲列表中
                    parents << parents.last()->child(parents.last()->childCount()-1);
                    indentations << position;
                }
            } else {
                while (position < indentations.last() && parents.count() > 0) {
                    parents.pop_back();
                    indentations.pop_back();
                }
            }

            // Append a new item to the current parent's list of children.
            parents.last()->appendChild(new TreeItem(columnData, parents.last()));    //2.然后把当前项加入到新的最后一个父亲的孩子中,奇怪的是这个子项也加入到了前一个父亲//的最后一个孩子的子项中,这是怎么回事呢?
        }

        number++;
    }
}
快速回复
限100 字节
 
上一个 下一个