Results 1 to 2 of 2

Thread: TreeWidget

  1. #1
    Join Date
    Jan 2011
    Location
    Paris
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question TreeWidget

    Hi all,

    I am trying to learn MVC within the Qt framework. So, to test, I am developping a small toy application. The idea is to display an OpenSceneGraph scene graph using a QTreeView. Thus, I have a good starting point using the "Editable Tree Model Example". However, after doing a few changes, it appears that my QTreeView is empty. I do not understand why, but I am nearly sure that I am missing the way to insert the root item in the model. Could you please help me?

    Basically the item and model are copy / paste of the example. I juste removed the setupModel method from the model and so, do not call it anymore from the constructor.

    Regards,

    The visitor for all the nodes contained in the scene graph:
    Qt Code:
    1. #ifndef __OSG_GRAPH_INFO_VISITOR_HPP__
    2. #define __OSG_GRAPH_INFO_VISITOR_HPP__
    3.  
    4. #include <osg/NodeVisitor>
    5. #include <osg/Geode>
    6. #include <osg/Drawable>
    7.  
    8. #include <string>
    9.  
    10. class QTreeView;
    11. class q_osg_tree_item;
    12.  
    13. class osg_graph_info_visitor : public osg::NodeVisitor
    14. {
    15. public:
    16. osg_graph_info_visitor(QTreeView* view);
    17.  
    18. std::string spaces() { return std::string(_level*2, ' '); }
    19.  
    20. virtual void apply( osg::Node& node );
    21. virtual void apply( osg::Geode& geode );
    22.  
    23. protected:
    24. QTreeView* _view;
    25. unsigned int _level;
    26. q_osg_tree_item* _current_parent;
    27. };
    28.  
    29. #endif // __OSG_GRAPH_INFO_VISITOR_HPP__
    To copy to clipboard, switch view to plain text mode 

    Its implementation:
    Qt Code:
    1. #include "osg_graph_info_visitor.hpp"
    2.  
    3. #include <osg/PagedLOD>
    4. #include <osg/Drawable>
    5.  
    6. #include <QAbstractItemModel>
    7. #include <QTreeView>
    8. #include <QModelIndex>
    9.  
    10. #include <iostream>
    11.  
    12. #include "q_osg_tree_item.hpp"
    13. #include "q_osg_tree_model.hpp"
    14.  
    15. using namespace osg;
    16. using namespace std;
    17.  
    18. Q_DECLARE_METATYPE(std::string);
    19.  
    20. osg_graph_info_visitor::osg_graph_info_visitor(QTreeView* view) :
    21. _view(view),
    22. _level(0),
    23. _current_parent(NULL)
    24. {
    25. _current_parent = ((q_osg_tree_model*)(_view->model()))->root();
    26. setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
    27. }
    28.  
    29. void osg_graph_info_visitor::apply( Node& node )
    30. {
    31. cout << spaces() << "[Node - level " << _level << "] " << node.className() << " #parents --> " << node.getNumParents() << endl;
    32.  
    33. QVector<QVariant> data;
    34. data.push_back( QVariant::fromValue(string(node.className())) );
    35.  
    36. _current_parent->insertChildren(_current_parent->childCount(), 1, 1 /*TODO*/);
    37. for (int column = 0; column < data.size(); ++column)
    38. {
    39. cout << "ljifs" << endl;
    40. _current_parent->child(_current_parent->childCount() - 1)->setData(column, data[column]);
    41. }
    42.  
    43. _level++;
    44. traverse( node );
    45. _level--;
    46. }
    47.  
    48. void osg_graph_info_visitor::apply( Geode& geode )
    49. {
    50. cout << spaces() << "[Geode - level " << _level << "] " << geode.className() << endl;
    51.  
    52. QVector<QVariant> data;
    53. data.push_back( QVariant::fromValue(string(geode.className())) );
    54.  
    55. _current_parent->insertChildren(_current_parent->childCount(), 1, 1 /*TODO*/);
    56. for (int column = 0; column < data.size(); ++column)
    57. {
    58. cout << "ljifsqmslksdmlks" << endl;
    59. _current_parent->child(_current_parent->childCount() - 1)->setData(column, data[column]);
    60. }
    61.  
    62. _level++;
    63. traverse( geode );
    64. _level--;
    65. }
    To copy to clipboard, switch view to plain text mode 

    And this is from my GUI that I create the model and attache it to the view, and then call the visitor:
    Qt Code:
    1. q_osg_treeview::q_osg_treeview(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::qosgtreeview)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QStringList headers;
    8. headers << "Nodes";
    9. _model = new q_osg_tree_model(headers, "");
    10.  
    11. _view = new QTreeView;
    12. _view->setModel(_model);
    13.  
    14. ui->verticalLayout->addWidget(_view);
    15. }
    16.  
    17. void q_osg_treeview::toolbutton_input_fileSelected(const QString& file)
    18. {
    19. QDir current_dir;
    20. QSettings application_settings;
    21. const QString DEFAULT_INPUT_DIR_KEY("default_input_dir");
    22. application_settings.setValue(DEFAULT_INPUT_DIR_KEY, current_dir.absoluteFilePath(file));
    23.  
    24. ref_ptr<Node> input = osgDB::readNodeFile( file.toStdString() );
    25.  
    26. osg_graph_info_visitor* v= new osg_graph_info_visitor(_view);
    27. input->accept(*v);
    28.  
    29. cout << ((q_osg_tree_model*)_view->model())->root()->childCount() << endl;
    30. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TreeWidget

    Well, the interesting thing would be the model, after all this is the interface to the data.

    Btw, a tree model is the most complicated to implement. Maybe start with a QAbstractListModel as the base class first (just rowCount() and data() to implement), then change to a QAbstractTableModel (additionally requires columnCount()) and only get into a full tree model (by deriving from QAbstractItemModel, requires to also implement index(), parent(), etc) once you've got the two easier ones to work.

    Cheers,
    _

Similar Threads

  1. add items to treewidget
    By Shien in forum Qt Programming
    Replies: 15
    Last Post: 1st January 2011, 19:40
  2. Replies: 2
    Last Post: 18th May 2010, 22:44
  3. How to add subitem in treewidget
    By phillip_Qt in forum Qt Programming
    Replies: 11
    Last Post: 16th November 2009, 11:22
  4. Problem with Treewidget
    By addu in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 09:24
  5. FTP recursive treeWidget
    By Aji Enrico in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2008, 11:26

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.