Results 1 to 7 of 7

Thread: Filtering a TreeWidget

  1. #1
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Filtering a TreeWidget

    I have tree widget and i want to filter, a certain kind of child

    I wrote the following slot that gets triggered whenever the contents of the lineedit eBrowseFilter changes:
    Qt Code:
    1. void QMyClass::refilterBrowseTree()
    2. {
    3. bool hide = true;
    4.  
    5. QObjectList children = browseTree->children();
    6. browseTree->setUpdatesEnabled(false);
    7. for (int i = 0; i < children.count(); ++i)
    8. {
    9. c = (QTreeWidgetItem*)children.at(i);
    10.  
    11. hide = false;
    12. if (c->type()==1000)
    13. {
    14. if (!c->text(0).contains(eBrowseFilter->text()))
    15. hide = true;
    16. }
    17. c->setHidden(hide);
    18. }
    19. browseTree->setUpdatesEnabled(true);
    20. browseTree->update();
    21. }
    To copy to clipboard, switch view to plain text mode 
    The Program segfaults as soon as I enter something into eBrowseFilter... The Backtrace tells me it actually failed in QTreeWidget::setItemHidden()...
    How can that be? Whats wrong?

    Thanks for your help!

    Disclaimer:
    I know Qt4 has this great model-view-thing, but I personally think it rather inconvenient, since QTreeWidget doesnt allow me to setModel (its private), and reimplementing all the Item-based features for QTreeView looks like a lot of work (after all I am really happy with QTreeWidget, i just want to do some filtering).
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Filtering a TreeWidget

    Quote Originally Posted by soul_rebel View Post
    The Program segfaults as soon as I enter something into eBrowseFilter... The Backtrace tells me it actually failed in QTreeWidget::setItemHidden()...
    How can that be? Whats wrong?
    Tree widget items are not widgets nor even QObjects and you treat them as such thus casting objects from the list to QTreeWidgetItem gives you trash instead of objects you expect and your code goes into the bushes, as we say in Poland

    Disclaimer:
    I know Qt4 has this great model-view-thing, but I personally think it rather inconvenient, since QTreeWidget doesnt allow me to setModel (its private),
    Because QTreeWidget is meant to hide the model-view architecture.

    and reimplementing all the Item-based features for QTreeView looks like a lot of work (after all I am really happy with QTreeWidget, i just want to do some filtering).
    Take a look at QStandardItemModel. It does more or less the same as convenience widgets.

    I never had to reimplement QTreeView or any other view to make it do what QTreeWidget or family do out of the box. If you think you have to, then maybe you're just missing some feature in the model or in the view.

    Disclaimer
    I don't say Item Views are perfect - they are not. But they are really nice to use in many cases.

  3. #3
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Filtering a TreeWidget

    I would recommend same as wysota says. Model/view.

    If you do that, you can use the Qsortproxyfiltermodel to filter /proxy on different views at the same time if needed as well. This make it easier to store loads of parameters not visible to the user, but needed in data structures, while you still can have some parameters visible to the user if you want. Also you can implement custom data/setdata functions on a proxy model, that makes it easier to edit the same data from different places or different widgets/input methods, while the original model keeps it's standard model...

    That wasn't well written, but you get the point...

  4. #4
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Filtering a TreeWidget

    Ok, I got it to filter like I want it to ( the Problem was - as Wysota pointed out - casting the children ):
    Qt Code:
    1. void QMyClass::refilterBrowseTree()
    2. {
    3. bool hide = true;
    4. *cc = 0;
    5. browseTree->setUpdatesEnabled(false);
    6.  
    7. for (int i = 0; i < browseTree->invisibleRootItem()->childCount(); ++i)
    8. {
    9. c = browseTree->invisibleRootItem()->child(i);
    10.  
    11. for (int ii = 0; ii < c->childCount(); ++ii)
    12. {
    13. cc = c->child(ii);
    14. hide = false;
    15. if (cc->type()==1000)
    16. {
    17. if ( !cc->text(0).contains(eBrowseFilter->text()) ) )
    18. hide = true;
    19. }
    20. cc->setHidden(hide);
    21. }
    22. }
    23. browseTree->setUpdatesEnabled(true);
    24. browseTree->update();
    25. }
    To copy to clipboard, switch view to plain text mode 
    The only problem is that it takes about 20seconds to do it (the UI hangs during that time).
    There are about 17K items in the list, but filtering a treewidget in kde3/qt3 with klistviewsearchline didnt take any time (and klistviewsearchline iterates through the items as well if I understood it correctly)...

    I had a closer look at Model/View-Programming and think it is really confusing. If you think there is no way around it I'll open a new thread and try to solve my problems with a new Model/View Thing, but if you have an idea how to to filter faster doing what I do right now, I would be happy!

    Thanks!
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Filtering a TreeWidget

    Try to find the bottleneck using a profiler. There is no point in trying to optimize something if it is something else that greatly reduces the performance.

  6. #6
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Filtering a TreeWidget

    Quote Originally Posted by wysota View Post
    Try to find the bottleneck using a profiler. There is no point in trying to optimize something if it is something else that greatly reduces the performance.
    Well, unfortunately valgrind ist broken on FreeBSD right now, valgrind-devel works but crashes my application (which doesnt crash when being run alone).
    Really weird and I know to little about Memory-Management-Internals to debug this.
    I'll try to setup a Model-View for my data and ask in a new thread when I run into Problems.
    Quote Originally Posted by Bertolt Brecht
    What is the robbing of a bank compared to the founding of a bank?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Filtering a TreeWidget

    Use gprof.

Similar Threads

  1. expand a treeWidget
    By mattia in forum Newbie
    Replies: 6
    Last Post: 6th December 2007, 13:24
  2. How to sort the treeWidget item according to size and date
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 10:32
  3. How to set the sizehint for treeWidget
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 12th June 2007, 11:35
  4. By default TreeWidget width setting in DockWidget
    By santosh.kumar in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2007, 05:55
  5. Replies: 2
    Last Post: 29th May 2007, 13:55

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.