Getting user data from wxListCtrl item

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4523
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Getting user data from wxListCtrl item

Post by ONEEYEMAN » Sun Sep 06, 2020 10:29 pm

Hi,

Code: Select all

struct MyData
{
int a;
double b;
};

long item = m_list->InsertItem( position, item );
MyData data;
data.a = 5;
data.b = 10.0;
m_list->SetItemPtrData( item, wxUIntPtr( &data ) );
Now I need a way to retrieve the data:

Code: Select all

wxUIntPtr dataPtr = m_list->GetItemData( position );
MyData *data = reinterpret_cast<MyData *>( &dataPtr );
results in a wrong data.

What is the proper way to do it?

Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 15262
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Getting user data from wxListCtrl item

Post by doublemax » Mon Sep 07, 2020 4:52 am

Looks ok in principle, you just have to create MyData on the heap.
Use the source, Luke!

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2654
Joined: Sun Jan 03, 2010 5:45 pm

Re: Getting user data from wxListCtrl item

Post by PB » Mon Sep 07, 2020 5:23 am

In addition to what doublemax wrote, I think there is another bug in your code. You are using the address of dataPtr (&dataPtr) instead of dataPtr itself.
ONEEYEMAN wrote:
Sun Sep 06, 2020 10:29 pm

Code: Select all

wxUIntPtr dataPtr = m_list->GetItemData( position );
MyData *data = reinterpret_cast<MyData *>( &dataPtr );
BTW, why create a new topic discussing the same issue as the one you have recently created? The code I posted there was correct.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4523
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Getting user data from wxListCtrl item

Post by ONEEYEMAN » Mon Sep 07, 2020 7:24 am

doublemax,
doublemax wrote:
Mon Sep 07, 2020 4:52 am
Looks ok in principle, you just have to create MyData on the heap.
So this should be a pointer?
And I presume I don't have to delete it as it will be owned by the control.

Thank you.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2654
Joined: Sun Jan 03, 2010 5:45 pm

Re: Getting user data from wxListCtrl item

Post by PB » Mon Sep 07, 2020 7:27 am

This needs to be a pointer to HEAP, not pointer to stack.

And obviously, you must delete it yourself. How do you expect the control would do that for an opaque pointer (e.g., how would a dtor be called)?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4523
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Getting user data from wxListCtrl item

Post by ONEEYEMAN » Mon Sep 07, 2020 8:14 am

Hi,
PB wrote:
Mon Sep 07, 2020 7:27 am
This needs to be a pointer to HEAP, not pointer to stack.
Yes, I understand.
So, the code should read:

Code: Select all

struct MyData
{
int a;
double b;
};

long item = m_list->InsertItem( position, item );
auto data = new MyData;
data.a = 5;
data.b = 10.0;
m_list->SetItemPtrData( item, wxUIntPtr( data ) );
PB wrote:
Mon Sep 07, 2020 7:27 am
And obviously, you must delete it yourself. How do you expect the control would do that for an opaque pointer (e.g., how would a dtor be called)?
Thank you.

Post Reply