使用Vtk - Qt Creator - VS 编译简单项目

VTK与Qt集成开发指南

前言:

关于如何集成Vtk可见上一篇:

Windows:VTK - Qt Creator - Visual studio - CMAKE编译可执行库

一、编译器配置

Qt Creator6.10.0
Visual studio 202217.x
VTK9.3.0

二、创建项目

我这里是使用Visual Studio 2022创建Qt Widgets Application,如果你手上的Visual Studio不能创建Qt Widgets项目,可以直接用Qt Creator创建项目,或者用Visual Studio创建,也是一样的。

想要Visual Studio创建Qt Widgets项目,可跳转:

Visual Studio 2022 - Qt 6.0.0+ 使用前准备 环境配置

1、使用Visual Studio 2022创建 Qt Widgets Application

2、输入名称、项目存放位置信息,点击创建

3、在弹出Qt Widgets Application Wizard 窗口中,选择Next

4、重点:选择Qt Modules

无论是Debug还是Release,一定要选择:Qt OpenGLQt OpenGL Widget

注1:无Qt OpenGL、Qt OpenGL Widgets

通常是在安装Qt时,未勾选Qt OpenGL相关的组件。可以通过运行自带的Qt MaintenanceTool.exe工具,选择更新Qt 组件

注2:选了其中一个Modules,遗漏了另外一个

右键项目 → Qt Project SettingsQt Modules

5、项目创建完成,运行项目

此时运行,应该是一个空的窗口,如下图

三、为项目配置必要的参数

目标:让一段最小代码正常运行。

1、在VS项目中添加VTK的包含目录

操作:项目 -> 右键属性 -> C++/C -> 常规 -> 附加目录(见下图)

添加路径:(记得改成你自己的路径,或者用相对路径)

1、E:\DevelopTools\VTK_9_3_0\VTK_Install\include\vtk-9.3

2、E:\DevelopTools\Qt\6.10.0\msvc2022_64\include\QtOpenGLWidgets

2、添加VTK库目录和VTK的lib文件

操作1:项目 -> 右键属性 -> 链接器 -> 常规 -> 附加目录(见下图)

添加路径:(记得改成你自己的路径,或者用相对路径)

E:\DevelopTools\VTK_9_3_0\VTK_Install\lib

操作2:项目 -> 右键属性 -> 链接器 -> 输入 -> 附加依赖项(见下图)

注:添加lib取巧方法

我嫌弃一个一个添加麻烦,所以我是直接把lib文件目录下lib全部放到附加依赖项中

将所有lib文件名生成txt文件,然后将名称直接粘贴过去,如上图所示。

具体操作如下:

1、打开 x64 Native Tools Command Prompt;

2、cd 至Vtk lib文件夹 cd /d E:\DevelopTools\VTK_9_3_0\VTK_Install\lib;(用自己的)

3、输入:Dir *.lib /b > libs.txt

4、等待运行完成,然后在lib文件夹里面就有一个libs.txt文件,把里面的东西全部拷贝进去(见上图)

四、编写一段最小可运行代码

1、.h文件

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_VTKPlatform.h"
#include <QtOpenGLWidgets/QOpenGLWidget>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>

class VTKPlatform : public QMainWindow
{
    Q_OBJECT

public:
    VTKPlatform(QWidget *parent = nullptr);
    ~VTKPlatform();

private:
    Ui::VTKPlatformClass ui;

    QVTKOpenGLNativeWidget* m_pVtkWidget = nullptr;

private:
    //VTK控件
    void setVTKControls();
};

2、.cpp文件

#include "VTKPlatform.h"


VTKPlatform::VTKPlatform(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    //创建 Qt 的 OpenGL 窗口部件,专门用于嵌入 VTK 渲染结果
    m_pVtkWidget = new QVTKOpenGLNativeWidget();
    //让该 VTK 部件占满主窗口中央区域(QMainWindow 的特色布局)
    this->setCentralWidget(m_pVtkWidget);

    setVTKControls();
}

VTKPlatform::~VTKPlatform()
{}

void VTKPlatform::setVTKControls() {
    //1、generate data
    auto m_cone = vtkSmartPointer<vtkConeSource>::New();
    //2、mapper
    auto m_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    //3、actor
    auto m_actor = vtkSmartPointer<vtkActor>::New();
    m_actor->GetProperty()->SetColor(1.0, 1.0, 0.4);

    vtkNew<vtkProperty> backProp;
    backProp->SetColor(1.0, 0.2, 0.2);   // 背面想用的颜色
    m_actor->SetBackfaceProperty(backProp);
    //m_actor->GetProperty()->SetBackfaceColor(1.0, 0.2, 0.2);

    //4、renderer
    auto m_render = vtkSmartPointer<vtkRenderer>::New();
    m_render->SetBackground(0.1, 0.2, 0.3);
    // filter 过滤器
    auto m_shrink = vtkSmartPointer<vtkShrinkPolyData>::New();
    auto m_stlReader = vtkSTLReader::New();
    m_stlReader->SetFileName("resource/stl/Dragao_Esqueleto.stl");

    //5、connect them
    m_shrink->SetInputConnection(m_stlReader->GetOutputPort());
    m_mapper->SetInputConnection(m_stlReader->GetOutputPort());
    //m_mapper->SetInputConnection(m_shrink->GetOutputPort());
    m_actor->SetMapper(m_mapper);
    m_render->AddActor(m_actor);

    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
    m_pVtkWidget->setRenderWindow(renderWindow);
    renderWindow->AddRenderer(m_render);
}

3、运行结果

我采用渲染STL文件,如果不想加载STL,就加载简单的图形,修改一句代码即可:

//m_shrink->SetInputConnection(m_stlReader->GetOutputPort());
m_mapper->SetInputConnection(m_cone->GetOutputPort());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值