1
我努力使用下面的代碼。我的信號處理程序on_button_press_event()永遠不會被調用,但我不知道爲什麼。有人可以看看嗎?也許有人能夠通過調試信息運行gtkmm庫。我只有預先安裝的gtkmm軟件包,不能用於調試庫本身。覆蓋gtkmm中的信號處理程序
#include <iostream>
using namespace std;
#include <gtkmm.h>
#include <goocanvasmm.h>
bool MyExternalHandler(const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev)
{
cout << "External Handler" << endl;
return false;
}
class MyRect : public Goocanvas::Rect
{
public:
MyRect(double x, double y, double w, double h)
//: Goocanvas::Rect(x,y,w,h)
{
property_x()=x;
property_y()=y;
property_width()=w;
property_height()=h;
}
public:
virtual void nonsens() {}
bool on_button_press_event(const Glib::RefPtr<Item>& target, GdkEventButton* event) override
{
cout << "override handler" << endl;
return false;
}
bool Handler(const Glib::RefPtr<Goocanvas::Item>& item, GdkEventButton* ev)
{
cout << "via mem_fun" << endl;
return false;
}
bool on_enter_notify_event(const Glib::RefPtr<Item>& target, GdkEventCrossing* event) override
{
cout << "override enter notify" << endl;
return false;
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(&argc, &argv);
Goocanvas::init("example", "0.1", argc, argv);
Gtk::Window win;
Goocanvas::Canvas m_canvas;
m_canvas.set_size_request(640, 480);
m_canvas.set_bounds(0, 0, 1000, 1000);
MyRect* ptr;
Glib::RefPtr<MyRect> m_rect_own(ptr=new MyRect(225, 225, 150, 150));
m_rect_own->property_line_width() = 1.0;
m_rect_own->property_stroke_color() = "black";
m_rect_own->property_fill_color_rgba() = 0x555555ff;
Glib::RefPtr<Goocanvas::Item> root = m_canvas.get_root_item();
root->add_child(m_rect_own);
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::ptr_fun(&MyExternalHandler));
((Glib::RefPtr<Goocanvas::Item>&)m_rect_own)->signal_button_press_event().connect(sigc::mem_fun(*ptr, &MyRect::Handler));
win.add(m_canvas);
win.show_all_children();
Gtk::Main::run(win);
return 0;
}
簡單的,如果我換到簽名'虛擬BOOL \t on_button_press_event沒有編譯(GdkEventButton * button_event )'因爲覆蓋失敗。 – Klaus
對不起,對,您的簽名對於GooCanvas :: item是正確的。這確實不是一個Gtk :: Widget。 – murrayc