Results 1 to 15 of 15

Thread: Items in QListView should sort on Header Click

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 13:37. Reason: missing [code] tags

  2. #2
    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().

  3. #3
    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

  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 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, 19:43
  2. moving Qlistview items
    By :db:sStrong in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2006, 12: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
  •  
Qt is a trademark of The Qt Company.