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
Image in cell of wxGrid? Topic is solved
Or you could create a new cell renderer that renders images :
http://docs.wxwidgets.org/stable/wx_wxg ... llrenderer
http://docs.wxwidgets.org/stable/wx_wxg ... llrenderer
"Keyboard not detected. Press F1 to continue"
-- Windows
-- Windows
-
- 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?
As Auria pointed, you should create a new cell renderer that renders images. It goes something like this:Elmi wrote:...
My question: how do I draw an image within a cell of a wxGrid? .....
Elmi
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);
}
}
};
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
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0
-
- wxWorld Domination!
- Posts: 1059
- Joined: Sun Dec 30, 2007 6:40 pm
- Location: Canada
Hi Mojo,
I agree with the approach, I have drawn many an image in a grid cell.
Only thing I see with your post:
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
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);
}
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.
-
- Super wx Problem Solver
- Posts: 401
- Joined: Wed Sep 21, 2005 8:17 am
- Location: Rostov-on-Don, Southern Russia
Thanks Jim, I knew that, it was just an example, instead of wxLogError and SetCellValue is't possibly to use any instruction.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:Is that if there really isn't an image, I think you would have an infinite loop.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); }
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
Win XP HE SP3, Vista
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0
Xubuntu 12.04 LTS
wxWidgets-2.9.5
wxWidgets-3.0.0