Re: wy signal handler could not be override
- From: "Klaus Rudolph" <lts-rudolph gmx de>
- To: "Yann Leydier" <yann leydier info>
- Cc: gtkmm-list gnome org
- Subject: Re: wy signal handler could not be override
- Date: Fri, 29 Mar 2013 14:31:43 +0100 (CET)
To override the signal handler is part of the documentation. Looking in the sources shows the virtual
functions:
virtual bool on_<Signal_Name>(...);
But it seems simply not to work as expected and I have no idea why.
I would provide a full example:
#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;
}
In C++, declaring the method as "virtual" is needed if you want to
override anything by polymorphism.
However, in Gtkmm, I never override methods. Signals do the same job and
are more flexible, so I connect methods to the signals I want to handle
in the constructor:
signal_button_press_event().connect(sigc::mem_fun(this,
&MyClass::button_pressed));
Yes, the slot mechanic works, but I simply want to test what is written in the docs. And I dive into the
source and also find the on_XXX(...) virtual function call. But there is something wrong with them or my
usage.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]