Gtkmm:使用保存在std :: vector中的小部件的RefPtr [英] Gtkmm: Using RefPtr with widgets kept in std::vector

查看:295
本文介绍了Gtkmm:使用保存在std :: vector中的小部件的RefPtr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留一个 std :: vector Gtk :: Widget 并且可能会在 Gtk :: Container 之间移动。

I am trying to keep a std::vector of Gtk::Widgets that I am showing (and will potentially be) moving around between Gtk::Containers.

c $ c> Gtk :: Notebook 这基本上是一个到 std :: vector 的一对一映射,但如果我使用 Glib :: RefPtr 周围的小部件我从笔记本中删除小部件时遇到问题。我已经必须使用一个hack来获取指向底层Gtk对象的指针,当它添加到笔记本,我怀疑当从容器中删除对象时,Notebook容器释放/删除对象。

At the moment I keep a Gtk::Notebook which is basically a one-to-one map to the std::vector, but if I use Glib::RefPtr around the widgets I get problems when removing the widget from the notebook. I already have to use a 'hack' to get a pointer to the underlying Gtk object when adding it to the notebook and I suspect that the Notebook container frees/deletes the object when I remove it from the container.

我已经定义了我的向量的小部件,像这样:
std :: vector< Glib :: RefPtr< Gtk :: Widget>小部件;

I have defined my vector of widgets something like this: std::vector<Glib::RefPtr<Gtk::Widget>> widgets;

当我向向量和笔记本中添加一个小部件时:

When I add a widget to the vector and the notebook I do:

Glib::RefPtr<Gtk::Widget> w (new Gtk::Widget());
widgets.push_back (w);
Gtk::Widget *wptr = w.operator->(); // hack
notebook.append_page (*wptr);

当我尝试删除它:

int c = 1; // widget no. to remove
notebook.remove_page (c);
auto it = widgets.begin() + c;
widgets.erase (it);

但这会导致 G_IS_OBJECT when(I think) std :: vector 中的元素在迭代器的函数),因为可能 notebook.remove_page()已经释放了对象。我如何做到这一点?是否可能与 RefPtr 的?

but this results in a G_IS_OBJECT fail assertion when (I think) the element in the std::vector is cleaned up at the end of the iterator (end of function), since possibly notebook.remove_page() already freed the object. How can I do this? Is it possible with RefPtr's?

相关(同一断言失败):破坏Glib :: RefPtr会导致GTK 3内核中的失败断言


Related (same assertion failure): Destructing Glib::RefPtr causes failed assertions in the GTK 3 core

推荐答案

不幸的是,你不能这样做,因为 Gtk :: Notebook 子对象的所有权。你必须重构你的代码,使用 Gtk :: Notebook 本身访问小部件,而不是向量示例,其中包含 Gtk :: Notebook :: get_nth_page()

Unfortunately you can't do this because the Gtk::Notebook takes ownership of the child objects. You have to refactor your code to use the Gtk::Notebook itself to access the widgets instead of the vector, for example with Gtk::Notebook::get_nth_page().

这篇关于Gtkmm:使用保存在std :: vector中的小部件的RefPtr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