How to make GtkListStore store object attribute in a row?

♀尐吖头ヾ 提交于 2019-12-01 08:15:51

问题


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
col.set_cell_data_func(cell, get_series_title) Warning: unable to set property
text' of type gchararray' from value of typedata+TrayIcon+Series'
window.show_all() Warning: unable to set property text' of typegchararray' from value of type `data+TrayIcon+Series'
gtk.main()gtk.main()

What should I do to make it work?


回答1:


Two mistakes in the second-to-last block.

  1. 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.

  2. 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)


来源:https://stackoverflow.com/questions/3488285/how-to-make-gtkliststore-store-object-attribute-in-a-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!