Re: gobject construction with parameter



Hey,

On Mon, 31 Jan 2011 13:14:30 +0000
James Morris <jwm art net gmail com> wrote:

Hi,

I'm trying to modify some existing code which uses gobject so that the
function to create a new object can take a parameter. The existing
code is quite simple, and my modifications simplify it further so I
don't want to spend a great deal of time messing about with Gobject
things.

Can anyone point me in the direction of code examples where the
construction of the g object requires a parameter so I can see how to
do it please? - I can't make sense of the documentation.

I usually do this by adding a construct or construct-only property to
the object class and creating a my_object_new() function that takes a
parameter for this property that I can then pass to g_object_new().

Here's a simple example from Xfce's XDG menu library garcon: GarconMenu
is the object and it needs a menu file (a GFile object) to be
constructed properly. So what we did is to to install a construct-only
property like this:

  http://git.xfce.org/libs/garcon/tree/garcon/garcon-menu.c#n246

and write a _new() function that takes a GFile for this property and
passes it to g_object_new():

  http://git.xfce.org/libs/garcon/tree/garcon/garcon-menu.c#n459

This property will not be available in the init function (e.g.
garcon_menu_init) but if you want to do something with it before the
object can be used by the outside world, you can do that by overriding
the GObject constructed function like you do with finalize or
get_property. In constructed, the construct and construct-only
properties will be set and you can do something with them.

  - Jannis



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