Jhanarato
(Ajahn Jhanarato)
January 23, 2025, 9:21am
1
G’day!
Sorry if this is dead obvious:
In Python, how would I add a property to a GObject
to hold an array of strings?
For example:
class HasAquaticMammals(GObject.Object):
def __init__(self):
super().__init__()
self._list_of_mammals = ["Dolphin", "Whale", "Squid"]
@GObject.Property(type=???) # What type here?
def aquatic_mammals(self):
return self._list_of_mammals
Jhanarato
(Ajahn Jhanarato)
January 23, 2025, 9:32am
2
OK, guessed it!
@GObject.Property(type=GObject.ValueArray)
ebassi
(Emmanuele Bassi)
January 23, 2025, 10:03am
3
No, ValueArray
is a deprecated type.
You want GObject.Strv
, which is a vector of strings.
Jhanarato
(Ajahn Jhanarato)
January 23, 2025, 11:27am
4
Ahh! So like this in Python:
@GObject.Property(type=GObject.TYPE_STRV)
def previous_names(self) -> list[str]:
return self._previous_names
Incidentally, I have a suite of tests, one of which failed after changing the type:
def test_previous_names_with_different_types(self):
species = SpeciesModel()
species.previous_names = ["abc", 123]
Glad it failed!
ebassi
(Emmanuele Bassi)
January 23, 2025, 12:30pm
5
Yes; GValueArray
is an array of values wrapped in a GValue
, which can contain any type representable by the GObject type system. This means you can have an heterogeneous set of values of any type, as opposed to a homogeneous set of values with the same type.