 |
| View previous topic :: View next topic |
| Author |
Message |
stefan
Joined: 17 Apr 2006 Posts: 35 Location: Germany
|
Posted: Sun Jul 23, 2006 3:34 am Post subject: liststore: insert_before |
|
|
Hi there,
i'm trying to use the insert_before and insert_after methods of GtkListStore, but everything i tried so far, returns
a) error messages, like
| Quote: | | Fatal error: Argument 2 passed to GtkListStore::insert_before() must be an object of class GtkTreeIter |
with the following code | Code: |
$selection = $abl->get_selection();
list($m, $iter) = $selection->get_selected();
if (empty($iter)){
$model->append(array('', 'new', 'row', 'at', 'the', 'end', '', '', ''));
}
else {
$model->insert_before($iter, array('here', 'should', 'be', 'an', 'iter', '', '', '', ''));
} |
or
b) appends two lines instead of one... (one on the right position, one on the last) with the following code:
| Code: |
...
else {
$its = $model->append(array('', 'test', '', '', '', '', '', '', ''));
$model->insert_before($its, $iter);
}
|
hope someone can help... |
|
| Back to top |
|
 |
stefan
Joined: 17 Apr 2006 Posts: 35 Location: Germany
|
Posted: Sun Jul 23, 2006 3:47 am Post subject: |
|
|
allright, i've found out how it goes.
don't ask me why, but you have to use:
| Code: |
$selection = $abl->get_selection();
list($model, $iter) = $selection->get_selected();
if (empty($iter)){
$model->append(array('', 'sdg', '', '', '', '', '', '', ''));
}
else {
$model->insert_before($iter, $iter);
} |
|
|
| Back to top |
|
 |
cweiske
Joined: 08 Dec 2005 Posts: 454 Location: Leipzig/Germany
|
Posted: Sun Jul 23, 2006 9:52 am Post subject: |
|
|
| Code: | | GtkListStore::insert_before(GtkTreeIter $iter, GtkTreeIter $sibling); |
After that, you can use set() |
|
| Back to top |
|
 |
stefan
Joined: 17 Apr 2006 Posts: 35 Location: Germany
|
Posted: Thu Jul 27, 2006 12:17 pm Post subject: |
|
|
Hi there,
just one more question
using insert_before works now, but only as long as i do not sort the colums! if i do that, the new row will be inserted as last or first row, depends on the sorting. Why??
And, to make it more funny, if i select one of the new rows, there the insert_before methods works again???
in the following, i post some parts of the code, mainly the part, where i create my columns and the method, where do insert a new row:
first the columns: | Code: |
$column1 = new GtkTreeViewColumn();
$column1->set_title('lfd. Nr.');
$column1->set_sizing(Gtk::TREE_VIEW_COLUMN_AUTOSIZE);
$column1->set_resizable(true);
$cellRenderer = new GtkCellRendererText();
$column1->pack_start($cellRenderer, true);
$column1->add_attribute($cellRenderer, 'text', 1);
$cellRenderer->set_property('ellipsize', Pango::ELLIPSIZE_END);
$cellRenderer->set_property('editable', true);
$cellRenderer->connect('edited', array($this, 'cell_edited'), 1);
$column1->set_sort_column_id(1);
$this->insert_column($column1, 0);
|
second my insert_row method:
| Code: | public function insertRow()
{
$abl = ArchivModul_AblieferungTree::singleton();
$model = $abl->get_model();
list($id, $lfdnr) = $this->get_ids($model);
$iterfirst = $model->get_iter_first();
if (empty($iterfirst)) {
$model->append(array($id, $lfdnr, '', '', '', '', '', '', ''));
}
else {
$selection = $abl->get_selection();
list($m, $iter) = $selection->get_selected();
if (empty($iter)){
$dialogOptions = 0;
$dialogOptions = $dialogOptions | Gtk::DIALOG_MODAL;
$dialogOptions = $dialogOptions | Gtk::DIALOG_DESTROY_WITH_PARENT;
$dialogOptions = $dialogOptions | Gtk::DIALOG_NO_SEPARATOR;
$dialogButtons = array();
$dialogButtons[] = Gtk::STOCK_OK;
$dialogButtons[] = Gtk::RESPONSE_OK;
$dialog = new GtkDialog('Info', null, $dialogOptions, $dialogButtons);
$hBox = new GtkHBox();
$dialog->vbox->pack_start($hBox);
$info = GtkImage::new_from_stock(Gtk::STOCK_DIALOG_INFO, Gtk::ICON_SIZE_DIALOG);
$hBox->pack_start($info, false, false, 5);
$label = new GtkLabel("Kein Eintrag ausgewählt!");
$hBox->pack_start($label);
$dialog->vbox->show_all();
if ($dialog->run() == Gtk::RESPONSE_OK) {
$dialog->destroy();
}
} else {
$m->insert_before($iter, $iter);
$m->set($iter, 0, $id, 1, $lfdnr, 2, '', 3, '', 4, '', 5, '', 6, '', 7, '', 8, '');
}
}
}
|
hope you can help... |
|
| Back to top |
|
 |
