Gtk4 Api, get_first_child

I was playing with Gtk4 tutorial at Gtk Gnome Develper Documentation trying to rewrite it in Vala, I’m aware that Gtk4 API not final and undergoes constant changes.

I’ve been faced with 2 problems I can’t solve and I could use some help.

  1. Closing the Window don’t exit the process, I’ve to either use the Quit menu or press Ctrl+C in Terminal.
  2. I can’t remove the Widgets (Buttons) from GtkListBox because get_first_child seems not to work. Please see the relevant lines here.

Thank you very much.


And how are you “closing” it then?

I can’t fix that code without knowing what it’s supposed to do. Remove all rows from a listbox?

  1. I mean pressing x on the ApplicationWindow hides it but the process still runs, either I have to open system monitor and kill the application, or use the app.quit action.
  2. The code supposed to remove all rows from Listbox and then populate new ones. I can’t remove those rows (I’ve added buttons as rows).

Do the C examples work fine for you?

I did intent to create a Nim version of it, but for me the C code does not work really well currently. Examples 1 to 3 seems to be ok, but for example 4 the makefile does not work, I get

~/gtk/examples/application4 $ make -f Makefile.example
make: *** No rule to make target ‘app-menu.ui’, needed by ‘resources.c’. Stop.

For application9 makefile works, but I get a lot of warnings like

(exampleapp:27002): GLib-GObject-WARNING **: 18:26:19.695: invalid cast from ‘GtkListBox’ to ‘GtkBox’

I had more luck with the files from

At least the headerbar example works for me:

But it is already outdated again, Matthias Clasen has replaced it lately with headerbar2.

  1. Are you maybe keeping an extra reference to the toplevel window around that you are not releasing?

  2. In the linked code, you are only ever removing the same child.

while (listbox.get_first_child() != null) {

I haven’t tried to compile the c code, which I tried to replicate as much as possible. Code Here.

As a side note, I like your work with Nim and gintro which I’ve also tried, but I don’t like the fact that Nim lacks OOP style.

the code looks like this
/* Brocken // TODO: What is the alternative?! */

while (words.get_first_child () != null) { words.remove (child); }

A possible replacement:

let child = words.get_first_child();
while (child != null) {
    let next = child.get_next_sibling();
    child = next;


And where are you ever assigning a new value to child?

Nim lacks OOP style.

Nim does not really lack OOP style, but it does not enforce it like old style languages like Java do. Nim has methods and inheritance, but no classes. I think other more modern languages like Go and Rust do not enforce OOP that much too, and GTK4 also consider composition over inheritance and subclassing now.

Thanks. That is the solution.
aside, Vala doesn’t have let keyword, instead var.

I got the C code working:

112 gtk_box_append (GTK_BOX (win->words), row);

seems to be legacy, I replaced it with

gtk_list_box_insert (GTK_LIST_BOX (win->words), row, -1);

And it works!

I assume you have solved that issue, can you tell us what the initial problem was? (I have something similar with the Nim version of latest GTK4 tests/simple.c currently, have still to investigate it.)

Yes I have, It turned out that I have a reference to the main window in the application class as a member variable. I’ve deleted that and updated the repository.

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