将Glib :: RefPtr <t>转换或转换为Gtk :: Widget&amp;需要</T>

时间:2012-09-27 13:36:31

标签: c++ g++ gtkmm glibmm

我很难将Glib :: RefPtr转换为GtkWidget,其中T来自Widget:

#include <gtkmm/drawingarea.h>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/fixed.h>

class MyPic : public Gtk::DrawingArea {
public:
};

int main(int argc, char* argv[]) {
    Gtk::Fixed f;

    Gtk::DrawingArea da; // this works.
    Gtk::DrawingArea mp; // this works.
    Glib::RefPtr<MyPic> rp_mp; // this not.

    f.put(da, 10, 20);
    f.put(mp, 10, 30);
    f.put(rp_mp, 10, 40); // Line # 19
}   

这不编译:

joerg> g++ x.cpp `pkg-config --cflags --libs gtkmm-3.0`
x.cpp: In function ‘int main(int, char**)’:
x.cpp:19:24: error: no matching function for call to ‘Gtk::Fixed::put(Glib::RefPtr<MyPic>&, int, int)’
x.cpp:19:24: note: candidate is:
/usr/include/gtkmm-3.0/gtkmm/fixed.h:123:8: note: void Gtk::Fixed::put(Gtk::Widget&, int, int)
/usr/include/gtkmm-3.0/gtkmm/fixed.h:123:8: note:   no known conversion for argument 1 from ‘Glib::RefPtr<MyPic>’ to ‘Gtk::Widget&’

joerg> g++ --version

g ++(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3 版权所有(C)2011 Free Software Foundation,Inc。 这是免费软件;查看复制条件的来源。没有 保证;甚至不适用于适销性或特定用途的适用性。

Glib :: RefPtr是一个智能指针,DrawingArea是从Widget派生的,所以这应该有效。

解除引用(如f.put(*rp_mp,...))故意无法正常工作。文档说明:"*Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.*"

如何从 SmartPtr 获得小部件和

1 个答案:

答案 0 :(得分:1)

虽然我怀疑你可以根据需要进行设计,但你可以取消引用,例如Glib::RefPtr foo如下:

some_method_needing_a_reference(*foo.operator->())