0

I'm beginning to learn GTK3 and have hit a roadblock. I declared an array of buttons Gtk::Button buttons[15], and linked them all to the same onClicked() function. Each button contains a unique image. When the user clicks a button, I want to retrieve the file name of the image so I know which button was clicked, the images change periodically, so I haven't given them labels.

// creating the button array 
for(int i = 0; i < 15; i++){
  Gtk::Image image("nothing.png");
  button[i].signal_clicked().connect(sigc::mem_fun(*this,
    &Window::doOnButtonClicked));
  button[i].set_image(image);
}


//doOnButtonClicked
void Window::doOnButtonClicked(Gtk::Button button){
   onClicked(&button);
}


//the onClicked() code
void onClicked(Gtk::Button * button){
  GValue value = G_VALUE_INIT;
  g_value_init (&value, G_TYPE_STRING);
  g_object_get_property(G_OBJECT(button->get_image()), "file", &value);
  string source = g_value_get_string(&value);
  cout<<source;
 }

I did try to follow the steps to get the file name of the image as listed here, but I realised that I needed to pass my button to extract the Image object, so, I followed the steps in this to pass in the button object to the function.

But right now I have the following errors :

error: return-statement with a value, in function returning 'void' [-fpermissive] { return functor_(); }

no match for call to ‘(sigc::bound_mem_functor1) ()’ { return functor_(); }

Thanks for all the help in advance

user7999116
  • 199
  • 1
  • 12
  • Does GTK not allow the identification of controls by an ID? It sounds like the wrong way to do it. – enhzflep Jul 10 '18 at 05:14
  • I'm really new to Gtk, so I don't know what do you mean by that. I can't do button[5], because I don't know which button is being clicked by the user. – user7999116 Jul 10 '18 at 11:12
  • I've resorted to using sigc::bind(sigc::mem_fun(*this, &Window::doOnButtonClicked)), button[i]); I did realise that I couldn't do this as Gtk::Button, since the copy ctor is deleted. – user7999116 Jul 10 '18 at 19:17
  • I would really appreciate it if someone could let me know if the *this in the sigc::mem_fun(*this, &Window::doOnButtonClicked)); refers to the button that was clicked, and if yes, how do I make use of it. – user7999116 Jul 10 '18 at 19:20
  • Tried playing with GTK years ago and gave it away in favour of WxWidgets as a cross-platform library. In both windows and wx, clicking a specific button causes a particular handler to be called. Hence, you know which control was clicked by whuch callback is invoked. It sounds off, that you'd be told a button was clicked, yet not whuch one... WxWidgets allowwd for the creation of a program that need only be recompiled to run under x64 windows, x64 linux, x32 windows and just for a laugh - a raspberry pi. Sorry i cant help you further. – enhzflep Jul 11 '18 at 13:38
  • Ahhhhhh! Just realized that i glossed over the "and linked them all to the same onClicked() function" part of your initial question.. *that* is the problem. Attach each button to its own handler, simples. :-) – enhzflep Jul 11 '18 at 13:40
  • `sigc::bind(sigc::mem_fun(*this, &Window::doOnButtonClicked)), button[i]);` is the way to go. You could also use lambda expression. `*this` is the object which doOnButtonClicked wil be called. Since it's a class member passing only doOnButtonClicked is not enough. – pan-mroku Jul 19 '18 at 15:12

0 Answers0