G_object_set_property explication

I have this code.

file 1:

/* finance-list-box.c
 *
 * Copyright 2020 Danilo Fernandes Galete <galetedanilo@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "finance-list-box.h"

struct _FinanceListBox
{
  GtkListBox  parent_instance;
  
  /* The Properties */
  gpointer    pane;
  
};

G_DEFINE_TYPE (FinanceListBox, finance_list_box, GTK_TYPE_LIST_BOX)

enum {
  PROP_0,
  PROP_PANE,
  N_PROPS
};

static GParamSpec *properties [N_PROPS] = { NULL, };

FinanceListBox *
finance_list_box_new ()
{
  return g_object_new (FINANCE_TYPE_LIST_BOX, NULL);
}

static void
finance_list_box_finalize (GObject *object)
{
  FinanceListBox *self = (FinanceListBox *)object;

  G_OBJECT_CLASS (finance_list_box_parent_class)->finalize (object);
}

static void
finance_list_box_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  FinanceListBox *self = FINANCE_LIST_BOX (object);

  switch (prop_id)
    {
    case PROP_PANE:
      g_value_set_pointer (value, self->pane);
      break;
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
finance_list_box_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  FinanceListBox *self = FINANCE_LIST_BOX (object);

  switch (prop_id)
    {
    case PROP_PANE:
      g_assert (self->pane == NULL);
      self->pane = g_value_get_pointer (value);
      break;
      
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
finance_list_box_class_init (FinanceListBoxClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = finance_list_box_finalize;
  object_class->get_property = finance_list_box_get_property;
  object_class->set_property = finance_list_box_set_property;
  
  properties[PROP_PANE] = g_param_spec_pointer ("pane",
                                                "the gpointer to pane",
                                                "A gpointer to pane",
                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  
  g_object_class_install_property (object_class, N_PROPS, properties);
}

static void
finance_list_box_init (FinanceListBox *self)
{

}

file 2:

/* Create the GtkListBox with the categories */
static void
finance_category_pane_create_rows (FinanceCategoryPane *self)
{
  if (self->list_box != NULL)
    gtk_widget_destroy (GTK_WIDGET (self->list_box));
  
  self->list_box = finance_list_box_new ();
  
  g_object_set_property (G_OBJECT (self->list_box), "pane", self);
    
  gtk_container_add (GTK_CONTAINER (self->viewport), GTK_WIDGET (self->list_box));
  
  gtk_widget_show_all (GTK_WIDGET (self->viewport));
  
}

This error:
(finance:2): GLib-GObject-CRITICAL **: 20:50:29.745: validate_pspec_to_install: assertion ‘G_IS_PARAM_SPEC (pspec)’ failed

(finance:2): GLib-GObject-WARNING **: 20:50:29.749: g_object_set_is_valid_property: object class ‘FinanceListBox’ has no property named ‘pane’

You are calling g_object_class_install_property() instead of g_object_class_install_properties():

-  g_object_class_install_property (object_class, N_PROPS, properties);
+  g_object_class_install_properties (object_class, N_PROPS, properties);

Alternatively this would work as well:

-  g_object_class_install_property (object_class, N_PROPS, properties);
+  g_object_class_install_property (object_class, PROP_PANE, properties[PROP_PANE);
1 Like

Thank you so much I didn’t even notice the error

Make sure you’re enabling compiler warnings; calling g_object_class_install_property() with a GParamSpec** as the last argument should have generated a warning at compile time.

I’m using Gnome Builder so I didn’t notice the error, it gives a lot of warnings, mainly in files that use G_DEFINE_TYPE_WITH_CODE in iface-> some function = another function, is this normal?

Without seeing the errors I can’t tell what’s “normal”. In general, no: warnings are not normal, and you should read what they say, and fix them.

If you’re assigning a function pointer to an interface virtual function and you’re getting a warning in means you’re not using the appropriate function prototype; for instance, you’re using the wrong type for the arguments, or the wrong number of arguments.

1 Like

For example, I have a button clicked signal, but in the CALLBACK function I don’t use the button, just gpointer. This generates a “unusede parameter button” warning, should these types of warnings be handled as well?

Yes. You can annotate the unused arguments with G_GNUC_UNUSED.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.