[Interest] 64-bit internal ID for QModelIndex?

Stephen Chu stephen at ju-ju.com
Wed Aug 22 01:33:16 CEST 2012

On 8/21/12 7:02 PM, Andreas Pakulat wrote:
> Hi,
> On Tue, Aug 21, 2012 at 11:54 PM, Stephen Chu <stephen at ju-ju.com> wrote:
>> On 8/21/12 5:47 PM, Nikos Chantziaras wrote:
>>> Note that I didn't work with QAbstractItemModel before, so perhaps I'm
>>> not the most suitable person here to give advice.  With that being said,
>>> you might be able to allocate like this:
>>>      createIndex(row, column, new qint64(value));
>>> Access the value with:
>>>      *(qint64*)internalPointer();
>>> And when you don't need the QModelIndex anymore, you deallocate like this:
>>>      delete (qint64*)internalPointer();
>>> You should probably create wrappers for those operations.  Or work out a
>>> subclass model.
>>> But again, I didn't work with that part of Qt before, so I might be
>>> talking gibberish here ;-)
>> Actually it makes a lot of sense and I was trying to do the same. But I
>> can not find out when a QModelIndex is no longer in use.
> Right, you cannot figure that out since QModelIndex are pod and
> allocated/discarded whenever they go out of scope. So even though your
> int64 is not used by one instance anymore, it might still be used by
> another instance elsewhere.
> You simply cannot do this due to a stupid API bug in the itemmodel
> API. If you look at createIndex it takes a quint32 overload, but the
> corresponding internalId in QModelIndex returns a 64bit int, thats
> just broken API in Qt. So either you can cope with 32bit integers or
> you'll have to build in 64bit mode and cast the void* into a qint64.
> This bug also survived the api-changes in Qt5 as it seems - sadly.

I was excited to see internalId() returns a qint64. Then realized that 
there's not way to store it. And after reading the source, the ID is 
stored in the void * pointer and cast to qint64 when returned.

A union of quint64 and void * for the store will save me tons of agony. 
Oh well...

More information about the Interest mailing list