Image in cell of wxGrid? Topic is solved

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
Elmi
Super wx Problem Solver
Super wx Problem Solver
Posts: 293
Joined: Thu Mar 12, 2009 3:23 pm
Location: Germany
Contact:

Image in cell of wxGrid?

Post by Elmi »

After Google sends me to wxPython pages only (where functions are used that do not exist in native wxWidgets) I'll try it here:

I want to create a small grid with an image in the first cell of each column (that reacts on double-click) and a label in the second cell of each column (which simply shows the name of the image in first cell). So that's nothing more than a small icon and its name per every row.

My question: how do I draw an image within a cell of a wxGrid? Or: is it totally oversized to use a wxGrid here?

Elmi
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
i think it is better to use wxListCtrl for that.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Or you could create a new cell renderer that renders images :
http://docs.wxwidgets.org/stable/wx_wxg ... llrenderer
"Keyboard not detected. Press F1 to continue"
-- Windows
Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Re: Image in cell of wxGrid?

Post by Mojo »

Elmi wrote:...

My question: how do I draw an image within a cell of a wxGrid? .....

Elmi
As Auria pointed, you should create a new cell renderer that renders images. It goes something like this:

Code: Select all

class myImageGridCellRenderer : public wxGridCellStringRenderer
{
public:
	virtual void Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool isSelected)
	{
		wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
		
		wxImage cellImage;
		
		if (cellImage.LoadFile(wxT("myimage.jpg")))
		{
			wxBitmap cellBitmap(cellImage);
			dc.DrawBitmap(cellBitmap, rect.x, rect.y);
		}
		else 
		{
			wxLogError(_T("The myimage.jpg, in cell:\n row '%d', column '%d',\n didn't load, does it exist?"), row, col);
			grid.SetCellValue(_T("Here's should be an image"), row, col);
		}
	}
};
Then you could set in the cell you need your renderer:

Code: Select all

m_grid1->SetCellRenderer(0, 0, new myImageGridCellRenderer);
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi Mojo,

I agree with the approach, I have drawn many an image in a grid cell.

Only thing I see with your post:

Code: Select all

                else
                {
                        wxLogError(_T("The myimage.jpg, in cell:\n row '%d', column '%d',\n didn't load, does it exist?"), row, col);
                        grid.SetCellValue(_T("Here's should be an image"), row, col);
                }
Is that if there really isn't an image, I think you would have an infinite loop.
SetCellValue() will cause the cell to refresh, which will call the renderer, which would fail again, then call SetCellValue... perhaps best just to render a big X or something?

Jim
OS: Vista SP1, wxWidgets 2.8.7.
Mojo
Super wx Problem Solver
Super wx Problem Solver
Posts: 401
Joined: Wed Sep 21, 2005 8:17 am
Location: Rostov-on-Don, Southern Russia

Post by Mojo »

JimFairway wrote:Hi Mojo,

I agree with the approach, I have drawn many an image in a grid cell.

Only thing I see with your post:

Code: Select all

                else
                {
                        wxLogError(_T("The myimage.jpg, in cell:\n row '%d', column '%d',\n didn't load, does it exist?"), row, col);
                        grid.SetCellValue(_T("Here's should be an image"), row, col);
                }
Is that if there really isn't an image, I think you would have an infinite loop.
SetCellValue() will cause the cell to refresh, which will call the renderer, which would fail again, then call SetCellValue... perhaps best just to render a big X or something?

Jim
Thanks Jim, I knew that, it was just an example, instead of wxLogError and SetCellValue is't possibly to use any instruction.
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0
Post Reply