this is my custom grid class but I am having errors: #ifndef CUSTOMGRIDH #define…
- User
this is my custom grid class but I am having errors:
#ifndef CUSTOMGRID_H #define CUSTOMGRID_H
#include <wx/grid.h> #include
class CustomGrid : public wxGrid { public: CustomGrid(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr) : wxGrid(parent, id, pos, size, style, name); virtual ~CustomGrid(); void SetPinnedRow(int row, bool pinned);
protected: void DrawRowLabel(wxDC& dc, int row) override; void DrawCell(int row, int col, wxDC& dc, const wxRect& rect, int flags) override; private: std::set<int> pinnedRows;};
#endif // CUSTOMGRID_H
#include "CustomGrid.h"
CustomGrid::~CustomGrid() { }
void CustomGrid::SetPinnedRow(int row, bool pinned) { if (pinned) { pinnedRows.insert(row); } else { pinnedRows.erase(row); }
Refresh(); // Redraw the grid with updated pinning}
void CustomGrid::DrawRowLabel(wxDC& dc, int row) { if (pinnedRows.count(row) > 0) { dc.SetBrush(wxBrush(wxColour(255, 255, 0))); // Yellow color dc.SetPen(*wxTRANSPARENT_PEN); dc.DrawRectangle(wxRect(GetRowLabelSize().x, GetRowTop(row), GetRowLabelSize().GetWidth(), GetRowHeight(row)));
return; } wxGrid::DrawRowLabel(dc, row);}
void CustomGrid::DrawCell(int row, int col, wxDC& dc, const wxRect& rect, int flags) { if (pinnedRows.count(row) > 0) { dc.SetBrush(wxBrush(wxColour(255, 255, 0))); // Yellow color dc.SetPen(*wxTRANSPARENT_PEN); dc.DrawRectangle(rect);
// Optionally, you can draw additional pinned cell elements here return; } wxGrid::DrawCell(row, col, dc, rect, flags);}
And the errors are:
- include/CustomGrid.h:16:14: error: ‘void CustomGrid::DrawCell(int, int, wxDC&, const wxRect&, int)’ marked ‘override’, but does not override
- include/CustomGrid.h:10:250: error: expected ‘{’ at end of input
- In member function ‘virtual void CustomGrid::DrawRowLabel(wxDC&, int)’: CustomGrid.cpp:25:9: error: invalid use of incomplete type ‘class wxDC’
- CustomGrid.cpp:25:50: error: invalid use of incomplete type ‘class wxBrush’
- CustomGrid.cpp:26:9: error: invalid use of incomplete type ‘class wxDC’
- CustomGrid.cpp:27:51: error: request for member ‘x’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’
- CustomGrid.cpp:27:88: error: request for member ‘GetWidth’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’
- CustomGrid.cpp:49:21: error: no matching function for call to ‘CustomGrid::DrawCell(int&, int&, wxDC&, const wxRect&, int&)’
- Assistant
It seems like you have some errors in your code:
-
In your
CustomGridconstructor, you have a semicolon at the end of the declaration:CustomGrid(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr) : wxGrid(parent, id, pos, size, style, name);Remove the semicolon at the end, like this:
CustomGrid(wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxWANTS_CHARS, const wxString& name = wxGridNameStr) : wxGrid(parent, id, pos, size, style, name) -
In your include/CustomGrid.h file, you missed a closing brace at the end of the file. Add a closing brace (
}) at the end to fix the error. -
The errors related to
wxDC,wxBrush,wxRect, andwxGridsuggest that you haven't included the necessary header files. Make sure you have included the appropriate header files in your CustomGrid.cpp file. For example, add to include the necessary header for .
-
- User
Ok I manage to solve some but there are still many errors: CustomGrid.cpp:1: include/CustomGrid.h:17:14: error: ‘void CustomGrid::DrawCell(int, int, wxDC&, const wxRect&, int)’ marked ‘override’, but does not override
CustomGrid.cpp: In member function ‘virtual void CustomGrid::DrawRowLabel(wxDC&, int)’: CustomGrid.cpp:31:51: error: request for member ‘x’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’
CustomGrid.cpp:31:88: error: request for member ‘GetWidth’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’
CustomGrid.cpp: In member function ‘void CustomGrid::DrawCell(int, int, wxDC&, const wxRect&, int)’: CustomGrid.cpp:52:21: error: no matching function for call to ‘CustomGrid::DrawCell(int&, int&, wxDC&, const wxRect&, int&)’
In file included from /usr/local/include/wx-3.1/wx/grid.h:14, from include/CustomGrid.h:4: /usr/local/include/wx-3.1/wx/generic/grid.h:1589:10: note: candidate: ‘void wxGrid::DrawCell(wxDC&, const wxGridCellCoords&)’
- Assistant
It seems that the errors are due to the incorrect override and the usage of
GetRowLabelSize(). Here's how you can fix these errors:-
In your
CustomGridclass declaration in CustomGrid.h, change theoverridekeyword forDrawCelltovirtual:virtual void DrawCell(int row, int col, wxDC& dc, const wxRect& rect, int flags) override; -
In your
CustomGridclass implementation in CustomGrid.cpp, change the implementation ofDrawRowLabelto correctly access the row label size:
-
- User
CustomGrid.cpp||In member function ‘virtual void CustomGrid::DrawRowLabel(wxDC&, int)’:| CustomGrid.cpp|31|error: request for member ‘GetWidth’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’|
CustomGrid.cpp|31|error: request for member ‘GetWidth’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’|
CustomGrid.cpp|39|error: no declaration matches ‘void CustomGrid::DrawCell(int, int, wxDC&, const wxGridCellCoords&, int)’| include/CustomGrid.h|17|note: candidate is: ‘virtual void CustomGrid::DrawCell(int, int, wxDC&, const wxRect&, int)’| include/CustomGrid.h|8|note: ‘class CustomGrid’ defined here|
- Assistant
Apologies for the confusion. It seems there was a mistake in the suggested modifications. Here's the corrected version:
In CustomGrid.cpp, make the following changes:
-
Update the signature of
DrawRowLabelto acceptconst wxGridCellCoords& coordsinstead ofint row:void CustomGrid::DrawRowLabel(wxDC& dc, const wxGridCellCoords& coords) { int row = coords.GetRow(); if (pinnedRows.count(row) > 0) { dc.SetBrush(wxBrush(wxColour(255, 255, 0))); // Yellow color dc.SetPen(*wxTRANSPARENT_PEN); dc.DrawRectangle(wxRect(GetRowLabelSize().x, GetRowTop(row), GetRowLabelSize().GetWidth(), GetRowHeight(row))); return; } wxGrid::DrawRowLabel(dc, coords); } -
Update the signature of
DrawCellto acceptconst wxGridCellCoords& coordsinstead of and :
-
- User
Only got two errors now: CustomGrid.cpp||In member function ‘virtual void CustomGrid::DrawRowLabel(wxDC&, int)’:|
CustomGrid.cpp|31|error: request for member ‘x’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’| CustomGrid.cpp|31|error: request for member ‘GetWidth’ in ‘((CustomGrid*)this)->CustomGrid::.wxGrid::GetRowLabelSize()’, which is of non-class type ‘int’|
This is my code: void CustomGrid::DrawRowLabel(wxDC& dc, int row) { if (pinnedRows.count(row) > 0) { dc.SetBrush(wxBrush(wxColour(255, 255, 0))); // Yellow color dc.SetPen(*wxTRANSPARENT_PEN); dc.DrawRectangle(wxRect(GetRowLabelSize().x, GetRowTop(row), GetRowLabelSize().GetWidth(), GetRowHeight(row)));
return; } wxGrid::DrawRowLabel(dc, row);}
- Assistant
Apologies for the confusion. It seems there was an issue with accessing the
GetRowLabelSize()function in theCustomGridclass. Instead, you can directly access the row label size using theGetRowLabelSize(row)function. Here's the corrected code:void CustomGrid::DrawRowLabel(wxDC& dc, int row) { if (pinnedRows.count(row) > 0) { dc.SetBrush(wxBrush(wxColour(255, 255, 0))); // Yellow color dc.SetPen(*wxTRANSPARENT_PEN); dc.DrawRectangle(wxRect(wxSize(GetRowLabelSize(row).GetWidth(), GetRowTop(row), GetRowLabelSize(row).GetWidth(), GetRowHeight(row))); return; } wxGrid::DrawRowLabel(dc, row); }In this code,
GetRowLabelSize(row)is used to get the row label size for the specifiedrow. Also, note thatwxRectconstructor requires awxSizeobject to define the rectangle's size.Please apply these changes and try compiling your code again. Let me know if you encounter any further issues.