如何让GtkListStore连续存储对象属性?
我试图使用我找到的代码片段保留 ListStore 非文本对象。这些是对象:
class Series(gobject.GObject, object):
def __init__(self, title):
super(Series, self).__init__()
self.title = title
gobject.type_register(Series)
class SeriesListStore(gtk.ListStore):
def __init__(self):
super(SeriesListStore, self).__init__(Series)
self._col_types = [Series]
def get_n_columns(self):
return len(self._col_types)
def get_column_type(self, index):
return self._col_types[index]
def get_value(self, iter, column):
obj = gtk.ListStore.get_value(self, iter, 0)
return obj
现在我试图让 TreeView 显示它:
...
liststore = SeriesListStore()
liststore.clear()
for title in full_conf['featuring']:
series = Series(title)
liststore.append([series])
def get_series_title(column, cell, model, iter):
cell.set_property('text', liststore.get_value(iter, column).title)
return
selected = builder.get_object("trvMain")
selected.set_model(liststore)
col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.set_cell_data_func(cell, get_series_title)
col.pack_start(cell)
col.add_attribute(cell, "text", 0)
selected.append_column(col)
...
但它失败并出现错误:
Gtk警告: gtk_tree_view_column_cell_layout_set_cell_data_func: 断言
info != NULL' 失败
text' 类型为
col.set_cell_data_func(单元格, 获取系列标题) 警告:无法设置属性gchararray' 的值 类型
data+TrayIcon+Series'
window.show_all() 警告:无法设置属性text' 类型为
gchararray' 来自值 输入“数据+TrayIcon+系列”
gtk.main()gtk.main()
我应该怎么做才能使它工作?
I'w trying to keep at ListStore non-text objects using snippet I found. These are the objects:
class Series(gobject.GObject, object):
def __init__(self, title):
super(Series, self).__init__()
self.title = title
gobject.type_register(Series)
class SeriesListStore(gtk.ListStore):
def __init__(self):
super(SeriesListStore, self).__init__(Series)
self._col_types = [Series]
def get_n_columns(self):
return len(self._col_types)
def get_column_type(self, index):
return self._col_types[index]
def get_value(self, iter, column):
obj = gtk.ListStore.get_value(self, iter, 0)
return obj
And now I'm trying to make TreeView display it:
...
liststore = SeriesListStore()
liststore.clear()
for title in full_conf['featuring']:
series = Series(title)
liststore.append([series])
def get_series_title(column, cell, model, iter):
cell.set_property('text', liststore.get_value(iter, column).title)
return
selected = builder.get_object("trvMain")
selected.set_model(liststore)
col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.set_cell_data_func(cell, get_series_title)
col.pack_start(cell)
col.add_attribute(cell, "text", 0)
selected.append_column(col)
...
But it fails with errors:
GtkWarning:
gtk_tree_view_column_cell_layout_set_cell_data_func:
assertioninfo != NULL' failed
text'
col.set_cell_data_func(cell,
get_series_title)
Warning: unable to set property
of typegchararray' from value of
data+TrayIcon+Series'
type
window.show_all()
Warning: unable to set propertytext'
gchararray' from value of
of type
type `data+TrayIcon+Series'
gtk.main()gtk.main()
What should I do to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
倒数第二个区块有两个错误。
在英语中,这意味着单元格渲染器不在列的单元格渲染器列表中。在调用
set_cell_data_func
之前,您需要先将单元格渲染器添加到列中。这是因为
add_attribute
行导致 GTK+ 尝试将单元格文本设置为 Series 对象,这当然会失败。只需删除该行即可;单元格数据函数已经负责设置单元格文本。在代码中:
Two mistakes in the second-to-last block.
In English, this means that the cell renderer is not in the column's list of cell renderers. You need to add the cell renderer to the column first before calling
set_cell_data_func
.This is because the
add_attribute
line causes GTK+ to try setting the cell text to a Series object, which of course fails. Just remove that line; the cell data func already takes care of setting the cell text.In code: