在gtkmm中覆盖信号处理程序

时间:2013-03-29 14:11:49

标签: gtkmm

我挣扎着以下代码。我的信号处理程序on_button_press_event()从未被调用,但我不知道为什么。有人可以看看吗?也许有人能够通过调试信息运行gtkmm lib。我只有预先安装的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;
    }

1 个答案:

答案 0 :(得分:-1)

你的on_button_press_event()方法不是覆盖,因为它有错误的参数: https://developer.gnome.org/gtkmm/unstable/classGtk_1_1Widget.html#aba72b7f8655d1a0eb1273a26894584e3