0

There is a question similar to this but it does not answer my question. I am working on a GUI using GTKMM. I am trying to pass a single char in the callback of my button.

This letter, is then assigned to the global variable letter. However, I don't understand pointers and have been trying to get this to work for quite a while without success.

main.cpp

#include <gtkmm.h>
#include "window.h"

int main(int argc, char *argv[])
{
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.gtkmm.tutorial3.base");

    mywindow window;

    return app->run(window);
}

window.cpp

#include "window.h"

mywindow::mywindow()
{
    set_default_size(480, 320);
    set_title("Transfer");
    Gtk::Box *vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0));
    add(*vbox);

    Gtk::Grid *grid = Gtk::manage(new Gtk::Grid);
    grid->set_border_width(10);
    vbox->add(*grid);

    Gtk::Button *a = Gtk::manage(new Gtk::Button("A"));
    //a->set_hexpand(true);
    //a->set_vexpand(true);//%%%%%%%% next line is the issue%%%%%%%%%
    a->signal_clicked().connect(sigc::mem_fun(*this, &mywindow::on_click('a')));
    grid->attach(*a, 0, 0, 1, 1);//x=0,y=0, span 1 wide, and 1 tall

    Gtk::Button *b = Gtk::manage(new Gtk::Button("B"));
    //b->set_hexpand(true);
    //b->set_vexpand(true);
    b->signal_clicked().connect(sigc::mem_fun(*this, &mywindow::on_click('b')));
    grid->attach(*b, 1, 0, 1, 1);

    Gtk::Button *c = Gtk::manage(new Gtk::Button("C"));
    //c->set_hexpand(true);
    //c->set_vexpand(true);
    c->signal_clicked().connect(sigc::mem_fun(*this, &mywindow::on_click('c')));
    grid->attach(*c, 2, 0, 1, 1);

    vbox->show_all();
}

mywindow::~mywindow()
{
    //dtor
}

void mywindow::on_click(char l)
{
    letter = l;
}

window.h

#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <gtkmm.h>

class mywindow : public Gtk::Window
{
    public:
        mywindow();
        virtual ~mywindow();
    protected:
        char letter;//global variable where the letter is stored
        char on_click(char l);
    private:
};
#endif // MYWINDOW_H

I tried replacing the * pointer with & and vice-versa for this and mywindow but I haven't gotten it to work and have no idea how to proceed.

NULL
  • 115
  • 8

1 Answers1

0

First of all there is gtkmm3 tutorial.

From there:

you can't hook a function with two arguments to a signal expecting none (unless you use an adapter, such as sigc::bind(), of course).

So you need something like this:

 c->signal_clicked().connect(sigc::bind<char>(sigc::mem_fun(*this,&mywindow::on_click), "c"));

On a side note:

If you have problem with pointers you could try smart pointers but to be honest I think it would be better for you to understand them.

Community
  • 1
  • 1
pan-mroku
  • 803
  • 6
  • 17