Qt&Vtk-012-CreateTree

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
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
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();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
2 运行效果

这里有点问题就是,我的点不知道为啥是圆的呀,不是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

3.4 vtkMutableDirectedGraph
参考链接:https://vtk.org/doc/nightly/html/classvtkMutableDirectedGraph.html


3.5 vtkRenderView
参考链接:https://vtk.org/doc/nightly/html/classvtkRenderView.html


4 其他非编译错误参考链接
vtk非编译错误:https://blog.csdn.net/cfqcfqcfqcfqcfq/article/details/51777193
★ 源码 ★

这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:


文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。
原文链接:dreamlife.blog.csdn.net/article/details/119573452
- 点赞
- 收藏
- 关注作者
评论(0)