Adding 2d array of wxPanel to wxGridSizer

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
Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Sun Sep 01, 2019 3:40 pm

Hello,
I want to place wxPanels representing a grid map into wxGridSizer

Code: Select all

int row = 2;
int col = 4;
wxPanel **cellsArray = new wxPanel*[row];

for(int i = 0; i < row; i++)
{
	  cellsArray[i] = new wxPanel[col];
}

wxGridSizer *gridM = new wxGridSizer(2,4,0,0);
But,
adding wxPanel as an element of cellsArray did not work.

Code: Select all

gridM->Add(cellsArray[1][1]); // did not work
error: no matching function for call to 'wxGridSizer::Add(wxPanel&)'


Adding wxPanel as object worked correctly.

Code: Select all

gridM->Add(new wxPanel(panel, -1)); // it works

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

Re: Adding 2d array of wxPanel to wxGridSizer

Post by doublemax » Sun Sep 01, 2019 9:42 pm

gridM->Add(cellsArray[1][1]); // did not work
cellsArray[1][1] is a wxPanel not a wxPanel* in your case.

I would probably use a stl container here to better readability.

Also, don't forget that each panel needs to be properly created with "new wxPanel(parent,id)"
Use the source, Luke!

Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Re: Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Mon Sep 02, 2019 8:22 am

Thanks for help

Code: Select all

///////////////////////////////////////////////////////////
  int row = 2;
  int col = 4;
  wxGridSizer *gridM = new wxGridSizer(row,col,0,0);
  // 2d array of panels
  wxPanel **cellsArray = new wxPanel*[row]; // allocate rows
  for(int i = 0; i < row; i++)
  {
	  cellsArray[i] = new wxPanel[col]; // allocate columns
  }
  // create wxPanel(s) and store them in the array, then add them to wxGridSizer
  for(int i=0 ; i < row ; i++)
	  for(int j=0 ; j < col ; j++)
	  {
		  wxPanel *newwxPanel = new wxPanel(panel, -1);
		  cellsArray[i][j] = *newwxPanel;
		  // add wxPanel to wxGridSizer
		  gridM->Add(&cellsArray[i][j]);
	  }
  
  ///////////////////////////////////////////////////////////
But the line:

Code: Select all

cellsArray[i][j] = *newwxPanel;
gives an error
error: 'wxPanel& wxPanel::operator=(const wxPanel&)' is private within this context

Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Re: Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Mon Sep 02, 2019 9:27 am

I searched for this error. It is because the copy constructor of wxPanel is private
Any ideas?

Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Re: Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Mon Sep 02, 2019 9:44 am

This works!

Code: Select all

 ///////////////////////////////////////////////////////////
  int row = 2;
  int col = 4;
  wxGridSizer *gridM = new wxGridSizer(row,col,0,0);
  // 2d array of panels
  wxPanel ***cellsArray = new wxPanel**[row]; // allocate rows
  for(int i = 0; i < row; i++)
  {
	  cellsArray[i] = new wxPanel*[col]; // allocate columns
  }
  // create wxPanel(s)
  for(int i=0 ; i < row ; i++)
	  for(int j=0 ; j < col ; j++)
	  {
		  cellsArray[i][j] = new wxPanel(panel, -1);
		  // add wxPanel to wxGridSizer
		  gridM->Add(cellsArray[i][j]);
	  }
But, I need to set borders to the wxPanel so it the grid becomes visible

Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Re: Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Mon Sep 02, 2019 9:56 am

This line compile and run without any errors, but the border still not appear

Code: Select all

Array[i][j] = new wxPanel(panel, -1, wxDefaultPosition, wxDefaultSize,wxBORDER_SIMPLE);

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

Re: Adding 2d array of wxPanel to wxGridSizer

Post by doublemax » Mon Sep 02, 2019 11:10 am

An empty wxPanel probably has a default size of (0,0). Try passing wxSize(16,16) as size. Also try setting a distinct background color, so you see where it is :)
Use the source, Luke!

Eman
Earned a small fee
Earned a small fee
Posts: 13
Joined: Sat Aug 31, 2019 2:11 pm

Re: Adding 2d array of wxPanel to wxGridSizer

Post by Eman » Mon Sep 02, 2019 11:55 am

Thanks a lot for helping
It works perfectly. I'm posting the code here to help others:

Code: Select all

///////////////////////////////////////////////////////////
  int row = 8;
  int col = 8;
  wxGridSizer *gridM = new wxGridSizer(row,col,0,0);
  // 2d array of panels
  wxPanel ***cellsArray = new wxPanel**[row]; // allocate rows
  for(int i = 0; i < row; i++)
  {
	  cellsArray[i] = new wxPanel*[col]; // allocate columns
  }
  // create wxPanel(s)
  for(int i=0 ; i < row ; i++)
	  for(int j=0 ; j < col ; j++)
	  {
		  cellsArray[i][j] = new wxPanel(panel, -1, wxDefaultPosition, wxSize(32,32),wxBORDER_SIMPLE);
		  // add wxPanel to wxGridSizer
		  gridM->Add(cellsArray[i][j]);
	  }
///////////////////////////////////////////////////////////

Post Reply