Results 1 to 15 of 15

Thread: Items in QListView should sort on Header Click

  1. #1
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Items in QListView should sort on Header Click

    Hai

    I have a QListView where i have 2 headers by ID and Name.Id is handling only numeric items where as Name is handling alphanumeric.I have written a logic to sort the alphanumeric (using sorting) where as the same logic is effecting the numeric ones when both the single digits and multiple digits are there.The click on the ID is sorting the numbers this way(10,11,12,13,14,2,3,4,5 etc on one click and on other click this way :5,4,3,2,14,13,12,11,10).just like the strings the numeric are also sorted because here i have converetd the numerics to strings to map the correcssponding items because both the ID and Name are interrelated.

    could anyone help me in sorting the Numerics on header click.

  2. #2
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    hai
    once again i am back with the previous question with clear details wat i need exactly..
    as i am not getting any response i am putting the question once again clearly

    Hi, I have a little problem with a QListView:

    I have a column that's a number and I want to sort the QListView by this
    column. The problem is that the sorting of list is alfanumeric
    because the column is a string and I want to sort 'numericaly' without
    padding '0' before each number.

    My Sorting The sort I want

    1 1
    10 2
    11 3
    12 4
    13 5
    14 6
    2 7
    3 8
    4 9
    5 10
    6 11
    ... ...

    I have no option of overriding compare because my parent class has been derived from QDialog and the dervived from QListview not from QListviewItem.
    if i am using compare i am getting the build error as compare is not a member of QListview.

    help is in urgent.

    thanks for the help

    bye
    vinni

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Create your own QListViewItem subclass and reimplement QListViewItem::compare().

  4. #4
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    hai

    cant i do it with out overriding compare.Is there any other way of sorting the numerics other than overriding comapare.

    thanks for the help
    vinnu

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Quote Originally Posted by vinnu View Post
    cant i do it with out overriding compare.Is there any other way of sorting the numerics other than overriding comapare.
    The easiest way is to reimplement QListViewItem::compare(), because that's what it was meant for, but you can also reimplement QListView::sort().

  6. #6
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    yes i have implemented it but it is not working for numerics .numerics are also getting sorted in the same way as that of strings.
    i.e for example if i have values like 2,4,6,8,10,11,12,13,14,20,21....
    when i click on column header it is sorting in this way.
    10,11,12,13,14,2,20,21...which is exactly the string sort...
    but i want it to get sorted in the numerics way...i.e 2,10,11,12,13,14,20,21....

    can u plz help me...i am in great need of it.

    thank u
    vinnu

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Quote Originally Posted by vinnu View Post
    yes i have implemented it but it is not working for numerics
    How did you implement it?

  8. #8
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    hai
    buddy,I think u havent understand my problem clearly.

    When i have used it numeric is working properly but alphanumerics are not sorting according to order.
    I have 2 headers in which one is purely numeric(ID column) and other is alphanumeric(Name column).
    by default i am able to sort the Numeric feild correctly.When i have written code for Alphanumeric type i.e using strcmp numeric feild have got disturb.these are getting sorted in the same way as string sorting(1,10,11,12,13,14,15,2,21,22,23,3,31,32.... .... )but i want in the order(1,2,3,10,11,12,13,14,15,21,22,23,24,31,32... ..).
    So with out using QListView::sort() also i am able to sort the mumerics but the logic of strcmp for alphanumerics disturbed numeric sort.

    thanks for the help...

    vinnu

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Quote Originally Posted by vinnu View Post
    So with out using QListView::sort() also i am able to sort the mumerics but the logic of strcmp for alphanumerics disturbed numeric sort.
    You need two sorting routines and you have to choose one depending on sortColumn() and sortOrder() values.

    Everything would be much easier if you would reimplement QListViewItem::compare() since, QListView would handle the rest automatically.

  10. #10
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    Hai

    I dont have any derived class that is derived from QListviewitem and i have no oppurtunity of deriving the class from QLIstviewItem.I have a class derived from QLIstView and thats it.When i use compare i get the problem as "Compare is not a member of QListview".So without overriding compare do i have any other method.

    When i have surfed the net i have got a solution of padding the items with zeros or spaces.so i have opted for spaces.
    But i feel this is not the generic answer so looking out for other solutions other than this.

    thanks
    vinnu

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Quote Originally Posted by vinnu View Post
    I dont have any derived class that is derived from QListviewitem and i have no oppurtunity of deriving the class from QLIstviewItem.
    Why? QListViewItem is meant to be subclassed.

    Quote Originally Posted by vinnu View Post
    When i use compare i get the problem as "Compare is not a member of QListview".
    Because compare() is a method of QListViewItem, not QListView. If you want to reimplement compare(), then you have to subclass QListViewItem, not any other class.

    Quote Originally Posted by vinnu View Post
    But i feel this is not the generic answer so looking out for other solutions other than this.
    If you really don't want to use QListViewItem::compare(), then you have to reimplement QListView::sort(), but do it properly --- check the value of QListView::sortColumn and sort either numerically or alphabetically.

  12. #12
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    hai

    OK...let me say my way of implementation....
    i have written the connect stmt to the header click.

    Qt Code:
    1. QHeader * pHeader = m_pBBListView->header();
    2. connect(pHeader,SIGNAL(clicked(int)),this, SLOT(sortColumns(int)));
    To copy to clipboard, switch view to plain text mode 

    in Sortcolumn() i have added the following functionality.

    Qt Code:
    1. void BBPickerDialog::sortColumns(int index)
    2. {
    3. BBPtr<BBStruct>listViewItemStruct(BBStruct::create("Item", "Item"));//it is our struct where we handle the our list view items//
    4.  
    5. QListViewItemIterator it( m_pBBListView );//m_pBBListview is member of BBpicker Dialog.
    6. int valueIndex = (index == 0)?1:0;
    7.  
    8. PChar tempStr,tempStr2;
    9.  
    10. for ( ; it.current(); ++it)
    11. {
    12. QListViewItem *pItem = it.current();
    13. QString key = pItem->text(index);
    14. QString value = pItem->text(valueIndex);
    15. sortList->addCopy(key.latin1());
    16. listViewItemStruct->addStringField(key.latin1(),value);
    17. }
    18. for(size_t i=0;i<sortList->size();i++)
    19. {
    20. for(size_t j = i+1 ;j <sortList->size()-1;j++)
    21. {
    22. if(strcmp((*sortList)[i],(*sortList)[j])>0)
    23. {
    24. tempStr = strDup((*sortList)[i]);
    25. tempStr2 = strDup((*sortList)[j]);
    26. sortList->replaceItemAtIndex(i,tempStr2);
    27. sortList->replaceItemAtIndex(j,tempStr);
    28. }
    29. }
    30. }
    31. QListViewItem *pLastItem = NULL;
    32. m_pBBListView->clear();
    33.  
    34. for(size_t indx=0;indx<sortList->size();indx++)
    35. {
    36. BBPtr<BBString> value(listViewItemStruct->getStringField((*sortList)[indx]));
    37. if(index==0)
    38. pLastItem = new QListViewItem( m_pBBListView,(*sortList)[indx],value->asPChar());
    39. else
    40. pLastItem = new QListViewItem( m_pBBListView,value->asPChar(),(*sortList)[indx]);
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    u can understand my code ...

    i think now u can understand my problem
    Last edited by jacek; 6th November 2006 at 14:37. Reason: missing [code] tags

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Items in QListView should sort on Header Click

    Quote Originally Posted by vinnu View Post
    u can understand my code ...
    Unreadable code is a bad code.

    First let's see how many lines of code it would take to implement it using QListViewItem::compare():
    Qt Code:
    1. int CustomListViewItem::compare( QListViewItem * item, int col, bool ascending ) const
    2. {
    3. Q_UNUSED( ascending ); // ignored, like the docs recommend
    4. if( col == 0 ) {
    5. int a = text( col ).toInt();
    6. int b = item->text( col ).toInt();
    7. return (a - b);
    8. }
    9. else {
    10. return QString::localeAwareCompare( text( col ), item->text( col ) );
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    See? It wasn't that hard.

    Now for the sortColumns(). First of all you use QString::latin1(). What will happen with non-ASCII characters? And why do you delete all items, if you just need to reorder them? Why don't you reuse existing code?

    I would do it this way:
    Qt Code:
    1. class NumericalComparator
    2. {
    3. public:
    4. NumericalComparator( int column ) : _column( column ) {}
    5.  
    6. bool operator()( const QListViewItem *a, const QListViewItem *b ) const
    7. {
    8. return ( ( a->text( _column ).toInt() - b->text( _column ).toInt() ) > 0 );
    9. }
    10.  
    11. private:
    12. int _column;
    13. };
    14.  
    15. class StringComparator
    16. {
    17. public:
    18. StringComparator( int column ) : _column( column ) {}
    19.  
    20. bool operator()( const QListViewItem *a, const QListViewItem *b ) const
    21. {
    22. return ( QString::localeAwareCompare( a->text( _column ), b->text( _column ) ) > 0 );
    23. }
    24.  
    25. private:
    26. int _column;
    27. };
    28.  
    29. void BBPickerDialog::sortColumns(int index)
    30. {
    31. m_pBBListView->setUpdatesEnabled( false );
    32.  
    33. std::vector< QListViewItem * > items( m_pBBListView->childCount() );
    34.  
    35. for( QListViewItemIterator it( m_pBBListView ); it.current(); ++it ) {
    36. items.push_back( it.current() );
    37. }
    38.  
    39. if( index == 0 ) {
    40. std::sort( items.begin(), items.end(), NumericalComparator( index ) );
    41. }
    42. else {
    43. std::sort( items.begin(), items.end(), StringComparator( index ) );
    44. }
    45.  
    46. for( std::vector< QListViewItem *>::iterator it = items.begin(); it != items.end(); ++it ) {
    47. m_pBBListView->takeItem( *it );
    48. m_pBBListView->insertItem( *it );
    49. }
    50.  
    51. m_pBBListView->setUpdatesEnabled( true );
    52. }
    To copy to clipboard, switch view to plain text mode 
    Now (provided that this code works, as I didn't test it) you can see that you don't need to copy anything, except for the pointers, and that you can easily reuse existing code (in this case from STL).

    If you don't like STL, you can do a similar thing using QPtrList --- just subclass QPtrList<QListViewItem> and reimplement compareItems().

  14. #14
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    Hai

    Thank u .i will put it in my code and see whether it works or not and come to u again..

    thanks a lot jaeck.

    vinnu

  15. #15
    Join Date
    Sep 2006
    Posts
    21
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Items in QListView should sort on Header Click

    Hai jacek,

    Thank u for ur help in time.I have approached my clients and said the problem and i have got the green signal to derive a class from QListviewItem and overide compare.It worked for me .

    Thank u so much

    bye
    vinnu

Similar Threads

  1. Drag and drop items in view to sort order
    By Big Duck in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2006, 20:43
  2. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 13:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.