【基础篇】Qt+VTK开发环境搭建

前言

VTK网站有大量的程序示例,可以通过手敲代码的形式进行学习。下面就开始使用Qt来完成所有示例。

环境搭建

1)准备VTK库、QtCreator开发环境、Qt库
VTK使用8.2版本(64),Qt使用5.14.0(mingw73_64)
2)添加库到系统环境变量
VTK路径:C:\VTK\bin Qt路径:C:\Qt\Qt5.14.0\5.14.0\mingw73_64\bin
3)创建项目
打开Vtk示例,网站地址:
https://examples.vtk.org/site/Cxx/
参考Hello world示例,
在这里插入图片描述
将该项目Cmakelist文件中的库文件名复制出来。同时创建一个Qt项目,将项目依赖的头文件路径和库文件添加到Qt工程文件中。
在这里插入图片描述
在这里插入图片描述

项目编译

1)编译问题
在这里插入图片描述
这里报错可以看出是库的引用出了问题,追踪到该函数所在类,
在这里插入图片描述
返回值出现问题,再追踪到返回值类型的类的头文件中
在这里插入图片描述
VTKCOMMONEXECUTIONMODEL_EXPORT可以看出依赖的是vtkcommonexecutionmodel相关的库,此时只需要去VTK库的lib目录下找
在这里插入图片描述
将该库名添加到工程的pro文件中。再次编译就没有问题了。

示例源码

1)工程文件

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

INCLUDEPATH += C:\VTK\include\vtk-8.2

LIBS += -L"C:\VTK\lib" \
        -lvtkFiltersSources-8.2 \
        -lvtkCommonColor-8.2 \
        -lvtkCommonCore-8.2 \
        -lvtkCommonExecutionModel-8.2 \
        -lvtkFiltersSources-8.2 \
        -lvtkInteractionStyle-8.2 \
        -lvtkRenderingContextOpenGL2-8.2 \
        -lvtkRenderingCore-8.2 \
        -lvtkRenderingFreeType-8.2 \
        -lvtkRenderingGL2PSOpenGL2-8.2 \
        -lvtkRenderingOpenGL2-8.2 \
        -lvtkGUISupportQt-8.2

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

2)主要代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <array>

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkGenericOpenGLRenderWindow.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    vtkNew<vtkNamedColors> colors;

    // Set the background color.
    std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
    colors->SetColor("BkgColor", bkg.data());

    // This creates a polygonal cylinder model with eight circumferential facets
    // (i.e, in practice an octagonal prism).
    vtkNew<vtkCylinderSource> cylinder;
    cylinder->SetResolution(8);

    // The mapper is responsible for pushing the geometry into the graphics
    // library. It may also do color mapping, if scalars or other attributes are
    // defined.
    vtkNew<vtkPolyDataMapper> cylinderMapper;
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

    // The actor is a grouping mechanism: besides the geometry (mapper), it
    // also has a property, transformation matrix, and/or texture map.
    // Here we set its color and rotate it around the X and Y axes.
    vtkNew<vtkActor> cylinderActor;
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->GetProperty()->SetColor(
    colors->GetColor4d("Tomato").GetData());
    cylinderActor->RotateX(30.0);
    cylinderActor->RotateY(-45.0);

    // The renderer generates the image
    // which is then displayed on the render window.
    // It can be thought of as a scene to which the actor is added
    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(cylinderActor);
    renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
    // Zoom in a little by accessing the camera and invoking its "Zoom" method.
    renderer->ResetCamera();
    renderer->GetActiveCamera()->Zoom(1.5);

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    window->AddRenderer(renderer);


    ui->scene->SetRenderWindow(window);
    ui->scene->GetRenderWindow()->Render();
    ui->scene->GetRenderWindow()->Start();

}

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


#include "mainwindow.h"

#include <QApplication>

#include<vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

