[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: KListView and sorting items
From:       André_Wöbbeking <Woebbeking () onlinehome ! de>
Date:       2005-10-16 19:56:22
Message-ID: 200510162156.22933.Woebbeking () onlinehome ! de
[Download RAW message or body]

On Sunday 16 October 2005 20:16, Giovanni Venturi wrote:
>
> Mmm, I don't understand. I used compare() before your reply because
> the sorting was done according the strings and I need according long:
>
>
> int KSnifferViewItem::compare( QListViewItem *i, int col,
>                                  bool ascending ) const
> {
>   int result;
>
>   switch (col)
>   {
>     case 0:
>       // frame number...
>       if (key(col, ascending).toLong() < i->key(col,
> ascending).toLong()) result = -1;
>       else if (key(col, ascending).toLong() > i->key(col,
> ascending).toLong()) result = 1;
>       else
>         result = 0;
>       break;

Don't use key() or you've two conversions (data -> string -> data). Use 
the data directly, i.e.

      if (number < i->number) result = -1;
      else if (number > i->number) result = 1;
      else result = 0;

this small template 

    template<typename Type>
    int
    Compare(Type const& lhs, Type const& rhs)
    {
        if (lhs < rhs)
            return -1;
        else if (rhs < lhs)
            return 1;
        else
            return 0;
    }

is very helpful. With it you can write

  result = Compare(number, i->number);

> Maybe compare() I wrote has some statement that is not for the system
> performance? Can someone reply me?
>
> Redifine text? In which way? I don't understand.

Store your data in your item. With text() you only convert the data on 
demand.
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic