I want to call api on close event of mainwindow

What i want is if multiple instance is opened only the last instance will call the delete machine api , right now what happen is if i closr the app it get the count of running instance in memory that will give me wrong count if recently last app closed is still in memory

void qSlicerAppMainWindow::closeEvent(QCloseEvent* event)
{
Q_D(qSlicerAppMainWindow);

// This is necessary because of a Qt bug on MacOS.
// (https://bugreports.qt.io/browse/QTBUG-43344).
// This flag prevents a second close event to be handled.
if (d->IsClosing)
{
    event->ignore();
    return;
}
d->IsClosing = true;

if (d->confirmCloseApplication())
{
    // Proceed with closing the application

    // Exit current module to leave it a chance to change the UI (e.g. layout)
    // before writing settings.
    d->ModuleSelectorToolBar->selectModule("");

    this->saveGUIState();
    event->accept();

    //delete api call based on running instance
    QTimer::singleShot(0, qApp, SLOT(closeAllWindows()));
    
}
else
{
    // Request is cancelled, application will not be closed
    event->ignore();
    d->IsClosing = false;
}

} can anyone tell me how i can achieve this ?

This was solved usin QSettings