Qt Creator: Close the previous application instance before starting again?

Each time I run my project in Qt Creator, it spins another instance of my application. I need to manually exit the application, otherwise my dock becomes pretty fast. This is pain. Is there any way around this? It would make much more sense if, when I restarted the application, I could simply end the already running version. It can be done?

+5
source share
1 answer

You can use shared memory to solve your problem. I used this approach so as not to run another instance of my program while there is already an instance running. In fact, I implemented this to achieve the so-called single-instance application.

However, your case is slightly different from mine, you need to somehow send a signal from the second application to the first in order to close it. I think you can achieve this behavior using QSharedMemory.

, , - (UUID) , , , , , .

, . , , ( qt) , , () , . , , , , ...

:

IN MAIN
   check if shared memory in use
      if yes
         fire the exit function via shared memory to close 1st app
      if no
         put the function pointer which will close the app when another instance come up

    do stuff

,

QSharedMemory shared(AppConstants::UUID); //Global variable

int main(){
// Ensure single instanse of App
if( !shared.create( 512, QSharedMemory::ReadWrite) )
{
    // QMessageBox msgBox;
    QMessageBox::critical(0, QObject::tr("App is already running!"), QObject::tr("App is already running!"), QMessageBox::Ok, QMessageBox::Ok);
    qCritical() << "Cevirgec is already running!";

    exit(0);
}
else {
    qDebug() << "App staring...";
}
}

;)

EDIT:

Qt-, (, ), , ( ) 1, .

! , , , . , . : QPair<int, QDateTime>

+1

All Articles