Single application library for Qt without network
dependency. Based on Dmitry Sazonov's code.
- Qt >= 5.9.2
- Compiler with standard >= C++17
- CMake >= 3.18.0
A list of additional CMake flags are listed below:
Flag | Default value | Description |
---|---|---|
USE_QT5 |
OFF |
Use Qt 5 instead of the higher major version available. |
GENERATE_PKG_CONFIG |
OFF |
Generate pkg-config file. |
STATIC_LIB |
OFF |
Build as a static library. |
If you want to use the library directly inside your application source code, include the singleapplication
directory and add the library on your CMakeLists.txt
file:
add_subdirectory(singleapplication)
target_link_libraries(YOUR_TARGET singleapplication)
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
After that, build and install the library on your system.
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release
cmake --build build --clean-first
cmake --install build --prefix build/pkg/usr/ --strip
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)
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()
.