Skip to content

Single application library for Qt without network dependency

License

Notifications You must be signed in to change notification settings

AlfredoRamos/singleapplication

Repository files navigation

About

Single application library for Qt without network dependency. Based on Dmitry Sazonov's code.

Build Status Latest Stable Version Code Quality License

Dependencies

  • Qt >= 5.9.2
  • C++11 compiler support

Build

Subproject

If you want to use the library directly inside your application source code, you can use any of the following methods:

QMake subproject

Include the singleapplication.pri file within your project file:

include(singleapplication/singleapplication.pri)
CMake subproject

Include the singleapplication directory and add the library on your CMakeLists.txt file:

add_subdirectory(singleapplication)
target_link_libraries(YOUR_TARGET singleapplication)

System library

First you will need to get the sources and create a build directory. In-source builds are not allowed.

git clone https://github.com/AlfredoRamos/singleapplication.git
cd singleapplication
mkdir build
cd build

After that, you can use any of the following methods to build and install the library on your system.

QMake build
qmake ../ CONFIG+=release
make
make INSTALL_ROOT="pkg" install

Note: If you also want to generate the pkg-config file, use the following qmake command instead:

qmake ../ CONFIG+=release CONFIG+=pkgconfig

Once the files are installed on your system, add the library in your project file:

LIBS += -lsingleapplication

Alternatively, if you built the library with the CONFIG+=pkgconfig flag, you can use the library with pkg-config:

CONFIG += link_pkgconfig
PKGCONFIG += singleapplication
CMake build
cmake -S ../ -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release
cmake --build . --clean-first
cmake --install . --prefix pkg/usr/ --strip

Note: If you also want to generate the pkg-config file, replace the first cmake command with the following:

cmake -S ../ -DCMAKE_INSTALL_PREFIX=/usr -DGENERATE_PKG_CONFIG=ON -DCMAKE_BUILD_TYPE=Release

Once the files are installed on your system, add the library in your CMakeLists.txt file:

find_package(singleapplication REQUIRED)
target_link_libraries(YOUR_TARGET singleapplication)

Alternatively, if you built the library with the -DGENERATE_PKG_CONFIG=ON flag, you can use the library with pkg-config:

find_package(PkgConfig REQUIRED)
pkg_check_modules(singleapplication REQUIRED IMPORTED_TARGET singleapplication)
target_link_libraries(YOUR_TARGET PkgConfig::singleapplication)

Usage

In the main.cpp file of your Qt/C++ application include the library, create a new instance of SingleApplication, and add a check if another instance is already running using SingleApplication::createInstance():

// Subproject
//#include "singleapplication.hpp"

// System library
//#include <singleapplication.hpp>

int main(int argc, char *argv[])
{
	SingleApplication *guard = new SingleApplication("key_string");

	if (!guard->createInstance()) {
		// Another instance of this application is already running
		return 0;
	}

	QApplication a(argc, argv);
	//...
}

The constructor of the SingleApplication class only accepts one parameter and must be a QString.

You can specify random generated QString or using information from the application, like QCoreApplication::applicationName().