Check Gtk2::ListStore empty or not

View: New views
4 Messages — Rating Filter:   Alert me  

Check Gtk2::ListStore empty or not

by Peter Dennis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

Is it possible to check if a Gtk2::ListStore is empty?

At the moment I have a combo box with a list of items.  The user can
select items from the combo box and add them to a list.  But I don't
want them to be able to add duplicate items to the list.

So first I was going to check if the list was empty.  If it was then
it can add an item, but if it wasn't then get an array of the items
already in the list store, loop through them checking that the combo
selection isn't already there and then add the item.

I'm not entirely sure how to check if the list is empty.  At the
moment I'm trying:
if ($list_store->get_iter_first)
#or
if (defined $list_store->get_iter_first)
  # get what's there and check for duplicates
else
 # add item to list

but this doesn't work because when I try to get everything from my first column:
my @col1 = $list_store->get($list_iter, 1);

It says:
variable not allowed to be undef where GtkTreeIter is wanted at line 331

which matches the 'my @col1' line above.

Could someone please help me with the testing logic.  I understand it
needs to be defined, but it can only be defined if there's something
in the row, but then I can't test if it's empty.

Very confused.....

Many thanks,

Peter.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Check Gtk2::ListStore empty or not

by Tadej Borovšak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi.

I would use construct like this one:

--- CODE ---
my $iter = $list_store->get_iter_first;
if( $iter )
    # Get contents of first line
    my @line = $list_store->get( $iter );
else
    # Add new line

--- CODE ---

This might not be the best way, but it should work.

Tadej

--
Tadej Borovšak
tadeboro.blogspot.com
tadeboro@...
tadej.borovsak@...
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Check Gtk2::ListStore empty or not

by anguila :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

integer = $tree_model->iter_n_children ($iter=undef)

Returns the number of children $iter has. If $iter is undef (or omitted) then returns the number of toplevel nodes.

with $list_store->iter_n_children(undef) you will know if is empty or not and the rows that it has throug a integer value.


The way of tadej showed is correct too.

David.

On Mon, Jul 27, 2009 at 1:49 PM, Tadej Borovšak <tadeboro@...> wrote:
Hi.

I would use construct like this one:

--- CODE ---
my $iter = $list_store->get_iter_first;
if( $iter )
   # Get contents of first line
   my @line = $list_store->get( $iter );
else
   # Add new line

--- CODE ---

This might not be the best way, but it should work.

Tadej

--
Tadej Borovšak
tadeboro.blogspot.com
tadeboro@...
tadej.borovsak@...
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list


_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Re: Check Gtk2::ListStore empty or not

by Peter Dennis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/7/27 anguila <anguila@...>:

> integer = $tree_model->iter_n_children ($iter=undef)
>
> $iter (Gtk2::TreeIter or undef)
>
> Returns the number of children $iter has. If $iter is undef (or omitted)
> then returns the number of toplevel nodes.
>
> with $list_store->iter_n_children(undef) you will know if is empty or not
> and the rows that it has throug a integer value.
>
>
> The way of tadej showed is correct too.
>
> David.
>
> On Mon, Jul 27, 2009 at 1:49 PM, Tadej Borovšak <tadeboro@...> wrote:
>>
>> Hi.
>>
>> I would use construct like this one:
>>
>> --- CODE ---
>> my $iter = $list_store->get_iter_first;
>> if( $iter )
>>    # Get contents of first line
>>    my @line = $list_store->get( $iter );
>> else
>>    # Add new line
>>
>> --- CODE ---
>>
>> This might not be the best way, but it should work.
>>
>> Tadej
>>

Thank you both for the suggestions.  I will try them out and see how I go.
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list@...
http://mail.gnome.org/mailman/listinfo/gtk-perl-list