简体   繁体   中英

How to force my application to open one exe only? qt, linux

I want my application to open only one process, ie if the one process is already opened and the user want to open the exe again - it won't open another process.

how can I do it in Qt - linux?

10x!

What you're looking for is QtSingleApplication .

If you start another instance of your application, the first one will even get notified about it (you can pass whatever data structure you want).

I used it to bring the existing application to the front whenever another instance is started.

Use the following code in the main.cpp to prevent to run more than one instance of your application. I tested this code under Linux (in QtCreator) and it works (works for Windows too). I find this solution simple and easy to implement. The example is for a console application. The code remain the same for a GUI application, check the comments in the code.

//main.cpp
#include <QCoreApplication> //Console application
//#include <QApplication>     //GUI application
#include <QSharedMemory>
#include <QDebug>
//Your QMainWindow derivated class goes here :
//#include "MainWindow.h"

int main(int argc, char *argv[])
{

  QCoreApplication app( argc, argv );

  app.processEvents();

  //---- Check for another instance code snippet ----

  //GUID : Generated once for your application
  // you could get one GUID here: http://www.guidgenerator.com/online-guid-generator.aspx
  QSharedMemory shared("62d60669-bb94-4a94-88bb-b964890a7e04");

  if( !shared.create( 512, QSharedMemory::ReadWrite) )
  {
    // For a GUI application, replace this by :
    // QMessageBox msgBox;
    //msgBox.setText( QObject::tr("Can't start more than one instance of the application.") );
    //msgBox.setIcon( QMessageBox::Critical );
    //msgBox.exec();

    qWarning() << "Can't start more than one instance of the application.";

    exit(0);
  }
  else {
      qDebug() << "Application started successfully.";
  }
  //---- END OF Check for another instance code snippet ----

  // Only one instance is running, declare MainWindow
  //MainWindow myMainWindow;
  //myMainWindow.show();


  //We enter the Qt Event loop here, we don't leave until the MainWindow is closed
  //or the console application is terminated.
  return app.exec();
}

This may not concern you, but I thought it would be useful to bring it up. I'm using QtSingleApplication myself and experienced some odd behavior a few days ago. QtSingleApplication doesn't seem to work under all circumstances. I made this expierence in windows, but depending on wether this is a windows specific bug or intended by the design of QtSingleApplication , it may also apply to linux.

Depending on the way you start your application multiple instances are possible. I made this experience when I did a testdrive of my application using my installer. The installer automatically starts the application after finishing. When I then started my application using the desktop link, I had two instances running. So the functionality of QtSingleApplication seems to depend on the way how (and by which user?) the application is started. The documentation is unclear about this. But I think usually one would expect this to work under all circumstances, if not stated otherwise.

So, if you don't need the extra functionality added by QtSingleApplication , QSystemSemaphore or QSharedMemory seems to be the better way to go.

Your application could check if a certain file in the user's home directory is present. If it is present, the application exits. If it is not there, the application creates it and continues. Of course, you might get a race condition if the user starts the application several times at once. But for most cases this simple solution should be sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM