gpt4 book ai didi

c++ - 需要将 Glib::RefPtr 转换或转换为 Gtk::Widget&

转载 作者:行者123 更新时间:2023-11-28 07:56:24 26 4
gpt4 key购买 nike

我无法将 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 获取Widget&

最佳答案

尽管我怀疑您可以围绕需求进行设计,但您可以取消引用,例如Glib::RefPtr foo 如下:

some_method_needing_a_reference(*foo.operator->())

关于c++ - 需要将 Glib::RefPtr<T> 转换或转换为 Gtk::Widget&,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12622695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com