在 C++ 中实现 GObject 接口
我尝试使用 Glibmm(Gtkmm 的一部分)在 C++ 中实现 GType 接口。该对象将被传递到 C 语言的 API。不幸的是,gtkmm 的 文档 没有涵盖了它如何包装 GObject 系统的许多细节。
到目前为止我所拥有的:
class MonaCompletionProvider : public gtksourceview::SourceCompletionProvider, public Glib::Object
{
public:
MonaCompletionProvider();
virtual ~MonaCompletionProvider();
Glib::ustring get_name_vfunc() const;
// ... and some more
}
所有方法和构造函数实现都是空的。代码的使用方式如下:
Glib::RefPtr<MonaCompletionProvider> provider(new MonaCompletionProvider());
bool success = completion->add_provider(provider);
执行此代码后,success
将为 false,并且命令行中出现以下消息:
(莫纳吉:24831): GtkSourceView-关键 **: gtk_source_completion_add_provider: 断言 `GTK_IS_SOURCE_COMPLETION_PROVIDER (提供商)失败
似乎底层的 gobj()
不知道它应该实现这个接口。如果该类不是从 Glib::Object 派生的,则 gobj() 甚至会返回 null。我希望我不必用 C 手动编写实现此接口的 GObject。
那么正确的方法是什么?提前致谢。
PS:对于那些感兴趣的人: SourceCompletionProvider
I try to implement a GType interface in C++ using Glibmm (part of Gtkmm). The object will be passed to an API in C. Unfortunately, the documentation for gtkmm does not cover many details of how it wraps the GObject system.
What I have so far:
class MonaCompletionProvider : public gtksourceview::SourceCompletionProvider, public Glib::Object
{
public:
MonaCompletionProvider();
virtual ~MonaCompletionProvider();
Glib::ustring get_name_vfunc() const;
// ... and some more
}
All method and constructor implementations are empty. The code is used like this:
Glib::RefPtr<MonaCompletionProvider> provider(new MonaCompletionProvider());
bool success = completion->add_provider(provider);
success
will be false after executing this code and the following message appears in the command line:
(monagui:24831):
GtkSourceView-CRITICAL **:
gtk_source_completion_add_provider:
assertion
`GTK_IS_SOURCE_COMPLETION_PROVIDER
(provider)' failed
It seems that the underlying gobj()
is not aware that it is supposed to implement this interface. If the class does not derive from Glib::Object
, gobj()
even returns null. I hope that I do not have to write a GObject implementing this interface in C manually.
So what is the correct way to do this? Thanks in advance.
PS: For those who are interested: SourceCompletionProvider
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我找到了解决方案。
类定义(子类的顺序很重要):
构造函数(同样,顺序很重要):
通过检查 Guikachu。
Finally, I found a solution.
Class definition (order of subclasses matters):
Constructor (again, order matters):
Solution found by inspecting how it has been done in Guikachu.