Results 1 to 14 of 14

Thread: Practical QTreeView: Advice needed

  1. #1
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Practical QTreeView: Advice needed

    Hello,

    I need some help using the QTreeView.
    I'm having problems fitting my datastructure into QT4s Interview MVC framework.

    Let my explain you what I want to do:
    I have a couple of files saved in an array which looks basically like this:

    Qt Code:
    1. struct file
    2. {
    3. string absoluteFilename; // Eg. "/var/foo/xx.log"
    4. };
    5. vector<file> _files;
    To copy to clipboard, switch view to plain text mode 

    Now I want to present this vector of files in a tree widget. My question is: How can I map this array of filepaths to the QAbstractItem (row, column) access? I want to show every folder as a node, and each file as a leaf.
    I've thought about this problem for quite some time, but all I can think of is some really ugly mess of alot of code.
    There must be some easy way to integrate a filestructure (not using QDirModel, because the files are scattered over several locations) into a tree.

    Thanks for help,
    Bye, Jojo

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Build a "tree" of ModelIndices:
    Create for every entry in your vector an ModelIndex with a parent. The root of your tree has no parent and becomes the root-item. For every other entry you have to find out its parent recursively.
    The row and column indices depend on their "parent": Their model index. In every Folder they start again from 0.

    The simple tree example should give you some more ideas.

  3. #3
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Hello,

    this is what I have partially implemented.
    But it totally sucks.

    I have to inherit from a builtin class, override 5 virtual functions, build up another storage system for an existing model (they both need to be synced too) .. and this all to display some files in a tree? Looking at my logging output, I also see QT is calling the rowCount and data functions like crazy.

    This approach is really a pita. I've tried alot of different GUI toolkits before QT, but none were so utterly complicated when it came to filling a tree with items.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    We have a problem:
    From your point of view: MVC does not allow easy display of trees
    From Qt's: your data structure is not suited for display in a tree.
    Further neither datastructure can be changed...

    When QTreeView is to complex and / or complicated use QTreeWidget: Instead of building a tree of model indices create a tree of QTreeWidgetItems. Store in every item the index into your vector. Then you can easily synchronize both representations.
    In this approach searching for an item of the vector in the tree by index is bad: So far I have no fast solution but to iterate through all items - use the path instead.

  5. #5
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: Practical QTreeView: Advice needed

    Hello,

    I've taken your advice and I have converted tree stuff to using QTreeWidgetItems now.
    Though I have a nasty problem:
    I can build the tree correctly, but no text or backgroundcolor is used to the shown elements. This means I see the structure of the tree not its contents.
    I have no clue what might be wrong here, I suppose the QTreeWidget has a flaw of some kind.
    I paste some code here, maybe you can look over it, to see if something is wrong with it. It looks right to me.

    Qt Code:
    1. void buildTree (QTreeWidgetItem* treeWidgetItem )
    2. {
    3. QTreeWidgetItem* child = new QTreeWidgetItem ( QStringList() << "xxx" );
    4. child->setText (0, "xxx"); // not used by Qt
    5. child->setText (1, "xxx"); // not used
    6. child->setBackgroundColor (0, QColor(255,0,0)); // not used by Qt
    7. child->setBackgroundColor (1, QColor(255,0,0));// not used by Qt
    8. treeWidgetItem->addChild (child);
    9. }
    10.  
    11.  
    12. void initTreewidget ( QTreeWidget* treeWidget )
    13. {
    14. treeWidget->setColumnCount (2);
    15. treeWidget->setHeaderLabels ( QStringList() << "Filename" );
    16. }
    17.  
    18.  
    19. void onUpdateTreeViewSlot ( QTreeWidgetItem * fromController )
    20. {
    21. _treeWidget->clear ();
    22. _treeWidget->insertTopLevelItem (0, fromController);
    23. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for help and looking over the code, Qt4 really is annoying when it comes to trees.

    Btw: I can even crash Qt when I collapse my invisible list and scroll to the very bottom of it (scrollbars appear when the leaf is visible)
    ASSERT failure in QVector<T>::at: "index out of range", file ../../include/QtCore/../../src/corelib/tools/qvector.h, line 211
    zsh: 16860 abort
    Last edited by Jojo; 19th February 2006 at 18:03.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Uh - that's not normal...
    Give the TreeWidgetItem a parent:
    Qt Code:
    1. QTreeWidgetItem* child = new QTreeWidgetItem (parent);
    To copy to clipboard, switch view to plain text mode 
    Parent is the QTreeWidget for top-level items or a QTreeWidgetItem for nested items.
    When you call setText you don't need to pass a QStringList in the ctor, but that should not be the problem here.
    edit: Colors work here.

    In onUpdateTreeViewSlot the fromController's parent has to be also the QTreeWidget or another item. The crash may result by Qt's memory management: The QTreeWidget takes ownership of all it's items, so you must not delete them.

    If Qt behaves strange I use debug build and run it from console. Then Qt gives in a few cases some usefull output like wrong parent.
    Last edited by Codepoet; 19th February 2006 at 18:24. Reason: Run a short test

  7. #7
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Thank you, this helped me a lot. Setting the parent properly solved all my problems.
    One question though, if I want to (partially) update the tree, I have to clear() it and reset it then, right? Sounds slow..

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Adding a new file or folder should be easy: Just search for the parent and set it accordingly when creating the new items.
    Removing entries works the same way: Search for the item and just delete it. Qt's memory management will delete all children and remove it from the tree.

    But I've just realized there's another problem: Your ID's change when one removes entries in the middle of the vector. Does this happen? Do you know the performance implications for vector? list could be better...
    You can avoid this ID problem by adding a new field to your file struct or always matching by the path string.

  9. #9
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Hello,

    I know how to update my QTreeWidgetItem tree, but I don't know how to partially update the QTreeWidget. Is simply resetting the QTreeWdigetItem-root enough?
    Btw, I am not using std::vector or std::string, they were just used for simplification, I am (of course) using QList

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    hmm - I misunderstood you...

    I am not sure what you mean by partially updating the QTreeWidget. It should repaint itself whenever you change any of its QTreeWidgetItems.

  11. #11
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    Hello,

    "it should paint itself" is exactly what I wanted to hear. This also makes it nonsense to call the update data slot very often.. excellent.

    Thanks for you help

  12. #12
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    It's terrible, tree problems do not stop..
    Now everything is added twice to the tree. If the tree has branches, they appear duplicated too and somehow have linked selection. It follows some kind of pattern it seems, but I couldn't find the logical background behind it yet.

    Here's what I'm doing. It's really nothing fancy, just some Qt tree building stuff. Maybe you find something terribly wrong with it?

    Qt Code:
    1. /// Tree generation
    2. QTreeWidgetItem* _treeRoot = new QTreeWidgetItem (QStringList() << "Head");
    3.  
    4. // Add *one* child
    5. QTreeWidgetItem* child = new QTreeWidgetItem(_treeRoot);
    6. child->setText (0, "texttext");
    7. child->setBackgroundColor (0, QColor(255,0,0));
    8. _treeRoot->addChild (child);
    9.  
    10. // Log the texts of each child. This *should* only print out one line, what it does, it prints out the following:
    11. /**
    12. FATAL (:50) - Child #0, text() == texttext
    13. FATAL (:50) - Child #1, text() == texttext
    14. */
    15. for ( int l=0; l<_treeRoot->childCount(); l++ )
    16. {
    17. LOG_FATAL (QString ("Child #%1, text() == %2").arg(l).arg(_treeRoot->child(l)->text(0)).toStdWString());
    18. }
    19.  
    20.  
    21. // Set tree to view ..
    22. emit updateTreeViewSignal(_treeRoot);
    23.  
    24.  
    25.  
    26. /// .. view code.. called by signal above
    27. void onUpdateTreeViewSlot ( QTreeWidgetItem * model )
    28. {
    29. // _treeWidget is a QTreeWidget*, created by the designer
    30. _treeWidget->insertTopLevelItem (0, model);
    31. }
    To copy to clipboard, switch view to plain text mode 


    I am again very clueless about this.
    I've tried lots of stuff today, one thing I tried was passing the QTreeWidgetItem* variable named _treeRoot from the controller to the view using a signal/slot connection. In the view I said "_treeRoot = new QTreeWidgetItem (_treeWidget)". Oddly the _treeRoot parameter came back untouched (was NULL), just like before.

    Thanks again for your kind help
    Bye,
    Jojo

  13. #13
    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: Practical QTreeView: Advice needed

    I think you are adding each item two times (lines 5 and 8 in the code above). Passing a parent to an item should make it a child of the item, and addChild should make it a child again. Try removing one of those relations.

  14. #14
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Practical QTreeView: Advice needed

    This helped me, thank you very much.
    Creating QTreeItemWidgets with another QTIW parameter does make it an implicit child, but calling addChild without QTIV ctor parameter does not make a parent-child relationship. Anyway, now it works .)

Similar Threads

  1. Logger Library : advice and help needed.
    By Antoine in forum Qt Programming
    Replies: 6
    Last Post: 6th April 2007, 09:40
  2. Qtopia H/w Advice needed
    By void* in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 19th January 2007, 12:25
  3. Multiple Windows -> Advice needed
    By vokal in forum Newbie
    Replies: 2
    Last Post: 8th January 2007, 09:40

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.