Get string value from dropdown in gtk4

i have a dropdown with let am_pm_cam1 = gtk4.newDropDownFromStrings("AM", "PM")

but getSelectedItem() returns a gobject instead of a string of the selected value. How to get the string?


What language binding is that?

In any case, the model used by gtk_drop_down_new_from_string() is a GtkStringList, a list model of GtkStringObject items. You can retrieve the string out of the object using your binding’s wrapper for gtk_string_object_get_string(), or accessing the GtkStringObject:string property.

it’s the nim bindings (gintro). thanks, i’ll try that out.

I hope you get it working. The actual situation for the gintro Nim bindings is, that whenever a function only returns a base type like a Gobject or a Widget, there are two possibilities: When the result is a value that we stored before as the true child type, maybe a GtkButton which is returned as a plain Widget, then we can use a Nim type conversion like Button(res) to get the value. But when it is more some internal GTK value, maybe generated from scratch or when an internal GTK lib value is returned, then we have to do in Nim unfortunately an ugly and unsecur cast like cast[Button](widgetResult). Of course we can create a better looking conversion proc (not a Nim converter!) for that our self, maybe applying some typ checks.

I know that this is a bit complicated and ugly currently. The automatically generated Nim bindings can not offer better solutions. With a lot human manpower we may provide overloaded functions, or we can provide more examples. Both would improve the situation.

You can retrieve the string out of the object using your binding’s wrapper for gtk_string_object_get_string(),

That one is provided, but untested still:

grep  -A9 gtk_string_object_get_string ~/.nimble/pkgs/gintro-#v0.9.2/gintro/gtk4.nim 
proc gtk_string_object_get_string(self: ptr StringObject00): cstring {.
    importc, libprag.}

proc getString*(self: StringObject): string =
  result = $gtk_string_object_get_string(cast[ptr StringObject00](self.impl))

Thanks for the explanation. I do keep gintro source files open in tabs to search for available functions and to try and understand how to use them. I still don’t fully understand what’s going on, but when i get type errors where gintro doesn’t seem able to derive the type that i think i should be able to use directly like: let my_string = getString(getSelectedItem(drop_down1))

In that case i get something like:

Error: type mismatch: got <Object>
but expected one of: 
proc getString(self: DesktopAppInfo; key: cstring): string

(which is a little confusing to me)

I’ll use: let my_string = getString(cast[StringObject](getSelectedItem(drop_down1)))

instead, which works for a dropdown.

Please consider adding this casting topic to your gtk4 book, as i think it’s necessary for real world use. Also, i would appreciate any info you could provide regarding the security implications of using a cast in these situations.

Thanks again. I’ll mark your answer as the solution, as it was the final piece of the puzzle, but i didn’t fully understand the relationships described by ebassi either, so that helped too.

1 Like

The GListModel API operates on generic objects; the type of object inside a GListModel implementation is known to the people implementing it, and it can be derived at run time. There are no introspection annotations for it, because it’s not a base container type. Essentially: if your language binding expects strongly typed guarantees, then you won’t be able to use GListModel without a downcast to a type derived from GObject.


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