get_toplevel to reach derived toplevel window's members



Hi,

Say a derived widget's member function has to reach the derived
toplevel window's member and call its member function.

#include <iostream>
#include <gtkmm/main.h>
#include <gtkmm/entry.h>
#include <gtkmm/textview.h>
#include <gtkmm/window.h>
#include <gtkmm/box.h>

struct mywindow;

struct myentry: Gtk::Entry
{
  void reach_textview();
};

struct mywindow: Gtk::Window
{
  Gtk::VBox vbox;
  myentry entry;
  Gtk::TextView textview;

  mywindow()
  {
    vbox.pack_start(textview);
    vbox.pack_start(entry);
    add(vbox);
    show_all();
  }
};

void myentry::reach_textview()
{
  mywindow* pwin= dynamic_cast<mywindow*>(get_toplevel());
  pwin-> textview.get_buffer()-> set_text("hello");
}

int main()
{
  mywindow win;
  win.entry.reach_textview();
}



But this causes GLib runtime messages and then a segfault
(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-CRITICAL **: g_once_init_leave: assertion
`initialization_value != 0' failed

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-GObject-CRITICAL **:
g_type_add_interface_static: assertion `G_TYPE_IS_INSTANTIATABLE
(instance_type)' failed

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

(process:18811): GLib-GObject-CRITICAL **:
/build/buildd/glib2.0-2.14.0/gobject/gtype.c:2242: initialization
assertion failed, use IA__g_type_init() prior to this function

Program received signal SIGSEGV, Segmentation fault.

And gdb says:

(gdb) bt
#0  0x00002b9b771e79e0 in strlen () from /lib/libc.so.6
#1  0x00002b9b7449138c in Glib::ustring::operator+= ()
   from /usr/lib/libglibmm-2.4.so.1
#2  0x00002b9b744873d9 in Glib::Class::register_derived_type ()
   from /usr/lib/libglibmm-2.4.so.1
#3  0x00002b9b732e4385 in Gtk::Window_Class::init ()
   from /usr/lib/libgtkmm-2.4.so.1
#4  0x00002b9b732e582c in Gtk::Window::Window ()
   from /usr/lib/libgtkmm-2.4.so.1
#5  0x0000000000406a7c in mywindow (this=0x7fff37cf9620) at gtkmain.cpp:22
#6  0x0000000000406490 in main () at gtkmain.cpp:38



I will try showing what I'm doing in a simpler scenario:
#include <iostream>

struct container;

struct widget
{
  container* parent;
  void f(){ std::cout<< "widget::f()"; }
  virtual ~widget(){}
};

struct mywidget: widget
{
  void reach_widget_two();
};

struct container: widget{};
struct bin: container{};
struct window: bin{};

struct mywindow: window
{
  mywidget one;
  widget two;
};

void mywidget::reach_widget_two()
{
  mywindow* pwin= dynamic_cast<mywindow*>(parent);
  pwin-> two.f();
}

int main()
{
  mywindow win;
  win.one.parent= dynamic_cast<container*>(&win);
  win.two.parent= dynamic_cast<container*>(&win);
  win.one.reach_widget_two();
}

output:
widget::f()


Thanks for your time


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