Change WxPropertyGrid based on button click 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
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Change WxPropertyGrid based on button click

Post by gillp28 »

I have a property window that is going to be controlled from the list. Every item on the list corresponds to the same property window but with different colour and label values. The colour is just going to be the background colour of the item. The only question i have is how do i change a property or the colour that has been appended to the wxPropertyGrid based on a new selection.

grid = new wxPropertyGrid(treePanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxPG_AUTO_SORT | wxPG_SPLITTER_AUTO_CENTER| wxPG_DEFAULT_STYLE);
wxPGProperty* prop = grid->Append(new wxColourProperty(wxT("Label Colour"),wxPG_LABEL,wxColour(242, 109, 0)));
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Change WxPropertyGrid based on button click

Post by evstevemd »

You will need to read this first
In your case you will need to catch relevant event.
To understand which item was clicked you will need to catch event of interest and use the unique names to differentiate items

//add the property in the grid
m_editorGeneralProps->Append(new wxPropertyCategory(_("Label"), "unique-name-here"));

Code: Select all

void MyClass::OnGridPropChanged(wxPropertyGridEvent &event) // catching wxEVT_PG_SELECTED
{
    wxPGProperty *property = event.GetProperty();
    wxString name = property->GetName();
    wxVariant value = property->GetValue();
    
    if(name == "unique-name-here")
    {
    	//change color here
    }
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Change WxPropertyGrid based on button click

Post by gillp28 »

wxPGProperty* pg = grid->Append(new wxColourProperty(wxT("Label Colour"),wxPG_LABEL,
wxColour(242, 109, 0)));

grid->ChangePropertyValue(pg,new wxColourProperty(wxT("Label Colour"), wxPG_LABEL,
wxColour(*wxBLACK)));

if I just want to change the colour property can I do it using the change property value? I tried this but it's not working
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Change WxPropertyGrid based on button click

Post by evstevemd »

gillp28 wrote: Tue Jul 09, 2019 2:40 pm wxPGProperty* pg = grid->Append(new wxColourProperty(wxT("Label Colour"),wxPG_LABEL,
wxColour(242, 109, 0)));

grid->ChangePropertyValue(pg,new wxColourProperty(wxT("Label Colour"), wxPG_LABEL,
wxColour(*wxBLACK)));

if I just want to change the colour property can I do it using the change property value? I tried this but it's not working
Do you want to do that immediately or you do it in some handler? Can you make small example?
I have never tested this but if you want to change property you can try this

Code: Select all

void MyClass::OnGridPropChanged(wxPropertyGridEvent &event) // catching wxEVT_PG_SELECTED
{
    wxPGProperty *property = event.GetProperty();
    wxPGPropArgCls pid(property);
    
    wxColor color = *wxBLACK;
    wxVariant value(color);
    grid->ChangePropertyValue(pid, value);
    
}
Cannot help further without illustrating your case with sample example of what you are doing!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Change WxPropertyGrid based on button click

Post by gillp28 »

This is the sample I'm working on. So based on the item selected on the listview the property window selected colour changes.

Code: Select all

wxTreeCtrlFrame::wxTreeCtrlFrame(wxWindow* parent, wxWindowID id)
{
	Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
	SetClientSize(wxSize(500, 500));

	wxPanel* treePanel = new wxPanel(this, wxID_ANY);
	wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* horSize = new wxBoxSizer(wxHORIZONTAL); 

	listView = new wxListView(treePanel, ID_TREELISTCTRL1, wxDefaultPosition, wxDefaultSize);
	wxListItem NewItem;
	grid = new wxPropertyGrid(treePanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxPG_AUTO_SORT | wxPG_SPLITTER_AUTO_CENTER| wxPG_DEFAULT_STYLE); 
	mainSizer->Add(listView, 1, wxEXPAND | wxALL, 4);
	mainSizer->Add(grid, 3, wxEXPAND | wxALL, 5); 

	horSize->Add(addNodeBtn); 
	horSize->Add(deleteNodeBtn);
	horSize->Add(editNodeBtn); 
	mainSizer->Add(horSize, 0 , wxALIGN_TOP | wxALIGN_RIGHT | wxTOP | wxRIGHT | wxBOTTOM, 4);
	treePanel->SetSizer(mainSizer);

	listView->AppendColumn("Column 1");
	listView->AppendColumn("Column 2");

	// Add three items to the list
	listView->InsertItem(0, "Item 1");
	listView->SetItem(0, 1, "Amber");
	listView->SetItemBackgroundColour(0, *wxRED); 
	listView->InsertItem(1, "Item 2");
	listView->SetItem(1, 1, "Blue");
	listView->SetItemBackgroundColour(1, *wxRED); 

	listView->InsertItem(2, "Item 3");
	listView->SetItem(2, 1, "Cyan");
	listView->SetItemBackgroundColour(2, *wxRED); 

   pg =  grid->Append(new wxColourProperty(wxT("Label Colour"),wxPG_LABEL,
		wxColour(242, 109, 0)));

	Connect(idMenuQuit, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)& wxTreeCtrlFrame::OnQuit);
	Connect(idMenuAbout, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)& wxTreeCtrlFrame::OnAbout);
	Connect(ID_TREELISTCTRL1, wxEVT_COMMAND_LIST_ITEM_SELECTED, (wxObjectEventFunction)& wxTreeCtrlFrame::OnNodeSelected);
	//*)
}


wxTreeCtrlFrame::~wxTreeCtrlFrame()
{
	//(*Destroy(wxTreeCtrlFrame)
	//*)
}

void wxTreeCtrlFrame::OnQuit(wxCommandEvent& event)
{
	Close();
}

void wxTreeCtrlFrame::OnAbout(wxCommandEvent& event)
{
	wxString msg = wxbuildinfo(long_f);
	wxMessageBox(msg, _("Welcome to..."));
}

void wxTreeCtrlFrame::OnNodeSelected(wxTreeListEvent& event)
{
	 wxColour back = listView->GetItemBackgroundColour(listView->GetFocusedItem());
	 grid->SetPropertyValue(pg, new wxColourProperty(wxT("Label Colour"), wxPG_LABEL,
		 back));
}
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Change WxPropertyGrid based on button click

Post by evstevemd »

Looking at your code, why do you even use wxPropertyGrid? You have only one property that need to change color.
Have you checked wxColorPickerCtrl?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Change WxPropertyGrid based on button click

Post by gillp28 »

I will be adding more properties after but for now just trying to get the colour part to work.
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Change WxPropertyGrid based on button click

Post by evstevemd »

gillp28 wrote: Tue Jul 09, 2019 5:30 pm I will be adding more properties after but for now just trying to get the colour part to work.
Appending change it to include unique name

Code: Select all

pg =  grid->Append(new wxColourProperty(wxT("Label Colour"),"unique-name-here", wxColour(242, 109, 0)));
Then try this code for event. Untested but according to docs it should work

Code: Select all

void wxTreeCtrlFrame::OnNodeSelected(wxTreeListEvent& event)
{
	 wxColour back = listView->GetItemBackgroundColour(listView->GetFocusedItem());
	 wxPGPropArgCls pid("unique-name-here");
    	wxVariant value(back);
    	grid->ChangePropertyValue(pid, value);
}
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
gillp28
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue Jun 25, 2019 3:19 pm

Re: Change WxPropertyGrid based on button click

Post by gillp28 »

Thanks
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Change WxPropertyGrid based on button click

Post by evstevemd »

gillp28 wrote: Mon Jul 15, 2019 6:43 pmThanks
Did that work?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Post Reply