Hi!
I've created an own model for treeview. At first I load data that is ysed to show the first level of ierarchy. And I know if element has children or has not.
Clild elements should be loaded and shown only after expanding the node.
So, to enable expand and form visual style, i add a "fake" child element "Loading.." for each node that should have children.
My model is based on example from Blanshed & Sammerfield book. I load data using lists, it looks like this:
void modelClients::setLists( treeNodeClient *node,
const QList<int> &p_IDs,
const QList<QString> &p_Names)
{
// Clearing existing node
qDeleteAll(node->children);
node->children.clear();
// Every adding element
for (int i = 0; i < p_IDs.count(); i++)
{
// Creating new element and adding it to existing
node->children.append( new treeNodeClient(p_IDs[i], p_Names[i], node) );
// If condition is true, add a fake child item - loading
if (*condition*)
node->children.last()->children.append(new treeNodeClient(-1, tr("Loading..."), node->children.last() ));
}
// Reset the model
reset();
}
void modelClients::setLists( treeNodeClient *node,
const QList<int> &p_IDs,
const QList<QString> &p_Names)
{
// Clearing existing node
qDeleteAll(node->children);
node->children.clear();
// Every adding element
for (int i = 0; i < p_IDs.count(); i++)
{
// Creating new element and adding it to existing
node->children.append( new treeNodeClient(p_IDs[i], p_Names[i], node) );
// If condition is true, add a fake child item - loading
if (*condition*)
node->children.last()->children.append(new treeNodeClient(-1, tr("Loading..."), node->children.last() ));
}
// Reset the model
reset();
}
To copy to clipboard, switch view to plain text mode
So, to fill the first level I use this code:
mdlClients = new modelClients(this);
ui->trvClients->setModel(mdlClients);
mdlClients->setLists(mdlClients->rootNode, mm_IDs, mm_Names);
mdlClients = new modelClients(this);
ui->trvClients->setModel(mdlClients);
mdlClients->setLists(mdlClients->rootNode, mm_IDs, mm_Names);
To copy to clipboard, switch view to plain text mode
And I thought that to fill other nodes I should use this construction:
void wgtManager
::on_trvClients_expanded(QModelIndex index
) {
...
treeNodeClient *node = static_cast<treeNodeClient*>(index.internalPointer());
mdlClients->setLists(node, mm_IDs, mm_Names);
}
void wgtManager::on_trvClients_expanded(QModelIndex index)
{
...
treeNodeClient *node = static_cast<treeNodeClient*>(index.internalPointer());
mdlClients->setLists(node, mm_IDs, mm_Names);
}
To copy to clipboard, switch view to plain text mode
Please, give me an advice, how to form children dynamically while expanding a node!
Thanks!
Bookmarks