Want to associate a pointer to object to each row of wxGrid

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
kathbeau
Knows some wx things
Knows some wx things
Posts: 48
Joined: Fri Nov 28, 2014 6:30 pm

Want to associate a pointer to object to each row of wxGrid

Post by kathbeau »

I'm new to wxWidgets, but have been using the Visual Component Library (C++ Builder and Delphi) for 18 years.

The VCL's TStringGrid class has an Objects array into which I can store pointers to objects representing information about the contents of a grid row. I generally store objects that have the source data primary key plus state information; I call them "cargo". (I maintain a separate list of these cargo objects for managing their destruction.) When a user performs an action on a row of the grid, I can quickly retrieve the "cargo" from the Objects array and use its information as needed.

Does a comparable concept exist in the wxWidgets list/grid controls? I'm not seeing it in the documentation, but wanted to ask before I build a Rube Goldberg solution of my own!

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

Re: Want to associate a pointer to object to each row of wxG

Post by doublemax »

You could try to derive your own class from wxGridCellAttr, store additional data in it and use wxGrid::SetRowAttr() to set it for each row.

But i'd suggest to derive your own class from wxGridTableBase and set it with wxGrid::SetTable(). In that case you manage all data yourself, wxGrid just displays it.
Use the source, Luke!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: Want to associate a pointer to object to each row of wxG

Post by Manolo »

wxGrid has many features, included the one you ask for.
http://docs.wxwidgets.org/trunk/overview_grid.html

Data is shown in the window as a string. This means "some one" translates data into a wxString. This is a wxGridCellRenderer.
Data in a cell may be edited by the user, using a wxGridCellEditor.
Cells/rows/cols may have "attributes" such as colour, font, etc. wxGridCellAttr is used for it.
Data, attributes and renderers may be stored in "grid table", derived from wxGridTableBase. In fact, wxGrid uses a default table; but you can tell wxGrid to use your own table.
wxWidgets provides many ready-to-use objects of those types.

In your case, you need to derive a table from wxGridTableBase and add your own relation between cells and your data.
See your/wx/dir/samples/grid for how to achieve it.
kathbeau
Knows some wx things
Knows some wx things
Posts: 48
Joined: Fri Nov 28, 2014 6:30 pm

Re: Want to associate a pointer to object to each row of wxG

Post by kathbeau »

Thank you so much! I'll study the BugsGridTable example in wx/samples/grid to gain understanding.

Kathleen
Post Reply