文章目录
Qt&Vtk-CreateTree
1 代码搬运
1.1createtree.h
1.2 createtree.cpp
2 运行效果
3 知识点
3.1 vtkGraphLayoutView
3.2 vtkViewTheme
3.3 vtkTree
3.4 vtkMutableDirectedGraph
3.5 vtkRenderView
4 其他非编译错误参考链接
★ 源码 ★
Qt&Vtk-CreateTree
今天我又来搬运代码了。今天终于不再搬运Cone了,今天我么能来搞Tree,官方实例运行起来的是下面这个样子。鼠标点击没有啥效果,先搞起来。
1 代码搬运
1.1createtree.h
#ifndef CREATETREE_H #define CREATETREE_H #include <QWidget> #include "QVTKOpenGLWidget.h" //新版本,旧版QVTKWidget #include "vtkAutoInit.h" #include "vtkDataSetAttributes.h" #include "vtkGraphLayoutView.h" #include "vtkMutableDirectedGraph.h" #include "vtkRandomGraphSource.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkStringArray.h" #include "vtkTree.h" #include "vtkViewTheme.h" namespace Ui { class CreateTree; } class CreateTree : public QWidget { Q_OBJECT public: explicit CreateTree(QWidget *parent = 0); ~CreateTree(); void startInteractor(); private: Ui::CreateTree *ui; vtkMutableDirectedGraph *graph = nullptr; vtkStringArray *lables = nullptr; vtkTree *tree = nullptr; vtkGraphLayoutView *view = nullptr; vtkViewTheme *theme = nullptr; }; #endif // CREATETREE_H
1.2 createtree.cpp
#include "createtree.h" #include "ui_createtree.h" #include <QDebug> CreateTree::CreateTree(QWidget *parent) : QWidget(parent), ui(new Ui::CreateTree) { ui->setupUi(this); graph = vtkMutableDirectedGraph::New(); vtkIdType a = graph->AddVertex(); vtkIdType b = graph->AddChild(a); vtkIdType c = graph->AddChild(a); vtkIdType d = graph->AddChild(b); vtkIdType e = graph->AddChild(c); vtkIdType f = graph->AddChild(c); lables = vtkStringArray::New(); //标签 lables->SetName("JianweiLable"); lables->InsertValue(a,"a"); lables->InsertValue(b,"b"); lables->InsertValue(c,"c"); lables->InsertValue(d,"d"); lables->InsertValue(e,"e"); lables->InsertValue(f,"f"); graph->GetVertexData()->AddArray(lables); tree = vtkTree::New(); bool validTree = tree->CheckedShallowCopy(graph); if(!validTree) { qDebug() << "Faile"; } view = vtkGraphLayoutView::New(); view->SetRepresentationFromInput(tree); theme = vtkViewTheme::CreateMellowTheme(); theme->SetLineWidth(5); //线的宽度 theme->SetCellOpacity(0.8); //cell的透明度 theme->SetCellAlphaRange(0.5,0.5); //cell的alpha的范围 theme->SetPointSize(10); //点的大小 theme->SetSelectedCellColor(1,0,1); //选中边的颜色 theme->SetSelectedPointColor(1,0,0); //选中是点的颜色 view->ApplyViewTheme(theme); view->EdgeLabelVisibilityOn(); //标签是否可见 view->SetEdgeLabelArrayName("JianweiLable"); view->SetVertexColorArrayName("VertexDegree"); view->SetColorVertices(true); view->SetVertexLabelArrayName("JianweiLable"); view->SetVertexLabelVisibility(true); view->Update(); view->ResetCamera(); view->SetRenderWindow(ui->widget->GetRenderWindow()); } CreateTree::~CreateTree() { delete ui; } void CreateTree::startInteractor() { view->GetInteractor()->Start(); }
这里有点问题就是,我的点不知道为啥是圆的呀,不是Demo中方块。
3 知识点
3.1 vtkGraphLayoutView
参考链接:https://blog.csdn.net/hit1524468/article/details/113443882
https://vtk.org/doc/nightly/html/classvtkGraphLayoutView.html
3.2 vtkViewTheme
暂无理解
3.3 vtkTree
参考链接:https://vtk.org/doc/nightly/html/classvtkTreeRingView.html