v0.7.3 loadWidgetsFromFile - load child widget

Started by sors, 26 February 2017, 02:31:51

sors

Hi,sorry for my bad english)

I had a problem loading widget.
For example, let it be this:
gui.txt
Panel."info" {
    Position: (0, 668);
    Size: (500, 300);
    Visible: false;

    Renderer {
        BackgroundColor: #967832;
        BorderColor: Black;
        Borders: (0, 0, 0, 0);
    }

    ProgressBar."info_health" {
        Maximum: 100;
        Minimum: 0;
        Position: (10, 40);
        Size: (200, 20);
        TextSize: 12;
        Value: 50;
   
        Renderer {
            BackImage: "data/GUI/theme/theme.png" Part(180, 64, 90, 40) Middle(20, 0, 50, 40);
            BorderColor: Black;
            Borders: (0, 0, 0, 0);
            FrontImage: "data/GUI/theme/theme.png" Part(180, 108, 90, 32) Middle(16, 0, 50, 32);
            TextColorBack: #BEBEBE;
            TextColorFront: #FAFAFA;
        }
    }
}


my code:
gui.loadWidgetsFromFile("gui.txt");
gui_panelInfo = gui.get<tgui::Panel>("info");
tgui::ProgressBar::Ptr  gui_ProgressBarHP;

tgui::ProgressBar::Ptr  gui_ProgressBarHP;
gui_ProgressBarHP = gui.get<tgui::ProgressBar>("info_health");

and everything seems to be OK, but when I write
gui.loadWidgetsFromFile("gui.txt");
gui_panelInfo = gui.get<tgui::Panel>("info");
tgui::ProgressBar::Ptr  gui_ProgressBarHP;

tgui::ProgressBar::Ptr  gui_ProgressBarHP;
gui_ProgressBarHP = gui.get<tgui::ProgressBar>("info_health");
gui_ProgressBarHP->setValue(50);

my app is crash, why ?

texus

The debugger should show you that it crashes because gui_ProgressBarHP is a nullptr.

The gui doesn't know about the existence of the progress bar. It only has the panel as child widget while the panel in turn has the progress bar as child. So you would have to get the progress bar from the panel:
Code (cpp) Select
gui_panelInfo->get<tgui::ProgressBar>("info_health");

Alternatively you can pass "true" as second parameter to the get function. This will search for the widget recursively.
Code (cpp) Select
gui.get<tgui::ProgressBar>("info_health", true);