有没有办法在不检查版本号的情况下检查 GTK 小部件是否支持 属性?
Is there a way to check if a GTK widget supports a property without checking version numbers?
根据 GTK API 参考,GtkAboutDialog 的 "license-type" 属性 仅存在于 GTK >= 3.0 中。为了兼容性,我的代码目前在设置 "license-type" 属性:
之前检查 GTK 版本
-- This is Lua code binding to GTK via lgi
local dialog = Gtk.AboutDialog {
title = "About Me",
-- ...,
}
if Gtk.check_version(3,0,0) == nil then
dialog.license_type = Gtk.License.MIT_X11
end
有没有办法直接询问 GTK 小部件是否支持某个 属性 而不是这样做?我认为如果我可以编写类似
的代码,代码将更加自我记录并且更不容易出错
if supports_property(dialog, "license-type") then
dialog.license_type = Gtk.License.MIT_X11
end
因为这个问题实际上是关于 GTK API,我可以用任何编程语言回答。虽然示例在 Lua 中,但我认为类似的问题应该出现在其他动态语言绑定中,甚至在 C 中,假设有一种方法可以按名称设置属性而无需通过 set_license_type
访问函数。
您可以使用 g_object_class_find_property()
函数查看 属性 是否存在。
请注意,此函数采用 GObjectClass,而不是 GObject 实例。所有 GObject classes 都出现在这些 class 实例对中,class 结构用于 vtable 方法等共享事物。要获取与对象实例关联的 GObjectClass,在 C 中,您可以使用 G_OBJECT_GET_CLASS()
宏。 (如果你想在 Lua 中这样做,并且如果 Lua 不能那样调用 C 宏,你将不得不跟踪 G_OBJECT_GET_CLASS()
的定义。)
在 lgi 中,class 的属性存在于其 _property 字段中:
if Gtk.AboutDialog._property.license_type then
dialog.license_type = Gtk.License.MIT_X11
end
您不需要像在 中那样使用 _property
字段,因为 lgi 会直接在类型类别表中查找所有名称。此外,还可以使用 _type
访问器获取实例的类型。所以我推荐以下解决方案:
if dialog._type.license_type then
dialog.license_type = Gtk.License.MIT_X11
end
如果出于某些原因你需要知道 属性 是否存在 而无需 实例化它,你可以通过这种方式使用@andlabs 的想法:
local lgi = require'lgi'
local Gtk = lgi.require'Gtk'
local class = Gtk.AboutDialogClass()
-- Prints yes
if class:find_property('license') then print('yes') end
-- Does not print anything
if class:find_property('unknown') then print('yes') end
根据 GTK API 参考,GtkAboutDialog 的 "license-type" 属性 仅存在于 GTK >= 3.0 中。为了兼容性,我的代码目前在设置 "license-type" 属性:
之前检查 GTK 版本-- This is Lua code binding to GTK via lgi
local dialog = Gtk.AboutDialog {
title = "About Me",
-- ...,
}
if Gtk.check_version(3,0,0) == nil then
dialog.license_type = Gtk.License.MIT_X11
end
有没有办法直接询问 GTK 小部件是否支持某个 属性 而不是这样做?我认为如果我可以编写类似
的代码,代码将更加自我记录并且更不容易出错if supports_property(dialog, "license-type") then
dialog.license_type = Gtk.License.MIT_X11
end
因为这个问题实际上是关于 GTK API,我可以用任何编程语言回答。虽然示例在 Lua 中,但我认为类似的问题应该出现在其他动态语言绑定中,甚至在 C 中,假设有一种方法可以按名称设置属性而无需通过 set_license_type
访问函数。
您可以使用 g_object_class_find_property()
函数查看 属性 是否存在。
请注意,此函数采用 GObjectClass,而不是 GObject 实例。所有 GObject classes 都出现在这些 class 实例对中,class 结构用于 vtable 方法等共享事物。要获取与对象实例关联的 GObjectClass,在 C 中,您可以使用 G_OBJECT_GET_CLASS()
宏。 (如果你想在 Lua 中这样做,并且如果 Lua 不能那样调用 C 宏,你将不得不跟踪 G_OBJECT_GET_CLASS()
的定义。)
在 lgi 中,class 的属性存在于其 _property 字段中:
if Gtk.AboutDialog._property.license_type then
dialog.license_type = Gtk.License.MIT_X11
end
您不需要像在 _property
字段,因为 lgi 会直接在类型类别表中查找所有名称。此外,还可以使用 _type
访问器获取实例的类型。所以我推荐以下解决方案:
if dialog._type.license_type then
dialog.license_type = Gtk.License.MIT_X11
end
如果出于某些原因你需要知道 属性 是否存在 而无需 实例化它,你可以通过这种方式使用@andlabs 的想法:
local lgi = require'lgi'
local Gtk = lgi.require'Gtk'
local class = Gtk.AboutDialogClass()
-- Prints yes
if class:find_property('license') then print('yes') end
-- Does not print anything
if class:find_property('unknown') then print('yes') end