Re: wy signal handler could not be override



Am 29.03.2013 14:31, schrieb Klaus Rudolph:
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;
}
One thing in general: You don't have to cast your Rect-pointer to an Item-pointer.
In C++, declaring the method as "virtual" is needed if you want to
override anything by polymorphism.
It would be better to declare the override-method as virtual, but I don't think it is necessary. But try that too.

What I think is the most probable answer to the question why your method is not called, is that the default signal handler is empty and therefore it was not considered necessary to connect it to the signal

And how to emit the signal manually, well... I don't know :D
Goocanvasmm and Gtkmm as well seem to use some sort of abstraction layer for the signals (SignalProxy), so that emitting them manually seems impossible. If you had a sigc::signal, then you would just call its emit()-function with the arguments to be sent to the handlers (in this case, a Glib::refPtr<Item> and a GdkEventButton*).

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]