v0.7.3 loadWidgetsFromFile - load child widget
« on: 26 February 2017, 02:31:51 »
Hi,sorry for my bad english)

I had a problem loading widget.
For example, let it be this:
gui.txt
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
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 ?
« Last Edit: 26 February 2017, 02:55:11 by sors »

*

texus

  • *****
  • 1161
    • View Profile
    • Texus's Blog
Re: v0.7.3 loadWidgetsFromFile - load child widget
« Reply #1 on: 26 February 2017, 12:46:58 »
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:
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.
gui.get<tgui::ProgressBar>("info_health", true);

Re: v0.7.3 loadWidgetsFromFile - load child widget
« Reply #2 on: 26 February 2017, 13:45:20 »
thx!