Change setText on clic

Started by Austriker, 21 August 2015, 15:33:56

Austriker

Hi,

I am having trouble finding a solution to my problem. I would like to change the state of my button when it's triggered. But I can't find the way to do it.


tgui::Callback callback;
    while (_gui.pollCallback(callback))
    {
        if (callback.id == 1)
        {
            _gui.get("startbutton")->setText("Stop");

        }
    }


Here is the widget :

// Create the login button
    tgui::Button::Ptr startbutton(gui, "startbutton");
    startbutton->load("./widgets/Black.conf");
    startbutton->setSize(260, 60);
    startbutton->setPosition(270, 300);
    startbutton->setText("Start");
    startbutton->bindCallback(tgui::Button::LeftMouseClicked);
    startbutton->setCallbackId(1);


Thanks for your help,

texus

Doesn't that code work?

Anyway, it's probably better to connect a function instead of using the pollCallback for things like this. Something like this:
Code (cpp) Select
void func(tgui::Button::Ptr button)
{
    if (button->getText() == "Start")
        button->setText("Stop");
    else
        button->setText("Start");
}

startbutton->bindCallback(std::bind(func, startbutton), tgui::Button::LeftMouseClicked);


You might want to learn about std::bind if you don't know how to use it yet, it allows binding practiacally anything which is why tgui relies on it.

Austriker

Nope it doesn't :

error: no member named 'setText' in 'tgui::Widget'
            _gui.get("startbutton")->setText("Stop");


Your solution solved it thank you.

texus

Just so that you don't get similar problems later on: the get function returns a Widget which is the base class of all widgets and thus has no setText function. It should first be cast to a Button, but there is a function in tgui that does that for you in one step:
Code (cpp) Select
_gui.get<tgui::Button>("startbutton")->setText("Stop");

Austriker

ok,

I saw that solution in the V05 full example but since it wasn't in the V06 i thought it was deprecated.

When i use this solution :

startbutton->bindCallback(std::bind(func, startbutton), tgui::Button::LeftMouseClicked);


the callback loop doesn't work anymore :

tgui::Callback callback;
    while (_gui.pollCallback(callback))
    {
        if (callback.id == 1)
        {
            cout<<"hello"<<endl;

        }
}

texus

It's because a callback is only buffered for later retrieval in the callback loop when you don't pass a function to bindCallback.

The callback loop is actually kindof depreciated, it no longer exists in v0.7-dev.

You should read the callback tutorial for more information.

Austriker

Thanks !

I am having an issue integrating the callback in a class :

Code (cpp) Select

#ifndef GUI_HPP
#define GUI_HPP

#include <TGUI/TGUI.hpp>
#include "manage_state.hpp"

class SystemGui
{
public:
    SystemGui(ManageState &manage_state);
    void loadWidgets(tgui::Gui& gui);

private:
    void linkStartButton(tgui::Button::Ptr button);
    ManageState &_manage_state;

};

#endif // GUI_HPP

//////////////////////////////////////////////////
#include "gui.hpp"

SystemGui::SystemGui(ManageState &manage_state) :
    _manage_state{manage_state}
{}

void SystemGui::loadWidgets( tgui::Gui& gui )
{
    // Create the login button
    tgui::Button::Ptr startbutton(gui);
    startbutton->load("./widgets/Black.conf");
    startbutton->setSize(260, 60);
    startbutton->setPosition(270, 300);
    startbutton->setText("Start");
    startbutton->bindCallback(std::bind(linkStartButton, startbutton), tgui::Button::LeftMouseClicked);
    startbutton->bindCallback(tgui::Button::LeftMouseClicked);
    startbutton->setCallbackId(1);
}

void SystemGui::linkStartButton(tgui::Button::Ptr button)
{
    _manage_state.updateStart();
    if (button->getText() == "Start")
    {
        button->setText("Stop");
    } else {
        button->setText("Start");
    }
}


I get this error :

error: reference to non-static member function must
      be called
    startbutton->bindCallback(std::bind(linkStartButton, startbutton), tgui::Button::LeftMous...


I can't manage to find the solution.

Sincerely,

texus

This is due to some technical detail in C++, for a quick explanation read the Connecting member functions part from the v0.7-dev tutorial.

The line would have to be:
Code (cpp) Select
startbutton->bindCallback(std::bind(&SystemGui::linkStartButton, this, startbutton), tgui::Button::LeftMouseClicked);

Austriker