Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, August 2006
RE: [Qt4.1] Model/View Design and Use (QTree) --and [Qt4.2]


Message 1 in thread

> From: Benjamin Meyer [mailto:bmeyer@xxxxxxxxxxxxx]
> Sent: Wednesday, August 30, 2006 3:59 AM
> 
> I have started a wiki page on KDE's wiki, feel free to add to it.
> http://wiki.kde.org/tiki-index.php?page=ItemView+Tips

"The internal pointer for a QModelIndex must *not* change over time and
must be universally unique."

Really?  Universally unique?  Or just unique within a model?  Otherwise,
I'm very scared about my use of createIndex(row, col, id);

-----------------------------------------------------------------------
DISCLAIMER:  Information contained in this message and/or attachment(s) may contain confidential information of Zetec, Inc. If you have received this transmission in error, please notify the sender by return email.

Ce courrier électronique est confidentiel et protégé. L'expéditeur ne renonce pas aux droits et obligations qui s'y rapportent. Toute diffusion, utilisation ou copie de ce message ou des renseignements qu'il contient par une personne autre que le (les) destinataire(s) désigné(s) est interdite. Si vous recevez ce courrier électronique par erreur, veuillez m'en aviser immédiatement, par retour de courrier électronique ou par un autre moyen.
-----------------------------------------------------------------------

--
 [ signature omitted ] 

Message 2 in thread

> "The internal pointer for a QModelIndex must *not* change over time and
> must be universally unique."
> 
> Really?  Universally unique?  Or just unique within a model?  Otherwise,
> I'm very scared about my use of createIndex(row, col, id);

Maybe I'll start a webpage where we can all post what internal pointers
we're using so we make sure not to use the same ones as anybody else. ;)


--
 [ signature omitted ] 

Message 3 in thread

> Benjamin spaketh:
> > "The internal pointer for a QModelIndex must *not*
> > change over time and must be universally unique."
> > 

> Tom respondeth:
> > Really?  Universally unique?  Or just unique
> > within a model?  Otherwise, I'm very scared about
> > my use of createIndex(row, col, id);

Nathan respondeth:
> Maybe I'll start a webpage where we can all post
> what internal pointers we're using so we make sure
> not to use the same ones as anybody else. ;)

:-))

I'm sure Benjamin intended to suggest "universally
unique *within* the model".  ;-))

My source of information for my conclusion:

* When the indices are *not* unique, the wrong
node gets "expanded" in my tree model (another
index with the same internalPointer() got expanded)

* After detailed search, I found in the Qt docs:

<http://doc.trolltech.com/4.1/qabstractitemmodel.html>

  "Custom models need to create model indexes for
  other components to use. To do this, call
  createIndex() with suitable row and column numbers
  for the item, and supply a unique identifier for
  the item, either as a pointer or as an integer
  value. Custom models typically use these unique
  identifiers in other reimplemented functions to
  retrieve item data and access information about
  the item's parents and children. See the Simple
  Tree Model example for more information about
  unique identifiers." -- Qt docs, Trolltech

Of course, even now, I'm interested in learning
more about the implementation for how QModelIndex
instances really work, or other elaboration of
this concept.

(As a guess, it looks like in *some scenarios*, 
the unique internal pointer is a "key/hash" to
access some internal node state directly/efficiently.
I'm not qualified to identify all these cases, but
I think I've seen some of them.)

Also, from Benjamin's wiki at:
<http://wiki.kde.org/tiki-index.php?page=ItemView+Tips>

...I'm not sure I understand the, "Implementing
QAbstractItemModel::parent()":

- Does this mean it's ok for me to do this?

QModelIndex MyModel::parent(const QModelIndex& index)
const
{
  // ...figure out the parent row and internal
  //    pointer...
  // ...CAN I DO THIS TO AVOID OVERHEAD OF
  //    createIndex()?...
  return QModelIndex(parent_row, 0, parent_internal);
/*
  ...old...
  return createIndex(parent_row, 0, parent_internal);
*/
}

--charley




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

--
 [ signature omitted ] 

Message 4 in thread

On Thursday 31 August 2006 00:32, Charley Bay wrote:
> > Benjamin spaketh:
> > > "The internal pointer for a QModelIndex must *not*
> > > change over time and must be universally unique."
> >
> > Tom respondeth:
> > > Really?  Universally unique?  Or just unique
> > > within a model?  Otherwise, I'm very scared about
> > > my use of createIndex(row, col, id);
>
> Nathan respondeth:
> > Maybe I'll start a webpage where we can all post
> > what internal pointers we're using so we make sure
> > not to use the same ones as anybody else. ;)
> >
> :-))
>
> I'm sure Benjamin intended to suggest "universally
> unique *within* the model".  ;-))

Yah, a really bad copy/paste job, it has been corrected.

[snip]

> Of course, even now, I'm interested in learning
> more about the implementation for how QModelIndex
> instances really work, or other elaboration of
> this concept.
>
> (As a guess, it looks like in *some scenarios*,
> the unique internal pointer is a "key/hash" to
> access some internal node state directly/efficiently.
> I'm not qualified to identify all these cases, but
> I think I've seen some of them.)

a few months ago there was a thread here about different ways you could 
implement parent, some of them touched on different ideas such as using a 
hash table. 

> Also, from Benjamin's wiki at:
> <http://wiki.kde.org/tiki-index.php?page=ItemView+Tips>
>
> ...I'm not sure I understand the, "Implementing
> QAbstractItemModel::parent()":
>
> - Does this mean it's ok for me to do this?
>
> QModelIndex MyModel::parent(const QModelIndex& index)
> const
> {
>   // ...figure out the parent row and internal
>   //    pointer...
>   // ...CAN I DO THIS TO AVOID OVERHEAD OF
>   //    createIndex()?...
>   return QModelIndex(parent_row, 0, parent_internal);
> /*
>   ...old...
>   return createIndex(parent_row, 0, parent_internal);
> */
> }
>
> --charley

Nope, you can't do that, you have to either createIndex or call 
index(parent_row, 0, parent->parent).  I recommend calling index() to reduce 
the number of places where createIndex() is called.  Having "valid" index 
passed around (whos internal pointer is wrong or row/column number are out of 
bound) is not fun to debug.  index()'s job is to make sure that it is 
returning a valid index only if it really does exists.  So calling it in 
parent() can snake out a few bugs early on when parent_row and grandparent 
are wrong.

-Benjamin Meyer

--
 [ signature omitted ]