Creating a wxStaticBitmap within a class constructor

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
oarzu
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Mar 20, 2009 3:49 pm
Location: El Segundo

Creating a wxStaticBitmap within a class constructor

Post by oarzu »

I have a class myframe that has a wxPanel* called panel. "panel" is where i put all my controls and such. I currently have many wxStaticBitmaps created on "panel" that represent LEDs. Instead of manually creating every LED i want to create a class within myframe called sLED that will create a wxStaticBitmap for every instance of the sLED.
Here's my constructor:
saviLED(int x_pos, int y_pos){
state = 0;
xcord = x_pos;
ycord = y_pos;
//parent = new wxPanel;
bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
my_bitmap = new wxStaticBitmap(panel, wxID_ANY, bmp, wxPoint(xcord,ycord),
wxSize(14,14), wxBU_AUTODRAW);
}
I need to pass the parent window -> myframme::panel to the wxStaticBitmap constructor but i haven't been able to. I've tried [this] and [myframe::panel] or just [panel]. Can anyone tell me how to do this? Thank you.
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Re: Creating a wxStaticBitmap within a class constructor

Post by mc2r »

Hello,

If you could, please use the code tags when posting code. It makes it much easier to read.
oarzu wrote: saviLED(int x_pos, int y_pos){
state = 0;
xcord = x_pos;
ycord = y_pos;
//parent = new wxPanel;
bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
my_bitmap = new wxStaticBitmap(panel, wxID_ANY, bmp, wxPoint(xcord,ycord),
wxSize(14,14), wxBU_AUTODRAW);
}
as you have this declared saviLED is not a class constructor. It is function called saviLED.

Code: Select all

saviLED::saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(this, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}

-Max
oarzu
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Mar 20, 2009 3:49 pm
Location: El Segundo

Post by oarzu »

The code i posted is housed inside the saviLED class definition. A class constructor is a function inside the class with the same name as the class. This particular constructor takes two int arguments. Here's the entire class difinition.

Code: Select all

class saviLED{
		public:
			int GetState();
			void SetState(int new_state);
			saviLED(){
				state = 0;
				xcord = 0;
				ycord = 0;
				//wxString str = "RED";
				//wxBitmap *r_bit = MyFrame::GetBitmap(str);

				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14),wxBU_AUTODRAW);
			}
			saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}
			~saviLED(){
				delete my_bitmap;
			}
		private:
			wxStaticBitmap *my_bitmap;
			wxBitmap bmp;
			wxPanel *parent;
			int state, xcord, ycord;

	};
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r »

oarzu wrote:A class constructor is a function inside the class with the same name as the class.
True but, it needs to be either inside the class definition(which you didn't show) or have the class name and :: prepended. People can only comment on what code is posted.

oarzu wrote:

Code: Select all

class saviLED{
		public:
			int GetState();
			void SetState(int new_state);
			saviLED(){
				state = 0;
				xcord = 0;
				ycord = 0;
				//wxString str = "RED";
				//wxBitmap *r_bit = MyFrame::GetBitmap(str);

				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14),wxBU_AUTODRAW);
			}
			saviLED(int x_pos, int y_pos){
				state = 0;
				xcord = x_pos;
				ycord = y_pos;
				//parent = new wxPanel;
				bmp.LoadFile(wxT("..\\..\\res\\redLED.bmp"), wxBITMAP_TYPE_BMP);
				my_bitmap = new wxStaticBitmap(parent, wxID_ANY, bmp, wxPoint(xcord,ycord),
					wxSize(14,14), wxBU_AUTODRAW);
			}
			~saviLED(){
				delete my_bitmap;
			}
		private:
			wxStaticBitmap *my_bitmap;
			wxBitmap bmp;
			wxPanel *parent;
			int state, xcord, ycord;

	};
Again, I can only comment on the actual code you've posted as I'm not a psychic.

But in both the default constructor and the 2 int argument constructor you have commented out the line where parent is initialized to something. As it is parent is an unitialized pointer and using it for anything is going to result in undefined behavior. Set it to NULL or a new wxPanel();

The reason the using this in the wxStaticBitmap doesn't work is that saviLED is NOT derived from anything. To be used as the parent it needs to be derived from wxWindow or a class derived from wxWindow.

-Max
Post Reply