GObject property validation during object construction

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

GObject property validation during object construction

Link Dupont
I have a GObject.

struct _MyObject
{
  gchar *foo;
};

I set a string property in my class_init with g_param_spec_string. I
set the property->cset_nth to a string of valid characters
("abc...xyz").

static void
my_object_class_init (MyObjectClass *klass)
{
  GParamSpecString *pspec = g_param_spec_string ("foo", "foo", "foo",
                                                 NULL,
                                                 G_PARAM_READWRITE | 
                                                 G_PARAM_CONSTRUCT);
  pspec->cset_nth = g_strdup ("abcdefghijklmnopqrstuvwxyz-_");

  g_object_class_install_property (object_class, PROP_FOO, pspec);
}

When I create an instance of my object with g_object_new:

MyObject *obj = g_object_new (MY_TYPE_OBJECT,
                              "foo", "invalid string",
                              NULL);

I notice that g_param_value_validate *is* being called, and does return
FALSE, but it's not setting an error; it appears to g_logs a warning
and eventually aborts. Is there any way to make that validation check
non-fatal, so I can handle the error nicely?

Also, this behavior seems contradictory to the GObject documentation on
properties: "the GValue is validated by g_param_value_validate [...] As
such, the g_object_set_property function will return with an error."
g_object_set_property returns void and does not accept a GError.
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list

signature.asc (484 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: GObject property validation during object construction

Gtk+ - General mailing list

Hi Link,

My understanding is that the private string gets setup in my_object_class_init(). Then in my_object_init() you can initialize it to a default value. After that, you can set a new string value using a property set accessor function that can validate the string and/or you can use CSS for a string value.

Worked on a couple of gauge widgets which the setup might be helpful. The adjustable_gauge_main() test programs validate some color strings from a GtkEntry. If the string isn't validated a simple warning is issued but the current or default value isn't changed.

https://github.com/cecashon/OrderedSetVelociRaptor/tree/master/Misc

They are in the adjustable gauge folders with the test programs for them. Also there is a Penrose folder with an object that uses CSS. That came from the GTK+ C++ or gtkmm tutorial which has a lot of great info.

https://developer.gnome.org/gtkmm-tutorial/stable/sec-custom-widgets.html.en

Eric





_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list