c++g++gtkmmglibmm

Conversion or cast of Glib::RefPtr<T> to Gtk::Widget& needed


I have trouble to convert a Glib::RefPtr into a GtkWidget, with T derived from 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
}   

This does not compile:

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 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Glib::RefPtr is a smart pointer, and DrawingArea is derived from Widget, so this should work.

Dereferencing (like f.put(*rp_mp,...) ) is intentionally not working. The documentation states: "*Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.*"

How do I get a Widget& from the SmartPtr?


Solution

  • Although I suspect you can design around the need, you can dereference e.g. Glib::RefPtr foo as follows:

    some_method_needing_a_reference(*foo.operator->())