I am very new to C++ gtkmm (Linux) programming. I developing a program where I need a button to be clicked in the callback function of another button on the gui. I have tried button.activate() But it only animates the button click but the callback function is not called. When I click the button manually, the callback function is called. Please explain how to inject event into the gtkmm C++ coding. Events may include button press, key press etc.
Asked
Active
Viewed 603 times
1 Answers
1
Here is an example that works with Gtkmm 3.24 for a button click:
#include <iostream>
#include <gtkmm.h>
class MainWindow : public Gtk::ApplicationWindow
{
public:
MainWindow();
private:
Gtk::Grid m_layout;
Gtk::Label m_label;
Gtk::Button m_buttonA;
Gtk::Button m_buttonB;
};
MainWindow::MainWindow()
: m_buttonA{"A"}
, m_buttonB{"B"}
{
m_label.set_text("Click a button...");
m_buttonA.signal_clicked().connect(
[this](){
std::cout << "Button A clicked!" << std::endl;
// Emits "clicked" on button B, just like when
// a user clicks it:
m_buttonB.clicked();
m_buttonB.activate_action("clicked");
}
);
m_buttonB.signal_clicked().connect(
[this](){
std::cout << "Button B clicked!" << std::endl;
}
);
m_layout.attach(m_buttonA, 0, 0, 1, 1);
m_layout.attach(m_buttonB, 1, 0, 1, 1);
add(m_layout);
}
int main(int argc, char *argv[])
{
std::cout << "Gtkmm version : " << gtk_get_major_version() << "."
<< gtk_get_minor_version() << "."
<< gtk_get_micro_version() << std::endl;
auto app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");
MainWindow window;
window.show_all();
return app->run(window);
}
With Gtkmm 4 however, the clicked()
method seems to have been removed from Gtk::Button
's interface. By looking at the new interface, there is a activate_action
method (inherited from Gtk::Widget
) that, maybe, could work. However, I don't have Gtkmm 4 here, so I could not try it.

BobMorane
- 3,870
- 3
- 20
- 42
-
error: ‘class Gtk::Button’ has no member named ‘activate_action’ 39 | button->activate_action("clicked"); | ^~~~~~~~~~~~~~~ please explain what to do. – Ritu Lahkar Nov 09 '21 at 14:52
-
What is your Gtkmm version? Build and run this to find out: `int main(int argc, char *argv[]) { std::cout << "Gtkmm version : " << gtk_get_major_version() << "." << gtk_get_minor_version() << "." << gtk_get_micro_version() << std::endl; return 0; }` I suspect you have Gtkmm 3 (and not 4). – BobMorane Nov 09 '21 at 15:07
-
1Gtkmm version : 3.24.30 – Ritu Lahkar Nov 09 '21 at 15:53
-
Ok, same as mine. In this version, `activate_action` is not yet available (you need Gtkmm 4). You can use the approach in the code snippet I posted in my answer instead. – BobMorane Nov 09 '21 at 16:06
-
But button.clicked() is not calling the callback. Please help. – Ritu Lahkar Nov 09 '21 at 16:39
-
Did you try the code I posted (exactly)? If not, you will have to post the code you are tying to run in your question. In the case of the code I posted, the callback is called (see output in console). – BobMorane Nov 09 '21 at 16:44
-
Yeah, I was using signal_button_press_event() not signal_pressed(). Now it's working. but how are these two functions different? – Ritu Lahkar Nov 09 '21 at 19:02
-
I don't know, I always use `signal_clicked`. – BobMorane Nov 09 '21 at 19:09