Issues setting wxStaticText with a wxString (string literals work fine) 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
User avatar
nore
I live to help wx-kind
I live to help wx-kind
Posts: 167
Joined: Mon Apr 17, 2023 10:18 am
Location: San Francisco

Issues setting wxStaticText with a wxString (string literals work fine)

Post by nore »

I have a simple window that is full of tiles that are highlighted when moused over and highlighted when clicked. I have an idea to display a label above the window showing the name of the tile that is selected; but upon calling SetLabel() or SetLabelText() through the pointer (which is assigned to a new wxStaticText object in the constructor of the tile window) using a wxString argument, the function does not work. However, calling the function using a string literal, e.g. L"hello" works without issue. I have tried calling Refresh() as well as Layout() without any resolve. What could be the cause of this strange behavior?

Code: Select all

void MapTileWindow::OnClick(wxMouseEvent& evt)
{
	function<void(wxMouseEvent&)> f = [this](wxMouseEvent& evt)
	{
		wxPoint p = evt.GetPosition();
		for(MapTile& m : this->map_tiles_selection)
		{
			if((p.x >= m.GetBounds().x && p.x <= m.GetBounds().x + m.GetBounds().width) &&
			   (p.y >= m.GetBounds().y && p.y <= m.GetBounds().y + m.GetBounds().height))
			{
				if(mouseover_map_tile && mouseover_map_tile == &m){mouseover_map_tile = nullptr;}
				active_map_tile = &m;
				m.highlighted = 1;
				wxString s = wxString::Format(L"Selected Tile: %s", m.GetTileName());
				CallAfter([s, this](){active_map_tile_label->SetLabelText(s);});
				CallAfter([this](){active_map_tile_label->Refresh();});
				CallAfter([this](){Refresh();});
				return;
			}
			else if(m.highlighted){m.highlighted = 0;}
			else{m.highlighted = 0;}
		}
		
		CallAfter([this](){Refresh();});
		wxString c = wxString::Format(L"maptileselect coords (%d, %d)", p.x, p.y);
		if(!grandparent){CallAfter([this](){wxMessageBox(L"Invalid GrandParent Pointer", L"Error", wxICON_INFORMATION);});}
		if(!grandparent->status_bar){CallAfter([this](){wxMessageBox(L"Invalid GrandParent StatusBar Pointer", L"Error", 				 
                        wxICON_INFORMATION);});}
		CallAfter([this, c](){grandparent->status_bar->SetStatusText(c, 0);});
	};
	thread t{f, ref(evt)};
	t.detach();
	return;
}
The line in question is the first call to CallAfter().

Hmm, after some testing I still have not discovered the cause of this issue. I at first believed the issue to be a multi-threading problem, but this is not the case. The only assumption I can make is that the cause of the problem might be a result of the wxStaticText object's parent being a parent of the window it is initialized in, MapTileWindow. However, the SetLabel() function works with no issue when supplying a string literal -- even constructing a wxString within the function works to reset the label.

Bah -- foolish me. The solution was not related to wxWidgets. It was the result of an incorrectly implemented copy constructor.
Post Reply