3)运行效果
在这里插入图片描述

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: QT是一个跨平台的C++图形界面应用程序框架,VTK是一个用于可视化和图形处理的开源库,而PRO脚本则是为了编写QTVTK程序而设计的脚本语言。 在使用QTVTK开发可视化应用程序时,PRO脚本可以帮助我们简化代码编写和操作。 在编写QT VTK程序时,PRO脚本可以用来管理程序的构建过程。我们可以编写一个.pro文件,其中包含编译、链接和运行程序所需的信息和指令。PRO脚本可以指定程序所依赖的库、头文件的路径和源文件的位置等等。通过简单地修改.pro文件,我们就可以非常方便地修改程序的编译和链接选项。 此外,PRO脚本还可以用来配置QTVTK程序的构建环境。我们可以通过指定不同的编译器、库和路径来定制程序的运行环境。PRO脚本可以帮助我们在不同的平台上使用相同的代码进行构建。 在QTVTK程序中,使用PRO脚本可以提高代码的可维护性和可重用性。我们可以将一些公共的编译和链接选项抽象为变量,在不同的项目中进行复用。此外,PRO脚本还可以定义自定义的构建目标,以及一些自定义的构建规则。 总之,QT VTK PRO脚本是用于编写QTVTK程序时的一种工具。它可以帮助我们管理程序的构建过程和配置环境,提高代码的可维护性和可重用性,使我们能够更方便地开发和调试可视化应用程序。 ### 回答2: Qt VTK Pro脚本是用于在Qt框架中集成VTK(Visualization Toolkit)库的一种脚本。VTK是一个用于三维可视化、图像处理和计算几何方面的开源工具包,提供了丰富的功能和算法用于处理和呈现三维数据。 Qt VTK Pro脚本主要用于简化在Qt应用程序中使用VTK库的过程。它提供了一种方便的方法来创建和管理VTK对象、渲染场景、添加交互功能等。使用该脚本,开发人员可以更轻松地将VTK功能集成到他们的Qt应用程序中。 通过Qt VTK Pro脚本,开发人员可以使用VTK库创建各种类型的3D图形,包括几何体、图像数据、曲线、体积数据等。它还提供了各种渲染技术,如体积渲染、剖面渲染、表面渲染等。 此外,Qt VTK Pro脚本还支持用户交互,允许用户与创建的3D图形进行交互。这可以通过添加鼠标事件、键盘事件等来实现。用户可以旋转、平移、缩放图形,选择对象、改变物体的属性等。 总之,Qt VTK Pro脚本为开发人员提供了一种快速、简便的方式来在他们的Qt应用程序中集成VTK库,从而使他们能够利用VTK的强大功能来处理和呈现三维数据。它使得开发3D可视化应用程序变得更加高效和灵活。 ### 回答3: Qt VTK Pro 脚本是一个用于与QtVTK和Pro软件集成的脚本。 Qt是一个跨平台的C++图形用户界面开发框架,它提供了丰富的GUI组件和工具,使开发人员能够方便地创建功能强大的应用程序。 VTK(Visualization Toolkit)是一个用于可视化和图形处理的开源软件库。它提供了许多可用于创建复杂可视化效果的算法和工具。 Pro是一种为软件项目管理而设计的构建工具。它提供了一种简单且强大的方式来构建、部署和管理软件项目。 Qt VTK Pro脚本的目的是将QtVTK和Pro集成在一起,以便更方便地开发、构建和管理应用程序。 通过使用Qt VTK Pro脚本,开发人员可以使用Qt的GUI组件来构建应用程序的用户界面。他们还可以使用VTK的算法和工具来创建复杂的可视化效果。同时,Pro工具使开发人员能够更轻松地构建、部署和管理他们的应用程序。 Qt VTK Pro脚本的使用可以在许多领域发挥作用。例如,在科学可视化中,它可以用于创建漂亮的3D可视化效果。在工程领域,它可以用于可视化和分析复杂的CAD数据。在医学图像处理中,它可以用于创建精确的医学图像可视化。 总之,Qt VTK Pro 脚本提供了一个强大的工具集,使开发人员能够更方便地将QtVTK和Pro集成在一起,从而创建出功能丰富、具有复杂可视化效果的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值