英文:
Exposing GTK3 custom widget properties in glade
问题
I can add a custom widget to GTK, as shown here. Now I'd like to add properties to the custom widget and have them show up in glade. Is this possible?
我可以像这里所示添加自定义小部件到GTK。现在,我想为自定义小部件添加属性,并让它们显示在glade中。这是否可能?
I have added properties to the custom widget as shown here but the properties do not show up in glade.
如这里所示,我已经为自定义小部件添加了属性,但属性不会显示在glade中。
Update
It appears that the glade catalogue supports a <parameter-spec>
element that can be used to define properties to be exposed within glade as shown below.
看起来glade目录支持<parameter-spec>
元素,可以用来定义在glade中公开的属性,如下所示:
<glade-widget-class title="Awesome TextView" name="AwesomeTextView" generic-name="awesome_text_view">
<properties>
<property id="num_fidgets" name="Number of fidgets" default="4" save="False" custom-layout="True">
<parameter-spec>
<type>GParamInt</type>
<min>4</min>
</parameter-spec>
<tooltip>The number of fidgets</tooltip>
</property>
</properties>
</glade-widget-class>
I have defined one such property but it does not show in glade. Rather I get the error message:
我定义了一个这样的属性,但它在glade中不显示。相反,我收到了错误消息:
GLib-GObject-CRITICAL **: 16:07:45.433: g_object_set_is_valid_property: object class 'AwesomeTextView' has no property named 'num-fidgets'
Where the custom widget AwesomeTextView
is essentially defined as:
自定义小部件AwesomeTextView
基本上被定义为:
class AwesomeTextView (Gtk.TextView):
__gtype_name__ = 'AwesomeTextView'
num_fidgets = GObject.Property(type=int, default='4', nick='num_Fidgets')
The <parameter-spec>
s appear to be undocumented. Thankfully though there are examples of the supported <type>
s in the glade repo.
似乎没有文档记录<parameter-spec>
。不过,幸运的是,在glade的仓库中有支持的<type>
的示例。
Correction: <parameter-spec>
s are documented. This documentation can be supplemented with examples of the supported <type>
s in the glade repo.
更正:<parameter-spec>
已经有文档记录。可以通过在glade的仓库中提供支持的<type>
示例来补充这份文档。
Update2
Found the cause of the error. The default argument has the wrong type -- str
instead of int
. The property should be:
找到错误的原因。默认参数的类型错误 - 应为int
而不是str
。属性应该如下:
num_fidgets = GObject.Property(type=int, default=4, nick='num_Fidgets')
Glade no longer emits an error. But the property does not show up in the UI.
Glade不再报错。但是属性仍然不会显示在用户界面中。
英文:
I can add a custom widget to GTK, as shown here. Now I'd like to add properties to the custom widget and have them show up in glade. Is this possible?
I have added properties to the custom widget as shown here but the properties do not show up in glade.
Update
It appears that the glade catalogue supports a <parameter-spec>
element that can be used to define properties to be exposed within glade as shown below.
<glade-widget-class title="Awesome TextView" name="AwesomeTextView" generic-name="awesome_text_view">
<properties>
<property id="num_fidgets" name="Number of fidgets" default="4" save="False" custom-layout="True">
<parameter-spec>
<type>GParamInt</type>
<min>4</min>
</parameter-spec>
<tooltip>The number of fidgets</tooltip>
</property>
</properties>
</glade-widget-class>
I have defined one such property but it does not show in glade. Rather I get the error message:
GLib-GObject-CRITICAL **: 16:07:45.433: g_object_set_is_valid_property: object class 'AwesomeTextView' has no property named 'num-fidgets'
Where the custom widget AwesomeTextView
is essentially defined as:
class AwesomeTextView (Gtk.TextView):
__gtype_name__ = 'AwesomeTextView'
num_fidgets = GObject.Property(type=int, default='4', nick='num_Fidgets')
<s>The <parameter-spec>
s appear to be undocumented. Thankfully though there are examples of the the supported <type>
s in the glade repo.</s>
Correction: <parameter-spec>
s are documented. This documentation can be supplemented with examples of the supported <type>
s in the glade repo.
Update2
Found the cause of the error. The default argument has the wrong type -- str
instead of int
. The property should be:
num_fidgets = GObject.Property(type=int, default=4, nick='num_Fidgets')
Glade no longer emits an error. But the property does not show up in the UI.
答案1
得分: 0
以下是翻译好的部分:
过于复杂的解决方案(请查看简化解决方案的更新)
找到了最后一部分。不要将<property>
属性的custom-layout
设置为"True"
,除非你知道自己在做什么,也就是说,你知道如何正确布局该属性(至少我不知道,至少目前还不知道)。我建议,在下面的更新的目录中将custom-layout="False"
设置为:
<glade-catalog name="awesome_text_view" library="gladepython" domain="glade-3" depends="gtk+">
<init-function>glade_python_init</init-function>
<glade-widget-classes>
<glade-widget-class title="Awesome TextView" name="AwesomeTextView" generic-name="awesome_text_view">
<properties>
<property id="num_fidgets" name="Number of fidgets" default="4" save="True" custom-layout="False">
<parameter-spec>
<type>GParamInt</type>
<min>4</min>
</parameter-spec>
<tooltip>The number of fidgets</tooltip>
</property>
</properties>
</glade-widget-class>
</glade-widget-classes>
<glade-widget-group name="Python" title="Python">
<glade-widget-class-ref name="AwesomeTextView"/>
</glade-widget-group>
</glade-catalog>
结果:
为了完整起见,这是示例自定义小部件类:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GObject
from gi.repository import Gtk
class AwesomeTextView(Gtk.TextView):
__gtype_name__ = 'AwesomeTextView'
num_fidgets = GObject.Property(type=int, default=4, nick='num_fidgets')
def __init__(self):
Gtk.TextView.__init__(self)
更新
原来在属性声明中的nick
参数
num_fidgets = GObject.Property(type=int, default=4, nick='num_fidgets')
是属性出现在Glade用户界面中所必需的。元素<properties>
更适合更复杂的值,例如枚举。我将保留更复杂的答案,以防对任何人有用。
英文:
Overly complicated solution (See update for simpler solution)
Found the final piece. Do not set the <property>
attribute custom-layout
to "True"
-- unless you know what are doing i.e. you know how to layout the property, properly (I don't, at least not yet). My advise, set custom-layout="False"
in the updated catalogue shown below:
<glade-catalog name="awesome_text_view" library="gladepython" domain="glade-3" depends="gtk+">
<init-function>glade_python_init</init-function>
<glade-widget-classes>
<glade-widget-class title="Awesome TextView" name="AwesomeTextView" generic-name="awesome_text_view">
<properties>
<property id="num_fidgets" name="Number of fidgets" default="4" save="True" custom-layout="False">
<parameter-spec>
<type>GParamInt</type>
<min>4</min>
</parameter-spec>
<tooltip>The number of fidgets</tooltip>
</property>
</properties>
</glade-widget-class>
</glade-widget-classes>
<glade-widget-group name="Python" title="Python">
<glade-widget-class-ref name="AwesomeTextView"/>
</glade-widget-group>
</glade-catalog>
Result:
For completeness here is the sample custom widget class
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import GObject
from gi.repository import Gtk
class AwesomeTextView(Gtk.TextView):
__gtype_name__ = 'AwesomeTextView'
num_fidgets = GObject.Property(type=int, default=4, nick='num_fidgets')
def __init__(self):
Gtk.TextView.__init__(self)
Update
Turns out the nick
argument in the property declaration
num_fidgets = GObject.Property(type=int, default=4, nick='num_fidgets')
is all that's required to for the property to show up in the glade UI. The element <properties>
would be more appropriate for more complicated values e.g. enums. I'm leaving the more complicated answer in place in the event that its of use to anyone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论