Prerequisites
Before starting, ensure you have:
- A compiled VTK library (built with CMake from VTK-9.3.0 or later)
- Qt 6.x installed on your system
- Qt Visual Studio Tools extension installed in Visual Studio 2022
Creating a Qt Widgets Application
Open Visual Studio 2022 and create a new project. Select Qt Widgets Application from the Qt project templates.
After naming your project, configure the Qt settings:
- Select Debug build configuration
- Ensure Qt OpenGLWidgets module is checked—this provides the OpenGL widget container for VTK rendering
Complete the wizard to generate the project structure.
Configuring VTK Library Paths
Right-click the project in Solution Explorer and open Properties.
Navigate to Configuration Properties → VC++ Directories and add the following paths:
Include Directories:
C:\VTK\VTK9.3.0\include\vtk-9.3
Library Directories:
C:\VTK\VTK9.3.0\lib
Aply the changes.
Linking VTK Static Libraries
Under Linker → Input → Additional Dependencies, add the VTK library files. For convenience, use a wildcard pattern:
C:\VTK\VTK9.3.0\lib\*.lib
Alternatively, add specific libraries as needed.
Implementing the VTK Widget
In your header file, declare the VTK widget pointer:
#include <QVTKOpenGLNativeWidget.h>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
QVTKOpenGLNativeWidget* vtkWidget = nullptr;
};
In your implementation file, initialize the widget and set it as the central widget:
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
vtkWidget = new QVTKOpenGLNativeWidget(this);
setCentralWidget(vtkWidget);
}
MainWindow::~MainWindow() {
delete vtkWidget;
}
Distributing VTK DLLs
Build the project too generate the executable. After a successful build, locate the output in the x64\Debug folder.
Copy all DLL files from the VTK bin\Debug directory to your project's x64\Debug folder. These runtime dependencies are required for execution.
Running the Application
Execute the project. The VTK widget should render correctly with in the Qt window, confirming successful integration.