Qt-interest Archive, May 2008
Determine visible Items in a QTreeView
Message 1 in thread
Hi,
I am trying to determine the QModelIndexes of the currently visible Items
in a QTreeView. I am (ab)using it to show a List (so the parent is always
QModelIndex()).
What I am doing so far is to test the following for each item from the
model:
QModelIndex index = mpModel->index(i,0);
QRect rect = mpView->visualRect(index);
if (mpView->viewport()->rect().intersects(rect)) {
doSomething();
}
I am wondering if I overlooked something in the docs which makes it easier
for me to get the currently visible items. I expect my solution to be
rather slow with large models ...
Alternatively I could query my my view with
mpView->indexAt(mpView->viewport->rect()->topLeft()) and
mpView->indexAt(mpView->viewport->rect()->bottomLeft())
and interpolate the indexes in between (since it's just a list and not a
real tree).
Does anyone know if there is a more generic way?
Regards,
Malte
--
[ signature omitted ]
Message 2 in thread
Hi,
"Malte Witt" <malte.witt@xxxxxxxxxxxxx> schreef in bericht
news:OFA68D0896.51771990-ONC1257458.00354BE9-C1257458.0037A0D8@xxxxxxxxxxxxxxxx
> Hi,
>
> I am trying to determine the QModelIndexes of the currently visible Items
> in a QTreeView. I am (ab)using it to show a List (so the parent is always
> QModelIndex()).
>
> What I am doing so far is to test the following for each item from the
> model:
>
>
> QModelIndex index = mpModel->index(i,0);
> QRect rect = mpView->visualRect(index);
>
> if (mpView->viewport()->rect().intersects(rect)) {
> doSomething();
> }
>
>
> I am wondering if I overlooked something in the docs which makes it easier
> for me to get the currently visible items. I expect my solution to be
> rather slow with large models ...
Indeed. This does not seem to be a very good solution...
> Alternatively I could query my my view with
>
> mpView->indexAt(mpView->viewport->rect()->topLeft()) and
> mpView->indexAt(mpView->viewport->rect()->bottomLeft())
>
> and interpolate the indexes in between (since it's just a list and not a
> real tree).
...but this seems to be a pretty good way of doing it. What's wrong with
this solution?
Yours,
André
--
[ signature omitted ]
Message 3 in thread
Hi André,
[snip]
> > Alternatively I could query my my view with
> >
> > mpView->indexAt(mpView->viewport->rect()->topLeft()) and
> > mpView->indexAt(mpView->viewport->rect()->bottomLeft())
> >
> > and interpolate the indexes in between (since it's just a list and not
a
> > real tree).
> ...but this seems to be a pretty good way of doing it. What's wrong with
> this solution?
I now implemented this solution and it is working well (even with huge
models). But I was wondering if there shouldn't be a method like
QList<QModelIndex> QTreeView::visibleIndexes() along with a signal
void visibleIndexesChanged()
The above mentioned solution is working for lists only and will fail on
trees.
I have to admit that my solution degrades the model/view concept to some
degree, but since my data is acuired through a network and constantly
changing I have to optimize this process. That's why I only update the
currently visible items.
But - André - I guess you are right there is nothing wrong with this
solution, I just hoped it would be easier ;-)
Thanks for your answer,
Malte
--
[ signature omitted ]