storing "data" in DataViewListCtrl

135 views
Skip to first unread message

Michael Hipp

unread,
Feb 24, 2012, 12:19:46 PM2/24/12
to wxPython Users
I need to reference an integer id to the rows in a DataViewListCtrl. The
AppendItem() method seems to allow the storing of ClientData with the row:

AppendItem (const wxVector< wxVariant > &values, wxClientData *data=NULL)

So once a row is selected how do I get the wxClientData for the selected row so
I can reference back to the actual db record it came from? I'm not seeing a
method to do that.

Or am I barking up the wrong tree?

Thanks,
Michael

Robin Dunn

unread,
Feb 24, 2012, 2:02:33 PM2/24/12
to wxpytho...@googlegroups.com

Passing client data to that method probably won't work right as it is
taking a sequence of values, and so the C++ method is probably expecting
a matching C array of data values if it is not NULL. However the
wxClientData typemap in wxPython is only going to be able to deal with
one data object there.

However you should be able to use SetItemData and GetItemData instead.
They are methods that wxPython adds on to the class, their C++
prototypes look like this:

PyObject* GetItemData(unsigned int row);
void SetItemData(unsigned int row, PyObject* data);


--
Robin Dunn
Software Craftsman
http://wxPython.org

Michael Hipp

unread,
Feb 24, 2012, 2:12:27 PM2/24/12
to wxpytho...@googlegroups.com
On 2012-02-24 1:02 PM, Robin Dunn wrote:
> However you should be able to use SetItemData and GetItemData instead. They are
> methods that wxPython adds on to the class, their C++ prototypes look like this:
>
> PyObject* GetItemData(unsigned int row);
> void SetItemData(unsigned int row, PyObject* data);

Thanks, that should work.

But I wonder why AppendItem() doesn't return the row that was added. Seems it
would be natural to do:

for key,val in dic.items():
row = ctrl.AppendItem(val)
ctrl.SetItemData(row, key)

Otherwise it appears I'll have to maintain a counter as I loop thru my data
items and my code will look suspiciously like FORTRAN.

Is there a better way I'm missing?

Thanks,
Michael

Robin Dunn

unread,
Feb 24, 2012, 8:08:54 PM2/24/12
to wxpytho...@googlegroups.com
On 2/24/12 11:12 AM, Michael Hipp wrote:
> On 2012-02-24 1:02 PM, Robin Dunn wrote:

I need to correct my earlier statement about AppendItem and friends,
apparently I didn't put my brain in gear when I glanced at the code...

void AppendItem( const wxVariantVector &values, wxClientData *data
= NULL );

While that does expect to get a sequence of items for the first
parameter, it is not for multiple rows as I mentioned before, but rather
for each of the items in a single row. Consequently the data value if
provided should be just a single object and will be associated with that
row.

>> However you should be able to use SetItemData and GetItemData instead.
>> They are
>> methods that wxPython adds on to the class, their C++ prototypes look
>> like this:
>>
>> PyObject* GetItemData(unsigned int row);
>> void SetItemData(unsigned int row, PyObject* data);
>

And you can still use GetItemData to fetch it later, or SetItemData to
change the data value.


> Thanks, that should work.
>
> But I wonder why AppendItem() doesn't return the row that was added.
> Seems it would be natural to do:
>
> for key,val in dic.items():
> row = ctrl.AppendItem(val)
> ctrl.SetItemData(row, key)
>
> Otherwise it appears I'll have to maintain a counter as I loop thru my
> data items and my code will look suspiciously like FORTRAN.
>
> Is there a better way I'm missing?

Well, you know that if you've just appended an item then its row number
will be one less than the number of items in the data store, so
something like this would work:

row = self.GetStore().GetCount()

Michael Hipp

unread,
Feb 25, 2012, 11:55:08 AM2/25/12
to wxpytho...@googlegroups.com
On 2012-02-24 7:08 PM, Robin Dunn wrote:
> I need to correct my earlier statement about AppendItem and friends, apparently
> I didn't put my brain in gear when I glanced at the code...
>
> void AppendItem( const wxVariantVector &values, wxClientData *data = NULL );

> And you can still use GetItemData to fetch it later, or SetItemData to change
> the data value.

This seems to be working very well and is plenty pythonic.

Thanks,
Michael

Reply all
Reply to author
Forward
0 new messages