File: gobject.rst

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (94 lines) | stat: -rw-r--r-- 2,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
.. currentmodule:: gi.repository

==============
GObject.Object
==============

Compare to other types, :obj:`GObject.Object` has the best integration between
the GObject and Python type system.

1) It is possible to subclass a :obj:`GObject.Object`. Subclassing
   creates a new :obj:`GObject.GType` which is connected to the new Python
   type. This means you can use it with API which takes :obj:`GObject.GType`.
2) The Python wrapper instance for a :obj:`GObject.Object` is always the same.
   For the same C instance you will always get the same Python instance.


In addition :obj:`GObject.Object` has support for :any:`signals <signals>` and
:any:`properties <properties>`

.. toctree::
    :titlesonly:
    :maxdepth: 1
    :hidden:

    signals
    properties
    weakrefs


Examples
--------

Subclassing:

.. code:: pycon

    >>> from gi.repository import GObject
    >>> class A(GObject.Object):
    ...     pass
    ...
    >>> A()
    <__main__.A object at 0x7f9113fc3280 (__main__+A at 0x559d9861acc0)>
    >>> A.__gtype__
    <GType __main__+A (94135355573712)>
    >>> A.__gtype__.name
    '__main__+A'
    >>>

In case you want to specify the GType name we have to provide a
``__gtype_name__``:

.. code:: pycon

    >>> from gi.repository import GObject
    >>> class B(GObject.Object):
    ...     __gtype_name__ = "MyName"
    ...
    >>> B.__gtype__
    <GType MyName (94830143629776)>
    >>>

:obj:`GObject.Object` only supports single inheritance, this means you can
only subclass one :obj:`GObject.Object`, but multiple Python classes:

.. code:: pycon

    >>> from gi.repository import GObject
    >>> class MixinA(object):
    ...     pass
    ...
    >>> class MixinB(object):
    ...     pass
    ...
    >>> class MyClass(GObject.Object, MixinA, MixinB):
    ...     pass
    ...
    >>> instance = MyClass()


Here we can see how we create a :obj:`Gio.ListStore` for our new subclass and
that we get back the same Python instance we put into it:

.. code:: pycon

    >>> from gi.repository import GObject, Gio
    >>> class A(GObject.Object):
    ...     pass
    ...
    >>> store = Gio.ListStore.new(A)
    >>> instance = A()
    >>> store.append(instance)
    >>> store.get_item(0) is instance
    True
    >>>