ron_t
Joined: 11 Dec 2005 Posts: 78 Location: Gatineau, Quebec
|
Posted: Thu Jul 27, 2006 4:50 pm Post subject: |
|
|
| stefan wrote: | Hi there,
just one more question
using insert_before works now, but only as long as i do not sort the colums! if i do that, the new row will be inserted as last or first row, depends on the sorting. Why??
|
If memory serves, iters are volatile and you can't count on them always pointing at the same position, especially after sorting. Try collecting another iter _after_ the sort.
| stefan wrote: |
And, to make it more funny, if i select one of the new rows, there the insert_before methods works again???
|
I can't explain this, but I think it's blind luck.
-Ron T. |
|
| Back to top |
|
 |
stefan
Joined: 17 Apr 2006 Posts: 35 Location: Germany
|
Posted: Thu Jul 27, 2006 10:29 pm Post subject: |
|
|
Hey Ron,
the insert_row-method will be executed each time, the user clicks the corresponding button.
within this method, i get the underlying model with get_model(). if the model was sorted before, i should get the sorted model with get_model().
so i guess the received iters should work.
but please correct if i'm wrong...
so long
stefan |
|
| Back to top |
|
 |
ron_t
Joined: 11 Dec 2005 Posts: 78 Location: Gatineau, Quebec
|
Posted: Fri Jul 28, 2006 7:14 am Post subject: |
|
|
| stefan wrote: | Hey Ron,
the insert_row-method will be executed each time, the user clicks the corresponding button.
within this method, i get the underlying model with get_model(). if the model was sorted before, i should get the sorted model with get_model().
so i guess the received iters should work.
but please correct if i'm wrong...
stefan |
I suspect all that you've said is true, but that doesn't update the iter unless the insert_row() method also grabs a selection, then derives an iter from it. If I'm not mistaken, it's not the model that's the problem. It's the selection object you get from the model and that's why your iter isn't up-to-date.
-Ron T. |
|
| Back to top |
|
 |
stefan
Joined: 17 Apr 2006 Posts: 35 Location: Germany
|
Posted: Fri Jul 28, 2006 3:27 pm Post subject: |
|
|
Hey Ron,
as you can see in my posted code, i do get the actual selection within the insertRow-Method, so the iter should be up-to-date.
any other suggestions??
stefan |
|
| Back to top |
|
 |
ron_t
Joined: 11 Dec 2005 Posts: 78 Location: Gatineau, Quebec
|
Posted: Fri Jul 28, 2006 6:41 pm Post subject: |
|
|
| stefan wrote: | Hey Ron,
as you can see in my posted code, i do get the actual selection within the insertRow-Method, so the iter should be up-to-date.
any other suggestions??
stefan |
I must admit I didn't actually read your code the first time. However, after a quick glance, the creation of the selection looks conditional and only happens if a new item is not added.
You might be better off to get the new selection directly after the sort and not while doing the insert. Once the insert is done, you'll have to get another selection, of course.
-Ron T. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
 |