v0.7 - anyway to get container using the gui.get method?

Started by starkhorn, 05 September 2015, 03:31:25


So what I am trying to do here is first setup a main menu panel where I had a background. I then want to put various widgets into the panel such as labels, radio buttons etc.

So I can create the panel without any issues.
Then the draw menu screen, I have a setupLabel function where one of the parameters to pass in is the tgui::Container::Ptr.
Now I use the gui.get to retrieve the panel but this returns type widget::ptr and so i get a compile error. I tried calling getparent but obviously this just returns gui.

Is there any to search the gui for the widget name and get the container back? Note right now it is a panel, but I also want to use setupLabel for say child windows or other container types.

Here is a full working example of what I'm trying to do.

See the drawLoadingScreenGUI() method.
It calls initMainMenuPanel() which adds a panel to gui. I now want to call setupLabel and pass in a ptr to the panel.
As you can see I'm calling gui.get("mainPanel") which does return the widget::ptr but I cannot add a label to a widget, I assume I need a container type?

Code (cpp) Select

#include <TGUI/TGUI.hpp>
#include <string>
#include <math.h>
#define THEME_CONFIG_FILE "C:\\programming\\rome_vs2013\\Debug\\widgets\\Black.txt"

using namespace std;

class Test


sf::RenderWindow gameWindow;
sf::VideoMode desktop;
sf::Event event;
tgui::Callback callback;
tgui::Gui gui;
sf::Texture mainMenuBackGround;

tgui::Theme::Ptr theme;
tgui::Layout windowWidth;
tgui::Layout windowHeight;

void setThemeConfigFile(string passed_filename);
void initBackGroundTextures();
void initMainMenuPanel(sf::Vector2i pos, sf::Vector2i size);
void setupPanel(sf::Vector2i pos, sf::Vector2i size, sf::Color passed_backGroundColour, string passed_panelName, sf::Texture *passed_texture = NULL);
void setupLabel(sf::Vector2i pos, int passed_TextSize, sf::Color passed_colour, string labelText, tgui::Container::Ptr container = NULL);
bool drawLoadingScreenGUI();

desktop = sf::VideoMode::getDesktopMode();
gameWindow.create(sf::VideoMode(desktop.width, desktop.height, desktop.bitsPerPixel), "Test");
gameWindow.setPosition(sf::Vector2i(0, 0));

void Test::setThemeConfigFile(string passed_filename)
// Load the black theme
theme = std::make_shared<tgui::Theme>(passed_filename);

// Get a bound version of the window size
// Passing this to setPosition or setSize will make the widget automatically update when the view of the gui changes
windowWidth = tgui::bindWidth(gui);
windowHeight = tgui::bindHeight(gui);

void Test::initBackGroundTextures()
if (!mainMenuBackGround.loadFromFile("C:\\progamming\\rome_vs2013\\Debug\\images\\loading_screen.png"))
std::cout << "Failed to load mainMenuBackGround image!" << std::endl;

void Test::initMainMenuPanel(sf::Vector2i pos, sf::Vector2i size)
setupPanel(pos, size, sf::Color::Black, "mainPanel", &mainMenuBackGround);

void Test::setupPanel(sf::Vector2i pos, sf::Vector2i size, sf::Color passed_backGroundColour, string passed_panelName, sf::Texture *passed_texture)

if (gui.get(passed_panelName) == NULL)
auto mainMenuPanel = std::make_shared<tgui::Panel>();
mainMenuPanel->setPosition(pos.x, pos.y);
mainMenuPanel->setSize(size.x, size.y);
if (passed_texture != NULL)
gui.add(mainMenuPanel, passed_panelName);

void Test::setupLabel(sf::Vector2i pos, int passed_TextSize, sf::Color passed_colour, string labelText, tgui::Container::Ptr container)
if (container->get(labelText) == NULL)
tgui::Label::Ptr label = theme->load("Label");
container->add(label, labelText);
label->setPosition(pos.x, pos.y);

bool Test::drawLoadingScreenGUI()
sf::Vector2i pos, size((gameWindow.getSize().x), (gameWindow.getSize().y));
initMainMenuPanel(pos, size);

//Setup LoadingScreen Label
setupLabel(pos, 100, sf::Color::Red, "Roman Empire", gui.get("mainPanel"));


return true;

int main()
Test testClass;

    // Main loop
while (testClass.gameWindow.isOpen())
        sf::Event event;
while (testClass.gameWindow.pollEvent(event))
            // When the window is closed, the application ends
            if (event.type == sf::Event::Closed)

            // Pass the event to all the widgets



return 0;


gui.get("mainPanel") will indeed return a Widget::Ptr which is just a typedef for std::shared_ptr<tgui::Widget> which you could cast yourself with std::static_pointer_cast<tgui::Container>. But there is a templated get function that already does exactly that for you:
Code (cpp) Select

Obviously this will lead to undefined behavior if "mainPanel" is not of type tgui::Container, you could use std::dynamic_pointer_cast to test that but that probably won't be necessary here.