0.8 Button ToolTips

Started by sors, 30 September 2018, 15:50:07

sors

Hi, why does this code give an error ?
    tgui::Button::Ptr button{tgui::Button::create("MyButton")};
    button->getToolTip()->setText("Hello world");

Quoteerror: 'tgui::Widget::Ptr {aka class std::shared_ptr<tgui::Widget>}' has no member named 'setText'|

texus

The tooltip can be anything, not just a label. You have to create the label yourself and pass it to setToolTip.
See https://tgui.eu/tutorials/v0.7/tool-tips/ (it's a tutorial for 0.7 but I don't think I made any changes to this in 0.8, I'll copy the tutorial to the 0.8 tutorial page on my website later today).

If you must change the text later (i.e. if the text is variable) then you first have to cast the widget to the right type:
Code (cpp) Select
button->getToolTip()->cast<tgui::Label>()->setText("Hello world");

But doing that before calling setToolTip will result in a crash as getToolTip would return a nullptr.

sors

Thanks for the answer.
i have ToolTips in .txt gui file:
    Button."Delete" {
        Position = (0, 0);
        Size = (64, 64);
        Text = "";
        TextSize = 20;

Renderer {
textcolor = rgb(190, 190, 190);
textcolorhover = rgb(250, 250, 250);

texture = "data/gui/tiles/panels/ZonesPanel.png" Part(384, 0, 64, 64);
texturedown = "data/gui/tiles/panels/ZonesPanel.png" Part(384, 64, 64, 64);
texturefocused =  "data/gui/tiles/panels/ZonesPanel.png" Part(384, 0, 64, 64);
texturehover = "data/gui/tiles/panels/ZonesPanel.png" Part(384, 128, 64, 64);
}

        ToolTip {
            DistanceToMouse = (5,20);
            TimeToDisplay = 0.5;
       
            Label {
                AutoSize = true;
                Size = (0, 0);
                Text = "Tooltip text";
                TextSize = 13;
           
                Renderer {
                    backgroundcolor = #00000000;
                    bordercolor = #3C3C3C;
                    borders = (0, 0, 0, 0);
                    textcolor = #3C3C3C;
                }
            }
        }
    }

Then I will not need to assign a button ToolTips ?
setToolTip 

texus

Indeed, setToolTip will automatically be called when loading the button from that text file.
So you just need to do the cast to tgui::Label if you want to change its text after loading it.

sors