Gtk.ColumnView and .ui file possible?

I used to use Glade and Treeview, and my data for the TreeView was stored in an sqlite3 database. It was simple.

Create ListStore with a bunch of columns.
Create Treeview, set model as the ListStore
Create the TreeViewColumns, each with a renderer that is set to the appropriate ListStore column.

Then, in Python it was a simple matter of connecting to the sqlite3 database and…
record = fetchall()
for row in record… liststore.append().

ColumnView… I’ve spent 30-40hrs over the past 4 days and I haven’t achieved anything.

Please could someone at least just explain what I’m supposed to be doing as raw as I described Glade/TreeView?

There are two links that have helped a lot but they are written purely in Python (which is what I’m using) and don’t include a .ui file.

I want to do as much as possible in a .ui file so each time I refresh the ColumnView it can be as short and simple as how I did Glade/TreeView.
Using .ui, I’ve added a ScrolledWindow, ColumnView, SingleSelection, GListStore, ColumnViewColumns, and setup and bind signals. The ColumnView shows up along with the column names but I can’t fill the columns. I’m not trying to use sqlite at this stage, BTW!

All examples I’ve found are written purely in Python. Is this the only way and am I trying to do something that won’t be possible?

Thank you for any help.

Let us display the tutorial.db database from sqlite3 — DB-API 2.0 interface for SQLite databases — Python 3.12.0 documentation.

Here is movies.ui:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.6"/>
  <object class="GtkApplicationWindow" id="main_window">
    <child>
      <object class="GtkColumnView" id="view">
        <property name="model">
          <object class="GtkSingleSelection">
            <property name="model">
              <object class="GListStore" id="store"/>
            </property>
          </object>
        </property>
        <child>
          <!-- The title row. -->
          <object class="GtkColumnViewColumn">
            <property name="title">Title</property>
            <property name="factory">
              <object class="GtkBuilderListItemFactory">
                <property name="bytes"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="GtkListItem">
    <property name="child">
      <object class="GtkLabel">
        <property name="hexpand">TRUE</property>
        <property name="xalign">0.0</property>
        <binding name="label">
          <lookup name="title" type="ExampleRecord">
            <lookup name="item">GtkListItem</lookup>
          </lookup>
        </binding>
      </object>
    </property>
  </template>
</interface>
         ]]></property>
              </object>
            </property>
          </object>
        </child>
        <child>
          <!-- The year row. Same as the title row. -->
        </child>
      </object>
    </child>
  </object>
</interface>

And there is movies.py:

import gi, sqlite3
gi.require_version('Gtk', '4.0')
from gi.repository import Gio, GObject, Gtk

class ExampleRecord(GObject.Object):
    __gtype_name__ = "ExampleRecord"

    def __init__(self, title, year):
        super().__init__()
        self._title = title
        self._year = year

    @GObject.Property(type=str)
    def title(self):
        return self._title

    @GObject.Property(type=str)
    def year(self):
        return self._year

def fill_store(store):
    # The database comes from http://docs.python.org/3/library/sqlite3.html
    con = sqlite3.connect("tutorial.db")
    cur = con.cursor()
    for row in cur.execute("SELECT title, year FROM movie ORDER BY year"):
        store.append(ExampleRecord(title=row[0], year=row[1]))
    con.close()

    
class ExampleApp(Gtk.Application):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.connect('activate', self.on_activate)

    def on_activate(self, app):
        builder = Gtk.Builder()
        builder.add_from_file("movies.ui")
        store = builder.get_object("store")
        fill_store(store)
        self.win = builder.get_object("main_window")
        self.win.set_application(self)
        self.win.present()

    def hello(self, button):
        print("Hello")

app = ExampleApp(application_id="com.example.GtkApplication")
app.run([])

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.