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

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

Postby kathbeau » Wed Dec 17, 2014 4:54 pm

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: 11297
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Postby doublemax » Wed Dec 17, 2014 5:56 pm

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
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 516
Joined: Mon Apr 30, 2012 11:07 pm

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

Postby Manolo » Wed Dec 17, 2014 6:00 pm

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: 27
Joined: Fri Nov 28, 2014 6:30 pm

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

Postby kathbeau » Wed Dec 17, 2014 7:34 pm

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

Kathleen


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 12 guests