Hi,
thx to amicitas and other posts in this thread I was able to build a tree from sql. But I am not using a model: On startup i execute a query for the parent items (children of the rootitem). My problem was, that I want to load only the data form the database that are visible at the beginning otherwise the whole thing is too slow. When the user clicks a parent (all of them have children) I execute another query to load the childitems. This is quite fast and works. My problem is, that i am not able to connect to the expanded() signal of the treeView. Right now I am hiding the decoration (setRootIsDecorated(false)) and use clicks on items within the view to expand or collapse. The expanding is done within the model ...
connect(ui.
treeView,
SIGNAL(expanded
(QModelIndex)), model,
SLOT(expand
(QModelIndex)));
//<-- no way. infinite loop
{
QModelIndex proxyIndex
= index.
sibling(index.
row(),
0);
if (!hasChildren(sourceIndex))
return;
TreeItem *item = getItem(sourceIndex);
if (item->childCount() > 0) {
if (view->isExpanded(proxyIndex)) {
view->collapse(proxyIndex);
return;
}
view->expand(proxyIndex);
}
else {
.. execute query
setupModelData(q, item);
view->expand(proxyIndex);
}
}
connect(ui.treeView, SIGNAL(clicked(QModelIndex)), model, SLOT(expand(QModelIndex)));
connect(ui.treeView, SIGNAL(expanded(QModelIndex)), model, SLOT(expand(QModelIndex))); //<-- no way. infinite loop
void TreeModel::expand(const QModelIndex &index)
{
QModelIndex idx = proxy->mapToSource(index);
QModelIndex sourceIndex = idx.sibling(idx.row(), 0);
QModelIndex proxyIndex = index.sibling(index.row(), 0);
if (!hasChildren(sourceIndex))
return;
TreeItem *item = getItem(sourceIndex);
if (item->childCount() > 0) {
if (view->isExpanded(proxyIndex)) {
view->collapse(proxyIndex);
return;
}
view->expand(proxyIndex);
}
else {
QSqlQuery q;
.. execute query
setupModelData(q, item);
view->expand(proxyIndex);
}
}
To copy to clipboard, switch view to plain text mode
This is not a very nice solution. I'd rather prefer to use the default rootdecoration. But i can not find a way to get the expanded signal and load the data: Nothing is dispayed because the signal is emitted after the view is updated (At that point there is no data). If I call the expand slot again after the query I end up in an infinite loop ... I took a look at the Qt source code, but all parts that are required to cache the expansion (the click on the decoration [+] ) before the model is checked for children are part of QTreeViewPrivate. Sorry for the long explanation, but maybe someone has an idea :-)
Bookmarks