Search code examples
gtkvala

width of a gtk.combobox with entry in vala


i'm beginning with vala and i'm unable to set the width of a Gtk.ComboBox. In Python i can do it like so:

combo = Gtk.ComboBox.new_with_entry()
entry = combo.get_child()
entry.set_width_chars(12)

in vala this:

var combo = new ComboBox.with_entry ();
var entry = combo.get_child();
entry.set_width_chars(12);

gives:

error: The name `set_width_chars' does not exist in the context of `Gtk.Widget'

altough this:

var combo = new ComboBox.with_entry ();
var entry = combo.get_child();
if (entry is Gtk.Entry)
    stdout.printf("Entry!\n")

prints "Entry!", so where is the problem?


Solution

  • (Disclaimer: I have extremely limited exposure to vala but I hope this helps )
    The issue could be the return type from get_child(), it is Gtk.Bin function which returns Gtk.Widget and not Gtk.Entry for which set_width_char() is valid. This is because a child widget of Gtk.Bin can be any widget. Try static typecasting:

    var entry = (Entry)combo.get_child();
    

    or more convoluted version

    var entry = combo.get_child();
    ((Entry)entry).set_width_chars(12);
    

    And as for the successful type check to Gtk.Entry, although what you have Gtk.Widget the type is of Gtk.Entry as the child added to combobox is an entry.

    Hope this helps!