Qt&Vtk-023-MultiView

举报
DreamLife 发表于 2022/04/15 01:06:43 2022/04/15
【摘要】 ​ 今天又来搬运代码了,这周是入职的第三周了,领导给安排的任务,后面要开始搞任务了。今天搬运的代码为官方实例MuliView。 文章目录 1 官方示例展示2 代码搬运2.1 multivie...

头图

​ 今天又来搬运代码了,这周是入职的第三周了,领导给安排的任务,后面要开始搞任务了。今天搬运的代码为官方实例MuliView。

1 官方示例展示

在这里插入图片描述

​ 从运行界面来看,和我们之前搬运的CreateTree没有啥区别呀。官方的描述也很简单呀,设个描述是不是很迷呀。

image-20210630090046231

2 代码搬运

2.1 multiview.h

#ifndef MULTIVIEW_H
#define MULTIVIEW_H

#include <QWidget>
#include "QVTKOpenGLWidget.h"               //新版本,旧版QVTKWidget
#include "vtkAutoInit.h"
#include "vtkAnnotationLink.h"
#include "vtkCommand.h"
#include "vtkDataRepresentation.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"
#include "vector"


class ViewUpdater :public vtkCommand
{
public:
    static ViewUpdater* New()
    {
        return new ViewUpdater;
    }
    void AddView(vtkView *view)
    {
        this->Views.push_back(view);
        view->AddObserver(vtkCommand::SelectionChangedEvent,this);
    }

    void Execute(vtkObject*,unsigned long,void*)override
    {
        for(unsigned int i = 0;i<this->Views.size();i++)
        {
            this->Views[i]->Update();
        }
    }


private:
    ViewUpdater() = default;
    ~ViewUpdater() override = default;
    std::vector<vtkView*> Views;
};




namespace Ui {
class MultiView;
}

class MultiView : public QWidget
{
    Q_OBJECT

public:
    explicit MultiView(QWidget *parent = 0);
    ~MultiView();

private:
    Ui::MultiView *ui;

    vtkMutableDirectedGraph *graph = nullptr;

    vtkStringArray* labels = nullptr;

    vtkTree* tree = nullptr;

    vtkGraphLayoutView* view = nullptr;

    vtkGraphLayoutView* view2 = nullptr;

    vtkAnnotationLink* link = nullptr;

    ViewUpdater* update = nullptr;


};

#endif // MULTIVIEW_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
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

2.2 multiview.cpp

#include "multiview.h"
#include "ui_multiview.h"

MultiView::MultiView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MultiView)
{
    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);

    labels = vtkStringArray::New();
    labels->SetName("Label");
    labels->InsertValue(a, "a");
    labels->InsertValue(b, "b");
    labels->InsertValue(c, "c");
    labels->InsertValue(d, "d");
    labels->InsertValue(e, "e");
    labels->InsertValue(f, "f");
    graph->GetVertexData()->AddArray(labels);


    tree = vtkTree::New();
    bool validTree = tree->CheckedShallowCopy(graph);
    if (!validTree)
    {
        std::cout << "ERROR: Invalid tree" << std::endl;
        graph->Delete();
        labels->Delete();
        tree->Delete();
    }

    view = vtkGraphLayoutView::New();
    view->SetRenderWindow(ui->widget->GetRenderWindow());
    vtkDataRepresentation* rep =view->SetRepresentationFromInput(tree);
    vtkViewTheme* theme = vtkViewTheme::CreateMellowTheme();
    view->ApplyViewTheme(theme);
    view->SetVertexColorArrayName("VertexDegree");
    view->SetColorVertices(true);
    view->SetVertexLabelArrayName("Label");
    view->SetVertexLabelVisibility(true);

    view2 = vtkGraphLayoutView::New();
    vtkDataRepresentation* rep2 =view2->SetRepresentationFromInput(tree);
    view2->SetVertexLabelArrayName("Label");
    view2->SetVertexLabelVisibility(true);

    link = vtkAnnotationLink::New();
    rep->SetAnnotationLink(link);
    rep2->SetAnnotationLink(link);

    update = ViewUpdater::New();
    update->AddView(view);
    update->AddView(view2);

}

MultiView::~MultiView()
{
    delete ui;
}


  
 
  • 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

3 运行效果

image-20210630090907680

​ 这个是我的运行效果了,但是呢不知道为什么这些代码跑到Qt里面,点就成了圆点了。

★ 源码 ★

源码分享一时爽,一直分享一直爽, 链接如下:

自取:https://github.com/DreamLife-Jianwei/Qt-Vtk

在这里插入图片描述


博客签名2021

文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。

原文链接:dreamlife.blog.csdn.net/article/details/119633322

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。