QTreeView / QAbstractItemModel does only display data when I call my own populate function from within the constructor

StackOverflow https://stackoverflow.com/questions/22751118

Question

I've implemented the code from the EditableTreeModel example and it works fine when I use my own function to build a directory tree. My functions is a public member of the TreeModel class and is defined like this:

void TreeModel::construct(TreeItem *parent, QString path,uint32_t parentID){//fileide of the parent

QDirIterator dir(path,QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Files | QDir::Dirs | QDir::Readable,QDirIterator::NoIteratorFlags);
QFileInfo file; QString curPath;TreeItem *child;
helix::ResourceEntry entry; uint32_t fileID = parentID;
while(dir.hasNext()){
    curPath = dir.next();
    file = dir.fileInfo();

    entry.name = dir.fileName().toStdString();

    entry.parent = parentID;
    entry.fileID = ++fileID;

    if(parent->childCount() > 0){
        //if we have a previous child, set its next id to point on the current entry
        child = parent->child(parent->childCount() - 1);
        child->resourceEntry.next = fileID;
    }       

    if(file.isDir()){
        entry.entryType = helix::HRESOURCE_ENTRY_TYPE_E::directory;
    }else{
        entry.entryType = helix::HRESOURCE_ENTRY_TYPE_E::file;
        entry.size = file.size();
    }

    //Insert a new child
    if(parent->insertChildren(parent->childCount(), 1, rootItem->columnCount())){
        child = parent->child(parent->childCount() - 1);
        child->setData(0, QVariant(dir.fileName()));
        child->resourceEntry = entry;
        child->path = curPath;

        //traverse into subdir if it is not empty
        if(file.isDir() && QDir(curPath).count() > 0) construct(child,curPath,entry.fileID);
    }       
}
}

When i call this function from the TreeModel constructor like this:

TreeModel::TreeModel(/*const QStringList &headers, const QString &data, */QObject *parent)
: QAbstractItemModel(parent)
{
QVector<QVariant> rootData; rootData << "Entry";

rootItem = new TreeItem(rootData);
rootItem->resourceEntry.fileID = 0;

construct(rootItem,"D:\\tmp",0);
}

It works fine and all entries are displayed correctly. But if I call this function from outside the class nothing happens: model->construct(model->getRootItem(),"D:\\tmp",0); My goal was to use a thread which implements the construct function and pass the root item to the thead, but i noticed that the treeview didn't display the data, thats why i copied the function to the TreeModel.cpp and called if from the constructor. Is this some kind of restriction of the Model that it can only be initialized from the constructor?

Any hint or tip is appreciated.

Thanks, Fabian

Was it helpful?

Solution

Okay it makes total sense that it didnt work: I need to construct the model before I call ui.treeView->setModel(model) So in my case when the thread is done executing the construct function above I call ui.treeView->reset() and then ui.treeView->setModel(model). Everything now works as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top