QT integration with vtkImageViewer2 in QVTKOpenGLNativeWidget

Hello everyone !

I’m new to using QT and VTK and trying to get the two to work together. The actual project is very simple and it’s a test project (few lines of code) for a bigger one.
I would like to be able to display an image in a QT widget.
I managed to promote this widget to QVTKOpenGLNativeWidget to properly integrate QT.

However I can’t get the render to be in the QT widget.

I work with QT 6 and VTK 9.3

Thank you in advance for your help !

You can find the full code here: 5.4 KB folder on MEGA

The function to display is:

void MyClass::DisplayImage() {
    vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
    reader->SetFileName("Brain_1.png");

    vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputConnection(reader->GetOutputPort());

    QVTKOpenGLNativeWidget* vtkWidget = new QVTKOpenGLNativeWidget();

    this->layout()->addWidget(this->ui->vtkWidget);

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow =
        vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    vtkWidget->setRenderWindow(renderWindow);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    imageViewer->SetupInteractor(renderWindowInteractor);

    imageViewer->Render();
}

Cross copy here

This example may help ImageDataToQImage.