Qt&Vtk-012-CreateTree
Posted DreamLife.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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();
}
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
★ 源码 ★
这里就要有人问了呀,这么优秀的代码,能不能分享下呀,当然可以呀,我不生产代码,我只是代码的搬运工,链接如下:
以上是关于Qt&Vtk-012-CreateTree的主要内容,如果未能解决你的问题,请参考以下文章