qt-interest@trolltech.com
[Top] [All Lists]

Re: Model/View (qt4 )

Subject: Re: Model/View qt4
From: "Maik Jorra"
Date: Mon, 20 Mar 2006 17:54:36 +0100
On Mon, 20 Mar 2006 17:41:15 +0100, Marian Popeanga <marian@xxxxxxxxxxxxxxxx> wrote:


Sounds interesting. Basicaly i need rowCount() for those high level items to return 1 and when index() is called for them to actualy create their childs

   Seems the best aproach so far.
Marian


rowCount( ) must return the real number of rows (children) under the given item. To mark the item as expandable just let the hasChildren( ) method of the model return true for every valid index, because the item is first queried if it has children then its marked es expandable in the view. If the user expands it then
rowCount() is called for the items index.

Regards
Maik Jorra

On Mon, 20 Mar 2006, Maik Jorra wrote:





Hello Marian,

I used following approach to accomplish this.

If an tree view item is expanded the row count of this item is queried
from the model using
int QAbstractItemModel::rowCount( const QModelIndex & par ) const;
So you can use this method to trigger the loading of child
contents (kind of lazy fetching). Put a flag like "bool Populated" in
the items implementation, which indicates that an item is already
expanded, and equip each item this a method "int GetChildCnt( )" which
delivers the number of children of the item. If the item is already
populated simply return the children count, else populate it first and
then return the child count.

Example:

The rowCount usually looks like:

int CMyItemModel::rowCount( const QModelIndex & par ) const{

   CMyItem *parent;

   if (!par.isValid())
       parent = RootItem;
   else
       parent = static_cast<CMyItem*>( par.internalPointer() );

   return parent->GetChildCnt();
}

The implementation of GetChildCnt will realize the lazy fetching of
data.

int CMyItem::GetChildCnt( ){

   if(! Poputlated )
       Populate( );
       Populated = true;
   }

   return ChildCnt;
}

Regards
Maik Jorra




On Mon, 20 Mar 2006 15:21:07 +0100, Marian Popeanga <marian@xxxxxxxxxxxxxxxx> wrote:

        Hello guys,
       I wasn't able to find out a solution for the following problem:
I want to implement a large Tree Model. In order to do this in an efficient way, i need to populate child items only when the user tryes to expand their parent items. Initialy the model wil have only high level items.
        Any ideas are highly apreciated.
 Thanks,
Marian
 --
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/



--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/




--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>