Results 1 to 1 of 1

Thread: Problems with custom sorting of QTreeWidget (problem solved)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Problems with custom sorting of QTreeWidget (problem solved)

    I need to sort a tree. The tree contains TreeItems:

    Qt Code:
    1. class TreeItem : public QTreeWidgetItem
    2. {
    3. public :
    4.  
    5. enum type_t
    6. {
    7. tFile,
    8. tDir
    9. };
    10.  
    11. QString *data;
    12. type_t dtype;
    13. bool parsed;
    14.  
    15. QStringRef title;
    16. QStringRef note;
    17. QStringRef auth;
    18. QStringRef src;
    19. QStringRef date;
    20. QStringRef url1;
    21. QStringRef url2;
    22. QStringRef txt;
    23. int idate;
    24.  
    25. TreeItem();
    26. virtual ~TreeItem();
    27.  
    28. virtual bool operator < ( const QTreeWidgetItem& other ) const;
    29.  
    30. bool SetData( const QString *fd );
    31. bool SetType( const type_t it );
    32. };
    To copy to clipboard, switch view to plain text mode 

    The tree is populated from a (application modal) dialog:

    Qt Code:
    1. void MyFrame::ShowDirDlg()
    2. {
    3. OpenDlg->ShowDlg(true); // show the dialog
    4.  
    5. if( OpenDlg->current != nullptr ) // if selected item acceptable
    6. {
    7. MyTraverser dt; // MyTraverser will load data and populate the tree
    8.  
    9. ui.wndTree->setSortingEnabled(false); // start updating the tree
    10. ui.wndTree->clear();
    11. ui.wndText->clear();
    12.  
    13. dt.tree = ui.wndTree;
    14. dt.traverse(OpenDlg->current->fname); // populate the tree
    15.  
    16. ui.wndTree->setSortingEnabled(true); // sort
    17. ui.wndTree->sortItems(0,Qt::AscendingOrder);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Everything works perfectly except for sorting. The operator < is never called. Without sortItems() the items get sorted but in descending order. With sortItems() the items get sorted in asscending order but still using default (sort by item labels). What am I doing wrong? How to force the tree using my operator < ?

    -----------------------------------

    Edit: Never mind, problem solved. I needed to rebuild instead of build after adding the operator to the header. I've seen this bug (it's definitely a "make" bug) many times even if I am a complete newbie in Qt. The bug consists in:

    - overlooking patched headers and using the old ones somehow. (You can see this while debugging the app.)
    - overlooking patched .ui files (even if you patch from the Creator project and not from the Designer) and using the old ones. (In fact, using the old "ui_form.h" and "ui_form.cpp" files.)

    When this happens, the project "compiles ok" and then crashes mysteriously. Step 1 (be polite): rebuild. Step 2 (be rude): quit Creator, delete debug and release folders, restart, build. Step 1 is usually enough. In the "sort" cause, the project did not crash so that I forgot to rebuild.

    I wonder whether the bug is specific for Linux Debian or whether the bug has been seen elsewhere, too.
    Last edited by Radek; 5th August 2013 at 09:58.

Similar Threads

  1. Sorting QTreeWidget after adding a row
    By xwhatsit in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2011, 23:13
  2. Sorting column in QTreeWidget
    By lni in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2007, 19:35
  3. QTreeWidget Weights/Sorting
    By VireX in forum Qt Programming
    Replies: 27
    Last Post: 25th May 2007, 00:49
  4. Sorting in QTreeWidget
    By adhit in forum Qt Programming
    Replies: 15
    Last Post: 8th May 2007, 13:49
  5. QT4: Sorting in QTreeWidget (subclass)
    By Michiel in forum Qt Programming
    Replies: 21
    Last Post: 29th March 2006, 19:08

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.