Re: Making Glade created ComboBox behave like Gtk2::ComboBox->new_text?



Martin Junius wrote:

Dear list,

in my application I'm using empty ComboBox'es created in Glade, which
are later filled with option values from a database. If the Glade file
contains an empty <property name="items" translatable="yes"> </property>
(this happens if you type a space into the "Items" field in Glade), I
can use append_text() to add the option values to the ComboBox. The
ComboBox then seems to behave like one created with
Gtk2::ComboBox->new_text.

If the items property is missing (default in Glade), this doesn't work
because then the CombBox model isn't initialized.

If I use code similar to

   my $model = new Gtk2::ListStore('Glib::String');
...
   for ... {
        $model->set($model->append, 0, ...);
   }
   $cbox->set_model($model);

this doesn't work either, the cell renderers seem to be messed up. cbox
is the Glade ComboBox widget.


You mean the 'ComboBox' from Gtk-2.2? If so, that's been depreciated for quite a while. What version of Gtk to do have? If you've got Gtk-2.4 or higher, you should use a 'ComboBoxEntry'. It allows you to use a Gtk2::ListStore ( as you're trying to do above ) to store values for your combo box. I think that's what your problem here is - you're trying to use an old ComboBox as a ComboBoxEntry.

Any ideas? Basically, I want to convert the empty ComboBox to one, where
I can just use append_text().

I wouldn't use that append_text method of getting values into the Combo's list. Gtk-2.4's ComboBoxEntry is the way to go, and use a loop as you have above to append ID / String pairs to the model. I call this little function to populate ComboBoxEntry models all over the place:

---

sub create_combomodel {
# Returns a model for use in a GtkComboBoxEntry my ( $dbh, $sql, $fields ) = @_; my $liststore = Gtk2::ListStore->new(
                                           $fields->{id_def},
                                           $fields->{display_def}
                                       );
my $sth = $dbh->prepare($sql);
   $sth->execute;
my $iter; while (my $row = $sth->fetchrow_hashref) { $iter = $liststore->append; $liststore->set( $iter, 0, $row->{$fields->{id}}, 1, $row->{$fields->{display}} ); } return $liststore; }

---

I then do something like:

---

Example usage:
my $model = create_combomodel(
                                   $dbh,
"select BusTypeID, BusinessType from BusinessTypes",
                                   {
                                      id          => "BusTypeID",
                                      id_def      => "Glib::Int",
                                      display     => "BusinessType",
                                      display_def => "Glib::String"
                                   }
                            );

$widget = $prospects->get_widget("BusinessTypes");
$widget->set_model($model);
$widget->set_text_column(1);

---

Also, if you're doing database forms, check out my projects at:
http://entropy.homelinux.org/axis_not_evil

--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak nusconsulting com au
website: http://www.nusconsulting.com.au



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]