vtkCaptionWidget interferes with Qt C++ Application quit

I am using vtkCaptionWidget to annotate the 2D image loaded in qvtkwidget. However once the caption widget is placed on the image QApplication::exit(0) doesn’t work. Even if the window is closed by calling close() function, application runs in the background.

This is my code for caption written in a clickcallback function

auto* iren = static_cast<vtkRenderWindowInteractor*>(caller);

    vtkSmartPointer<vtkCoordinate> coordinate = vtkSmartPointer<vtkCoordinate>::New();
    coordinate->SetCoordinateSystemToDisplay();
    coordinate->SetValue(iren->GetEventPosition()[0],iren->GetEventPosition()[1],0);

    double* world = coordinate->GetComputedWorldValue(iren->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
    double pos[3] = {world[0], world[1], 0};

    std::string str = annotText.toStdString();
    const char* cText = str.c_str();

    vtkTextProperty *tprop = vtkTextProperty:: New();
    tprop->SetFontFamilyToArial();
    tprop->SetFontSize(14);
    tprop->BoldOn();
    tprop->ShadowOn();
    tprop->ItalicOn();
    tprop->SetColor(1, 1, 1);

    vtkNew<vtkCaptionRepresentation> captionRepresentation;
    captionRepresentation->GetCaptionActor2D()->GetTextActor()->SetTextScaleMode(1);
    captionRepresentation->GetCaptionActor2D()->SetCaptionTextProperty(tprop);
    captionRepresentation->GetCaptionActor2D()->SetCaption(cText);
    captionRepresentation->GetCaptionActor2D()->BorderOn();
    captionRepresentation->GetCaptionActor2D()->ThreeDimensionalLeaderOff();
    captionRepresentation->GetCaptionActor2D()->SetMaximumLeaderGlyphSize(10);
    //captionRepresentation->GetCaptionActor2D()->GetPositionCoordinate()->SetCoordinateSystemToViewport();
    captionRepresentation->SetPosition(0.7,0.3);
    captionRepresentation->SetPosition2(0.1,0.03);
    captionRepresentation->SetAnchorPosition(pos);

    vtkNew<vtkCaptionWidget> captionWidget;
    captionWidget->SetInteractor(iren);
    captionWidget->SetRepresentation(captionRepresentation);
    captionWidget->On();
    iren->Start();

This is the code in quit button click

    QApplication::exit(0);
    ui->~MainWindow();

What could be causing this. This doesn’t arise on placing any other widgets like distance or angle widget.

Hello,

Is there any reason to explicitly call QApplication::exit()? Once the application’s last QWidget gets destroyed, the UI’s event loop (QApplication::exec()) returns. If it is the last call in your program, the program finishes. You don’t need to call QApplication::exit() explicitly. Also, you’re not supposed to call a destructor like that. It either gets called by delete or when a stack or static object goes out of scope.

take care,

Paulo

You can try calling the MainWindow::close() slot from the button click to finish your program.

Hello… I have tried calling close(). The window shuts down. However the next time i try to build the application, previous instance is seen as crashed - which means it was running in background and didn’t have a normal exit.

This issue occurs only when we place the caption widget. No problem whatsoever otherwise. Was wondering why.

Do you have other Qt applications other than Qt Creator and your own?