开发者

Implementing a GObject interface in C++

开发者 https://www.devze.com 2023-02-10 01:08 出处:网络
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 wra

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::S开发者_Go百科ourceCompletionProvider, 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


Finally, I found a solution.

Class definition (order of subclasses matters):

class MonaCompletionProvider : public Glib::Object, public gtksourceview::SourceCompletionProvider {
...

Constructor (again, order matters):

MonaCompletionProvider::MonaCompletionProvider() :
    Glib::ObjectBase(typeid(MonaCompletionProvider)),
    Glib::Object(),
    gtksourceview::SourceCompletionProvider() {
...

Solution found by inspecting how it has been done in Guikachu.

0

精彩评论

暂无评论...
验证码 换一张
取 消