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?