0

I built a large tree whose branches are loaded from disk, an operation that takes 5 minutes to complete. To speed it up, I want to only load the sub-trees the user selects from the GUI.

This is the current code, that loads everything all at once:

int MyWidget::AddSubtree(QTreeWidgetItem *_projectItem,
                         const QString &_filePath)
{
    const QString currentPrj = _projectItem->text(0);
    QDir* rootDir = new QDir(_filePath);
    QFileInfoList filesList = rootDir->entryInfoList();

    filesList = rootDir->entryInfoList();

    foreach(QFileInfo fileInfo, filesList)
    {
        if(fileInfo.isDir())
        {
            const QString job = fileInfo.fileName();

            QTreeWidgetItem* jobItem = new QTreeWidgetItem();
            jobItem->setText(0, job);

            //+P Performance critical! Do it on demand
            AddSubSubtree(jobItem, fileInfo.filePath());

            _projectItem->addChild(jobItem);
        }
    }

    return 0;
}


int MyWidget::AddSubSubtree(QTreeWidgetItem *_jobItem,
                            const QString &_filePath)
{
    const QString currentJob = _jobItem->text(0);
    QDir* rootDir = new QDir(_filePath);
    rootDir->cd("subDir");

    // Retrieve the runs

    filesList = rootDir->entryInfoList();

    foreach(QFileInfo fileInfo, filesList)
    {
        if(fileInfo.isDir())
        {
            const QString run = fileInfo.fileName();

            QTreeWidgetItem* runItem = new QTreeWidgetItem();
            runItem->setText(0, run);

            _jobItem->addChild(runItem);
        }
    }

    return 0;
}

What I would like to do is to remove the performance critical line (below //+P), and call AddSubSubtree() on a single sub-sub-tree just when the user clicks on the corresponding parent branch.

How can I detect a specific parent branch has been clicked?

I know I need something like this in AddSubtree:

connect(jobItem, SIGNAL(triggered()), this, SLOT(AddSubSubtree()));

but I cannot make it work.
Am I lost in the details, or am I on a completely wrong path?

Pietro
  • 12,086
  • 26
  • 100
  • 193
  • If performance is important, your approach will not be fast enough by definition. You need to implement your tree view differently and Qt provides such framework. It called "Model/View architecture". You can refer this tutorial for details: http://qt-project.org/doc/qt-4.8/modelview.html – vahancho Sep 26 '13 at 11:03

2 Answers2

2

I think you may be looking for the signal itemClicked(QTreeWidgetItem * item, int column) from QTreeWidget. I'd try something like:

connect(yourTree, SIGNAL(itemClicked()), yourTree, SLOT(AddSubTree()));

You'd have to subclass TreeWidget and make AddSubTree(QTreeWidgetItem* item, int column) a slot of that subclass. Then in AddSubTree, check the column number is right and if so add the appropriate sub-tree to the argument item.

James Elderfield
  • 2,389
  • 1
  • 34
  • 39
1

you might wish to port to a QAbstractItemModel and a QTreeView

where you implement the QAbstractItemModel to return the data and use canFetchMore(QModelIndex&) and fetchMore to do the actual fetching

ratchet freak
  • 47,288
  • 5
  • 68
  • 106