After reading http://doc.trolltech.com/4.3/model-view-view.html and http://doc.trolltech.com/4.3/model-view-delegate.html over and over again I still wonder how I am supposed to create my custom view. What I want is to take a tree-structure (I already have the model done) and show nodes in different ways.
I already did so by starting from QTreeView and reimplementing paintEvent() and going forward from there, just like QTreeView does with drawTree() until I realized I could use readily available widgets to display nodes instead. Say I want to use tabs to display nodes and QTextEdits (or any other widget) when editing information in them.
As an example, say I have a tree with a Root and it has 2 children, 1 QTextEdit and 1 QTabWidget. The QTabWidget has 2 children, both QTextEdits. The code for this is:
//Root
qvboxlayoutPage->setSpacing( 2 );
qvboxlayoutPage->setMargin( 1 );
//Create and add children
qvboxlayoutPage->addWidget(qtextEdit);
qvboxlayoutPage->addWidget(qtabwidget2);
//Child1
qvboxlayoutPage2->setSpacing( 2 );
qvboxlayoutPage2->setMargin( 1 );
qvboxlayoutPage2->addWidget(qtextEdit2);
//Child2
qvboxlayoutPage2b->setSpacing( 2 );
qvboxlayoutPage2b->setMargin( 1 );
qvboxlayoutPage2b->addWidget(qtextEdit3);
qtabwidget->addTab( qwidgetPage, "Root");
qtabwidget2->addTab( qwidgetPage2, "Node1");
qtabwidget2->addTab( qwidgetPage2b, "Node2");
QWidget parent; //somehow defined
//Root
QTabWidget *qtabwidget = new QTabWidget(parent);
QWidget *qwidgetPage = new QWidget(qtabwidget);
QVBoxLayout *qvboxlayoutPage = new QVBoxLayout(qwidgetPage);
qvboxlayoutPage->setSpacing( 2 );
qvboxlayoutPage->setMargin( 1 );
//Create and add children
QTextEdit *qtextEdit = new QTextEdit(qwidgetPage);
QTabWidget *qtabwidget2 = new QTabWidget(qwidgetPage);
qvboxlayoutPage->addWidget(qtextEdit);
qvboxlayoutPage->addWidget(qtabwidget2);
//Child1
QWidget *qwidgetPage2 = new QWidget(qtabwidget);
QVBoxLayout *qvboxlayoutPage2 = new QVBoxLayout(qwidgetPage2);
qvboxlayoutPage2->setSpacing( 2 );
qvboxlayoutPage2->setMargin( 1 );
QTextEdit *qtextEdit2 = new QTextEdit(qwidgetPage2);
qvboxlayoutPage2->addWidget(qtextEdit2);
//Child2
QWidget *qwidgetPage2b = new QWidget(qwidgetPage2);
QVBoxLayout *qvboxlayoutPage2b = new QVBoxLayout(qwidgetPage2b);
qvboxlayoutPage2b->setSpacing( 2 );
qvboxlayoutPage2b->setMargin( 1 );
QTextEdit *qtextEdit3 = new QTextEdit(qwidgetPage2);
qvboxlayoutPage2b->addWidget(qtextEdit3);
qtabwidget->addTab( qwidgetPage, "Root");
qtabwidget2->addTab( qwidgetPage2, "Node1");
qtabwidget2->addTab( qwidgetPage2b, "Node2");
To copy to clipboard, switch view to plain text mode
Where am I supposed to do this? I can't do it in the paintEvent() for sure.
I just cannot find the function from where I can start creating these widgets to create my own view. The tree is defined in the model so the function I'm looking for is loaded after setModel has been called.
Could it possibly be that I should use QItemDelegate for this - I still have problems understanding how and when they are supposed to be used. If so, how? Any guides available online?
Bookmarks