button1_on_image = Gtk::manage(new Gtk::Image{"button1_on.png"}); // Load icon
button1_off_image = Gtk::manage(new Gtk::Image{"button1_off.png"}); // images
button1 = Gtk::manage(new Gtk::ToolButton{*button1_off_image}); // Create button
button1->set_tooltip_markup("Select one stick"); // with image
button1->signal_clicked().connect(sigc::mem_fun(*this,
&Main_window::on_button1_click));
toolbar->append(*button1);
这是一段代码片段,显示了我如何成功按下按钮。问题是,单击该按钮时,我希望显示的是“ button1_on.png”而不是“ button1_off.png”,但是我不知道该怎么做。
答案 0 :(得分:1)
以下是满足您需求的代码段:
请注意,这是一个最小的示例,因此再次单击按钮不会将其状态更改回“打开”,但是如果您需要的话,我将把这部分留给您。
#include <gtkmm.h>
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create(argc, argv, "buttons.on.off");
// Load images:
Gtk::Image button1_on_image{"button1_on.png"};
Gtk::Image button1_off_image{"button1_off.png"};
// Create button:
Gtk::ToolButton button1{button1_off_image};
button1.set_tooltip_markup("Select one stick");
// Create handler (as a lambda):
const auto handler = [&button1, &button1_on_image, &button1_off_image]()
{
// We change to "on" here (when clicked):
button1.set_icon_widget(button1_on_image);
// We make it visible:
button1.show_all();
};
button1.signal_clicked().connect(handler);
// Add the button to the window.
Gtk::Window window;
window.add(button1);
// Make the window visible:
window.show_all();
return app->run(window);
}
我对您的代码段做了一些简化:
new
)。我认为它使语法更清晰。