Re: Problems with setting values for combobox
| From: | Jake Cobb | Date: | Thu, 26 Apr 2007 18:40:41 +0000 |
| Subject: | Re: Problems with setting values for combobox | ||
| References: | 1 2 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7797@lists.php.net to get a copy of this message | ||
One quick correction, in the second method the line:
$this->cmbTur->set_active_iter($row["iter"]);
should be:
$this->cmbTur->set_active_iter($row->iter);
-Jake Cobb
Jake Cobb wrote:
Jon, Is the value you want to set definitely in the combo box model already? If this is a choice between a static set of options, equivalent to an enum in the database, you might try this: $user_value = getUserValue(); //dummy function, whatever you do to get the value $model = $this->cmbTur->get_model(); $iter = $model->get_iter_first(); //iter to the first element while($iter != null) { $value_at_iter = $model->get_value($iter,0); if($value_at_iter == $user_value) {$this->cmbTur->set_active_iter($iter); break;} $iter = $model->iter_next($iter); //null if no more elements } if($iter == null) print("Value not present in combo box model."); You can also use a path instead of an iterator if that's more convenient for you. Since a list store has only one level, paths starting at 0 reference its elements. To set the first element active: $this->cmbTur->set_active(0); This should suit your purposes, for another way of doing this, read on. This is the "native" Gtk+ way of handling these objects. Andrei has added the ability for PHP-GTK to manipulate and traverse these types of objects as arrays. See the mailing list archive at the URL below: http://marc.info/?l=php-gtk-general&m=114029530823343&w=2 His examples are more complicated than the usage here because he's showing a GtkTreeStore. GtkListStore is basically a much simpler form of this. So, as an alternative to above, you could try this: $user_value = getUserValue(); //dummy function $model = $this->cmbTur->get_model(); if(empty($model)) print("No elements in the model.\n"); else { $matched = false; foreach($model as $row) //$row is a GtkTreeRow {$value_at_iter = $row[0]; if($value_at_iter == $user_value) { $this->cmbTur->set_active_iter($row["iter"]); $matched = true; break; }} if(!$matched)print("Value not found in the combo box model.\n");} Hope some of that is helpful. -Jake Cobb Jon Lurås wrote:Hello! I have a combobox where the user can choose a value and save this value in a database. The value is retrieved from the combobox with the following code:$model = $this->cmbTur->get_model(); $iTur = $model->get_value($this->cmbTur->get_active_iter(), 0);My problem is too set this combobox too the same value that the user choose when she reads the values back from the database. I am quite sure that the solution involves$this->cmbTur->set_active_iter();Can anybody help me? Combo and ListStore setup: $lstStrTur = new GtkListStore(Gtk::TYPE_LONG,Gtk::TYPE_STRING); $lstStrTur->append(array(1,'Test 1')); $lstStrTur->append(array(2,'Test 2')); $lstStrTur->append(array(3,'Test 3')); $this->cmbTur = new GtkComboBox(); $this->cmbTur->set_model($lstStrTur); $cellRenderer = new GtkCellRendererText(); $this->cmbTur->pack_start($cellRenderer); $this->cmbTur->set_attributes($cellRenderer, 'text', 1); $table->attach($this->cmbTur, 1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 0, 0); I borrowed some code from the very helpful "php-gtk2 Cookbook" site. I have been searching and reading around the Net and now I have to ask in the list :-)