如何使GtkListStore存储对象属性在一行中? [英] How to make GtkListStore store object attribute in a row?
问题描述
我试图使用我发现的代码片段来保留ListStore非文本对象。这些是对象:
class Series(gobject.GObject,object):
def __init __(self,title) :
super(Series,self).__ init __()
self.title = title
gobject.type_register(系列)
$ b $ class SeriesListStore(gtk .ListStore):
def __init __(self):
super(SeriesListStore,self).__ init __(Series)
self._col_types = [Series]
$ b def get_n_columns (self):
return len(self._col_types)
$ b $ 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 full_conf ['featuring']:
series = Series(title)
liststore.append([系列])
def get_series_title(列,单元格,模型,iter):
cell.set_property('text',liststore.get_value(iter,column).title)
return
selected = builder.get_object( trvMain)
selected.set_model(liststore)
col = gtk.TreeViewColumn(_(系列标题))
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)
...
但它失败,错误:
GtkWarning:
gtk_tree_view_column_cell_layout_set_cell_data_func:
断言info!= NULL'失败
文本'
col.set_cell_data_func(cell,
get_series_title)
警告:无法根据
type $的值设置属性
类型gchararray' c $ c> data + TrayIcon + Series'
window.show_all()
警告:无法设置属性文本'
gcha rarray'从
的类型
类型`data + TrayIcon +系列'
gtk.main()gtk.main()
我应该做些什么才能使它工作?
倒数第二个错误块。
-
GtkWarning:gtk_tree_view_column_cell_layout_set_cell_data_func:assertion'info!= NULL'
在英语中,这意味着单元格渲染器不在列的单元格渲染器列表中。在调用
set_cell_data_func
之前,您需要先将单元格渲染器添加到列中。 警告:无法从`typedata + TrayIcon + Series'的值中设置`gchararray'类型的属性`text'
这是因为
add_attribute
行导致GTK +尝试将单元格文本设置为Series对象,这当然会失败。只要删除该行;单元格数据func已经负责设置单元格文本。
在代码中:
col = gtk.TreeViewColumn(_(Series title))
cell = gtk.CellRendererText()
col.pack_start(单元格)
col.set_cell_data_func(cell,get_series_title)
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: assertion
info != NULL' failed
text' of type
col.set_cell_data_func(cell, get_series_title) Warning: unable to set propertygchararray' from value of type
data+TrayIcon+Series'
window.show_all() Warning: unable to set propertytext' of type
gchararray' from value of type `data+TrayIcon+Series'
gtk.main()gtk.main()
What should I do to make it work?
Two mistakes in the second-to-last block.
GtkWarning: gtk_tree_view_column_cell_layout_set_cell_data_func: assertion `info != NULL'
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
.Warning: unable to set property `text' of type `gchararray' from value of `typedata+TrayIcon+Series'
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:
col = gtk.TreeViewColumn(_("Series title"))
cell = gtk.CellRendererText()
col.pack_start(cell)
col.set_cell_data_func(cell, get_series_title)
这篇关于如何使GtkListStore存储对象属性在一行中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!