c++ gtkmm信号函数必须返回“int”?

vyswwuz2  于 2023-04-01  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在写一个程序来显示Sierpinski三角形,为此,我首先为cairo创建了一个演示,因为我以前从未使用过cairo;它只会创建一行:

#include <glibmm.h>
#include <gtkmm.h>

Gtk::Window* W_Main;
Gtk::DrawingArea* D_Triangle;
Gtk::Button* B_Draw;
Gtk::Adjustment* A_Depth;

int on_D_Triangle_draw(const Cairo::RefPtr<Cairo::Context> cr) {
    cr->set_source_rgb(0.0, 0.0, 0.0);
    cr->set_line_width(4.0);
    cr->move_to(0, 0);
    cr->line_to(30, 30);
    cr->stroke();
    return 0;
}

int main(int argc, char** argv) {
    auto app = Gtk::Application::create(argc, argv, "org.CENSORED.sierpinski");
    
    auto builder = Gtk::Builder::create_from_file("sierpinski.glade");
    
    builder->get_widget("W_Main", W_Main);
    builder->get_widget("D_Triangle", D_Triangle);
    builder->get_widget("B_Draw", B_Draw);
    builder->get_widget("A_Depth", A_Depth);
    
    D_Triangle->signal_draw().connect(sigc::ptr_fun(on_D_Triangle_draw));
    
    app->run(*W_Main);
}

奇怪的是显然,“on_D_Triangle_draw”函数必须返回一个整数!我首先试图使它成为一个空函数,但随后编译器返回了一个完全无法理解的错误消息。
有人能给我解释一下这里是怎么回事吗?进一步说,0是要返回的正确值吗(从“最佳实践”的意义上说)?
编辑:以下是(非常详细的)错误消息,如所要求的:

In file included from /usr/include/sigc++-2.0/sigc++/signal_base.h:27,
                 from /usr/include/sigc++-2.0/sigc++/signal.h:8,
                 from /usr/include/sigc++-2.0/sigc++/sigc++.h:123,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:50,
                 from /usr/include/glibmm-2.4/glibmm.h:103,
                 from sierpinski.cpp:1:
/usr/include/sigc++-2.0/sigc++/functors/slot.h: In instantiation of ‘static T_return sigc::internal::slot_call1<T_functor, T_return, T_arg1>::call_it(sigc::internal::slot_rep*, sigc::type_trait_take_t<T_arg3>) [with T_functor = sigc::pointer_functor1<Cairo::RefPtr<Cairo::Context>, void>; T_return = bool; T_arg1 = const Cairo::RefPtr<Cairo::Context>&; sigc::type_trait_take_t<T_arg3> = const Cairo::RefPtr<Cairo::Context>&]’:
/usr/include/sigc++-2.0/sigc++/functors/slot.h:177:56:   required from ‘static void* (* sigc::internal::slot_call1<T_functor, T_return, T_arg1>::address())(void*) [with T_functor = sigc::pointer_functor1<Cairo::RefPtr<Cairo::Context>, void>; T_return = bool; T_arg1 = const Cairo::RefPtr<Cairo::Context>&; sigc::internal::hook = void* (*)(void*)]’
/usr/include/sigc++-2.0/sigc++/functors/slot.h:679:90:   required from ‘sigc::slot1<T_return, T_arg1>::slot1(const T_functor&) [with T_functor = sigc::pointer_functor1<Cairo::RefPtr<Cairo::Context>, void>; T_return = bool; T_arg1 = const Cairo::RefPtr<Cairo::Context>&]’
/usr/include/sigc++-2.0/sigc++/functors/slot.h:1843:26:   required from ‘sigc::slot<T_return, T_arg1, sigc::nil, sigc::nil, sigc::nil, sigc::nil, sigc::nil, sigc::nil>::slot(const T_functor&) [with T_functor = sigc::pointer_functor1<Cairo::RefPtr<Cairo::Context>, void>; T_return = bool; T_arg1 = const Cairo::RefPtr<Cairo::Context>&]’
sierpinski.cpp:40:38:   required from here
/usr/include/sigc++-2.0/sigc++/functors/slot.h:170:16: error: void value not ignored as it ought to be
  169 |       return (typed_rep->functor_).SIGC_WORKAROUND_OPERATOR_PARENTHESES<type_trait_take_t<T_arg1>>
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170 |                (a_1);
      |                ^~~~~
lndjwyie

lndjwyie1#

来自gtkm-3文档:

插槽原型:

bool on_my_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr)
这个信号应该返回一个值,可以是bool,也可以是可转换为bool的值。返回int是可以接受的,但void是不可以的。

退货

true阻止其他处理程序为事件调用。false进一步传播事件。
如果返回零(false),则可能会调用其他处理程序。如果返回非零(true),则不会调用其他处理程序。您可以将返回值读取为“job done”。

相关问题