It’s a complicated nightmare. All I am trying to do is add rows from an sql database to a columnview.
I can get the column headings to show but only one element from the database row to show. At this point I’m just trying to get each column to show Gtk.Labels but ultimately I’d like at least one of the columns to show something different (EditableLabel. for example).
A rough outline of what I have…
<child>
<object class="GtkColumnView" id="columnview">
<property name="model">
<object class="GtkSingleSelection" id="selection">
<property name="model">
<object class="GListStore" id="liststore">
<property name="item-type">DataObject</property>
</object>
</property>
</object>
</property>
<property name="row-factory">
<object class="GtkSignalListItemFactory" id="factory"/>
</property>
<property name="vexpand">True</property>
</object>
</child>
class DataObject(GObject.Object):
__gtype_name__ = 'DataObject'
def __init__(self, rowid, name, phone_number):
super().__init__()
self.rowid = rowid
self.name = name
self.phone_number = phone_number
@GObject.Property(type=str)
def rowid(self):
return self.rowid
@GObject.Property(type=str)
def name(self):
return self.name
@GObject.Property(type=str)
def phone_number(self):
return self.phone_number
def factory_setup(factory, item):
label = Gtk.Label()
item.set_child(label)
self.factory.connect("setup", factory_setup)
def factory_bind(factory, item):
item.get_child().set_label(item.get_item().rowid)
self.factory.connect("bind", factory_bind)
headings = ["rowid", "name", "phone_number"]
for heading in headings:
column = Gtk.ColumnViewColumn.new(heading, self.factory)
self.columnview.append_column(column)
for row in database_pull:
self.liststore.append(DataObject( rowid=str(row[0]),
name = row[1],
phone_number = row[2]))
where
print(database_pull)
[(1, 'john', '0794'), (2, 'dave', '0788')]
This puts the rowid in all the cells on each row. (a full row of 1s and second row of 2s).
How do I get the name and phone_number in too ?? I’ve tried adding two more factory_setups and factory_binds for name and phone_number but unsurprisingly, it didn’t work.
As I said at the beginning, ultimately I don’t want to use Gtk.Label for all mu columns but that’s the next hurdle!
Thank you for any help
edit:
I should also add that this code throws up errors for each database row even though it seems to work…
Traceback (most recent call last):
File "...columnview.py", line 117, in factory_setup
item.set_child(label)
^^^^^^^^^^^^^^
AttributeError: 'ColumnViewRow' object has no attribute 'set_child'
Traceback (most recent call last):
File "...columnview.py", line 121, in factory_bind
item.get_child().set_label(item.get_item().rowid)
^^^^^^^^^^^^^^
AttributeError: 'ColumnViewRow' object has no attribute 'get_child'