VTK9.1.0 is compiled and installed in Windows10+VS2019+Qt 5.15.2 environment and VTK is applied to QT

catalogue

Download VTK installation package

Compilation environment installation

CMake

Qt 5.15.2

Visual Studio Community Edition

Ninja

Compile directory planning

Compiling VTK libraries using Cmake

Visual Studio 2019 compiling and installing VTK

Using VTK in QT

Download VTK installation package

On VTK official website Download | VTK Download the source code of VTK9.1.0 to be compiled. After decompression, you can see the official Prerequisites and simple tutorials in the path Documentation/dev/bulid.md

Compilation environment installation

Install the following environments according to the official Prerequisites:

CMake

  • Version 3.12 or newer, however, the latest version is always recommended

Qt 5.15.2

Visual Studio Community Edition

  • Visual Studio 2019

Ninja

  • Unzip the binary and put it in PATH. Note that newerVisual Studio releases come with a version of ninja already and shouldalready exist in PATH within the command prompt.

Compile directory planning

Set up four folders according to the official recommendation:

  1. VTK-9.1.0.rc2-src

Store the source code to be compiled downloaded by VTK

  1. VTK-9.1.0.rc2-build

Store the binary file generated when compiling VTK with Cmake

  1. VTK-9.1.0.rc2-install-debug

Store the Debug version of the lib file generated using Visual Studio

  1. VTK-9.1.0.rc2-install-release

Store the Release version of lib files generated using Visual Studio

Compiling VTK libraries using Cmake

  • Open the installed cmake GUI, select the VTK source code path VTK-9.1.0.rc2-src and binary storage path VTK-9.1.0.rc2-build, click Configure to start, select Visual Studio 16 2019 to compile in the pop-up window, and then click Finish

  • Some red options will appear after configuring. The red options need to be solved below

  • Including CMAKE_INSTALL_PREFIX is the location where the VTK library is to be installed. The location of the library file generated after VTK compilation is modified to VTK-9.1.0.rc2-install-debug. In subsequent generation, the VTK Library of Release version is generated. After generation, the generated file is moved to VTK-9.1.0.rc2-install-release, and then the VTK Library of Debug version is generated, At this time, the generated files are stored in the VTK-9.1.0.rc2-install-debug folder

  • Search QT in the search window, set all options to WANT, and then click Configure

  • After Configure, a new red area will appear. Here, let's choose the location of Qt5, basically Qt_DIR/5.15.2/msvc2019_64/lib/cmake/Qt *, check by the name on the left, VTK_QT_VERSION is 5

  • After Configuring done, if there is only a white interface, it means there is no error. If your interface is still red, go back and check which of the above steps is not checked until there is only a white interface, and then click Generate
  • After Generate done, if there is only a white interface, it means there is no error, and then click Open Project to open Visual Studio 2019

Visual Studio 2019 compiling and installing VTK

  • After Cmake is completed, click Open Project to open Visual Studio 2019, select generate - > batch generate, and select all in the interface that appears_ For the Release version of build, click generate and wait for the generation to complete. It takes about 40 minutes

  • Put ALL_BUILD   The √ after Release is removed and installed   Check after Release and click generate

  • After the generation is completed, the Release version of VTK will be installed. At this time, cmake set in the front will be installed_ INSTALL_ In prefix, i.e. VTK-9.1.0.rc2-install-debug, then you need to cut the file into the VTK-9.1.0.rc2-install-release folder. At this time, the VTK Release version is generated, and the generated library files are as follows:

  • In generate - > batch generate, cancel the √ after the just checked INSTALL Release. Then you can directly check all together_ BUILD Debug x64   And INSTALL Debug. After the generation, the library file will be generated in the VTK-9.1.0.rc2-install-debug folder. The compilation phase is complete. The generated Debug version library is followed by d, which is different from the Release version.

Using VTK in QT

I wrote a demo project using VTK in QT. The link is GitHub - isongxw/Qt-VTK-Demo , you can download it directly. Here are the specific steps

  • Add the generated vtk-9.1.0. RC2 install debug \ bin and vtk-9.1.0. RC2 install release \ bin to the environment variable and restart
  • Create QT project in Qt Creator and add include + = "D: \ VTK \ vtk-9.1.0. RC2 install debug \ include \ vtk-9.1" and LIBS in * pro file. LIBS needs to add many lib files. Use the following code to generate LIBS instructions
import os

def main():
    BASEDIR = "D:\VTK\VTK-9.1.0.rc2-install-release\lib"

    fn = os.listdir(BASEDIR)
    with open("lib_r.txt", 'w') as fp:
        for i in fn:
            fp.write("LIBS += {}\{}\n".format(BASEDIR, i))
    print("hello")


if __name__ == "__main__":
    main()
  • Add OpenGL Widget in the interface, right-click to promote to qvtkopenglnaturewidget, and the interface is as follows

  • Add the sample code and draw a sphere using VTK. The mainwindows.cpp code is as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSurfaceFormat>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkSphereSource> sphereSource;
    vtkNew<vtkPolyDataMapper> sphereMapper;
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    vtkNew<vtkActor> sphereActor;
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(sphereActor);
    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("RenderWindowNoUIFile");
    setCentralWidget(ui->openGLWidget);
    ui->openGLWidget->setRenderWindow(renderWindow);
}

MainWindow::~MainWindow()
{
    delete ui;
}
  • Click Run to display VTK interface in Qt window

Tags: C++ Qt vtk

Posted on Thu, 14 Oct 2021 14:29:11 -0400 by vchris