Change setText on clic
« on: 21 August 2015, 15:33:56 »
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.

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

        }
    }

Here is the widget :
Code: [Select]
// 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

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Change setText on clic
« Reply #1 on: 21 August 2015, 15:40:56 »
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:
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.

Re: Change setText on clic
« Reply #2 on: 21 August 2015, 15:56:47 »
Nope it doesn't :
Code: [Select]
error: no member named 'setText' in 'tgui::Widget'
            _gui.get("startbutton")->setText("Stop");

Your solution solved it thank you.

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Change setText on clic
« Reply #3 on: 21 August 2015, 16:00:28 »
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:
_gui.get<tgui::Button>("startbutton")->setText("Stop");

Re: Change setText on clic
« Reply #4 on: 21 August 2015, 16:05:05 »
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 :
Code: [Select]
startbutton->bindCallback(std::bind(func, startbutton), tgui::Button::LeftMouseClicked);

the callback loop doesn't work anymore :
Code: [Select]
tgui::Callback callback;
    while (_gui.pollCallback(callback))
    {
        if (callback.id == 1)
        {
            cout<<"hello"<<endl;

        }
 }

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Change setText on clic
« Reply #5 on: 21 August 2015, 16:23:27 »
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.

Re: Change setText on clic
« Reply #6 on: 24 August 2015, 16:00:41 »
Thanks !

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

#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 :
Code: [Select]
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

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: Change setText on clic
« Reply #7 on: 24 August 2015, 16:04:15 »
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:
startbutton->bindCallback(std::bind(&SystemGui::linkStartButton, this, startbutton), tgui::Button::LeftMouseClicked);

Re: Change setText on clic
« Reply #8 on: 24 August 2015, 16:14:29 »
Thanks !