VTK widgets don't seem to work inside QVTKOpenGLNativeWidget (in Qt)

Hi,

I’m trying to launch VTK widget example in Qt. So I’ve rewriten the code to use it in Qt and I now have something like:

    MyMainWindow::MyMainWindow(){
        // A renderer and render window
        vtkSmartPointer<vtkRenderer> renderer =
          vtkSmartPointer<vtkRenderer>::New();
        vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow =
          vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
        renderWindow->AddRenderer(renderer);

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

        double pos1[3] = {50.0, 200.0, 0.0};
        double pos2[3] = {200.0, 20.0, 0.0};
        double center[3] = {100.0, 100.0, 0.0};
        vtkSmartPointer<vtkAngleRepresentation2D> rep =
          vtkSmartPointer<vtkAngleRepresentation2D>::New();
        rep->ArcVisibilityOff();

        vtkSmartPointer<vtkAngleWidget> angleWidget =
          vtkSmartPointer<vtkAngleWidget>::New();
        angleWidget->CreateDefaultRepresentation();
        angleWidget->SetRepresentation(rep);
        angleWidget->SetInteractor(renderWindowInteractor);

    //    renderWindow->Render();

        renderWindowInteractor->Initialize();
    //    renderWindow->Render();
        angleWidget->On(); // This line gives me error in debug mode
        rep->SetPoint1DisplayPosition(pos1);
        rep->SetPoint2DisplayPosition(pos2);
        rep->SetCenterDisplayPosition(center);
        rep->Ray1VisibilityOn();
        rep->Ray2VisibilityOn();
        rep->ArcVisibilityOn();

        // Begin mouse interaction
        renderWindowInteractor->Start();

        QPointer<QVTKOpenGLNativeWidget> vtkWidget = new QVTKOpenGLNativeWidget();
        vtkWidget->setRenderWindow(renderWindow);
        setCentralWidget(vtkWidget);
    }

I also tried other examples where VTK widgets are used but unsuccessfully…
I think there are some secrets when using VTK widgets in Qt.
Could someone explain where is the problem and how solve the problem?

I use VTK 9.0.0 and Qt 5.14.2, Windows 10 x64, MSVC 2017 x64

I think the (Qt) Widget must be visible and the QApplication main loop must be running before you call "On() on a widget.

Good luck

Hi,

Thank you for help but seems it doesn’t work.
I just placed the above code to the mouseDoubleClickEvent so the VTK angle widget should appear after I double click on the Qt’s widget. If I do this then many errors appear like:

ERROR: In F:\Qt\Downloaded\VTK-9.0.0\Rendering\Core\vtkRenderer.cxx, line 1456
vtkOpenGLRenderer (000001E575BDB550): WorldToPose: no active camera, cannot compute world to pose, returning 0,0,0

ERROR: In F:\Qt\Downloaded\VTK-9.0.0\Rendering\Core\vtkRenderer.cxx, line 1483
vtkOpenGLRenderer (000001E575BDB550): PoseToView: no active camera, cannot compute pose to view, returning 0,0,0

I also tried QVTKInteractor but it didn’t help neither

I’ve found Qt example with VTK widget. It works fine.

But I noticed one thing: to make VTK widget work (visible) we need to keep this VTK widget as private (or public/protected) variable of a class (like in the given example). If we don’t do that then VTK widget will not be visible. Is this a necessary condition or we can somehow avoid that?

1 Like

Because the code used vtkSmartPointer class. When no body reference the pointer, it will be released, and does’t like source, mapper and actor etc., they are have been referenced by others always.