[vs+qt] Use the VTK library to read the STL model and display the STL model on the QT interface

Article directory

    • summary
    • Show results
    • Implementation steps
    • Code summary
    • summary

Summary

To use the complete interface, click the button to select the file, read the stl three-dimensional model, and display the model on the qvtkWidget control.

Effect display

1.Select the file address
![](https://img-blog.csdnimg.cn/54001afa109f48c387ad5e47bf042af8.png

2. Interface window display

Implementation steps

1. Click the Open File button, select the file path, and make a judgment. If the file is read normally, continue to perform the VTK visualization operation. If the read file content is empty, an error window will pop up.
Note: I won’t go into detail here about setting buttons, connecting slot functions, etc. They are relatively common functions.

stl_fileName = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("Please select the file path..."), "*.stl");
    if (stl_fileName.isEmpty())
    {<!-- -->
        QMessageBox::warning(this, "Warning!", "Failed to open the file!");
    }
    else
    {<!-- -->
     //Read operation.......
    }

2. Using VTK’s reading operation, the code analysis is as follows
The required vtk library functions are as follows:

#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>

The first step is to create an STL reader

//Create STL reader
    vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
    reader->SetFileName(stl_fileName.toStdString().c_str());
    reader->Update();

One thing to note here is that when importing stl_fileName, a step is performed: toStdString().c_str(). If you are not operating on the qt interface, you can write stl_fileName directly. On the qt interface, what we define is

QString stl_fileName

Therefore, you need to convert the string when reading, otherwise you will get this error: The operator of this type is not defined.

Step two, create VTK mapping and actors

 vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
 mapper->SetInputData(reader->GetOutput());

 vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
 actor->SetMapper(mapper);

This is the main idea of VTK. You can imagine it as a stage performer wearing performance clothes. Mapper: Convert different data types into graphic data. Actor: The object that executes the rendering mapper.

The third step is to create a VTK renderer

//Create VTK renderer
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    ui.qvtkWidget->GetRenderWindow()->AddRenderer(renderer);//This step is the difference in using qt
 //Add actor to renderer
    renderer->AddActor(actor);

The meaning here is to take over the previous step. The renderer, as the name suggests, is used to render images. Adding actors to the renderer is equivalent to stage performers standing on the stage.

Need to pay attention to this step:

ui.qvtkWidget->GetRenderWindow()->AddRenderer(renderer);

Get the rendering window of the qvtkwidget control from the UI interface and add the renderer to it. The rendering window can be understood as a theater with a stage and actors inside. It can display graphics very well.

Note:
The qvtkwidget control has been added to the UI interface. If you configure the VTK library correctly, you can normally add it like this

Note: the qvtkwidget control in the lower left corner.

Finally, initialize the rendering window

 //Set renderer background color and other attributes
    renderer->SetBackground(0.1, 0.1, 0.1);

    // Rendering window initialization
    ui.qvtkWidget->GetRenderWindow()->Render();

Code summary

#include <vtkSmartPointer.h>
#include <vtkSTLReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>

QString stl_fileName;

   stl_fileName = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("Please select the file path..."), "*.stl");
    if (stl_fileName.isEmpty())
    {
        QMessageBox::warning(this, "Warning!", "Failed to open the file!");
    }
    else
    {
    // Create STL reader
    vtkSmartPointer reader = vtkSmartPointer::New();
    reader->SetFileName(stl_fileName.toStdString().c_str());
    reader->Update();

    // Create VTK map and actor
    vtkSmartPointer mapper = vtkSmartPointer::New();
    mapper->SetInputData(reader->GetOutput());

    vtkSmartPointer actor = vtkSmartPointer::New();
    actor->SetMapper(mapper);

    //Create VTK renderer
    vtkSmartPointer renderer = vtkSmartPointer::New();
    ui.qvtkWidget->GetRenderWindow()->AddRenderer(renderer);

    //Add actor to renderer
    renderer->AddActor(actor);

    //Set renderer background color and other properties
    renderer->SetBackground(0.1, 0.1, 0.1);

    // Rendering window initialization
    ui.qvtkWidget->GetRenderWindow()->Render();

    }

Add these codes to the correct locations and you’re ready to go. Since my project is divided into different functional categories on the main interface and sub-interfaces, and there are other functions, I have pasted the code for the vtk display part, and you can add it as needed.

Summary

1. If a vtk warning appears, the control will be removed when it expires:

Generic Warning: In E:\GitHub\VTK\GUISupport\Qt\QVTKWidget.cxx, line 83
QVTKWidget was deprecated for VTK 8.1 and will be removed in a future version.

Generic Warning: In E:\GitHub\VTK\GUISupport\Qt\QVTKPaintEngine.cxx, line 25
QVTKPaintEngine was deprecated for VTK 8.1 and will be removed in a future version

You can add it in the main interface

#include <vtkOutputWindow.h>
...
...
...
   vtkOutputWindow::SetGlobalWarningDisplay(0);//Shield warnings
...
...

2. If the vtk interface display is stuck or garbled characters are repeated, it may be related to the control interface display not being initialized.
Can be added

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)

Perform vtk initialization.

Record the project experience to help you understand and learn. Everyone is welcome to communicate and discuss. After all, the project functions are different and the writing methods are different. For reference only!