GithubHelp home page GithubHelp logo

dedicate617 / qsingleinstance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juangburgos/qsingleinstance

0.0 1.0 0.0 9 KB

Qt C++ library to check if another instance of an application is running. Disallow multiple application instances.

QMake 29.21% C++ 70.79%

qsingleinstance's Introduction

Usage

Add to project

Add qsingleinstance.pri to your application's project file (Qt's pro file), e.g.

# other stuff

include(./QSingleInstance.git/src/qsingleinstance.pri)

# other stuff

Add to source

An instance of the QSingleInstance class must be added to the main.cpp file of your application. The instance must be added right after the QApplication instantiation, but before the instantiation of any other cusotm widget. E.g.

#include <QApplication>
#include <QMessageBox>
#include "dialog.h"

// Add single instance header
#include <QSingleInstance>

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

    // Apply guard after QApplication instantiation
    // but before instantiating any custom widgets
    QSingleInstance guard("MyQDialog");
    if (!guard.tryToRun())
    {
        QMessageBox::critical(nullptr,
            QObject::trUtf8("Single Instance Guard"),
            QObject::trUtf8("An instance of this application is already running.\nClose existng instance before running a new one."),
            QMessageBox::Ok);
        return -1;
    }

    // Custom application dialog
    Dialog w;
    w.show();
    
    return a.exec();
}

Specifics

The QSingleInstance class creates some temporary files to obtain the single instance locking mechanism. The directory and name of these files are platform dependent.

The directory in Windows is in the system's temporary directory, this is usually the path in the TEMP or TMP environment variable.

In Linux, this is the path in the TMPDIR environment variable or /tmp if TMPDIR is not defined.

An example of the names of thse files are:

qipc_systemsem_beeccbcdeeaefcde296e990973cc975cf92bdb10bdb3bfd583bb2d5d
qipc_systemsem_aefbdbfedfcada7964cb9b08e40a7170e807eac76844af69280cd5
qipc_sharedmemory_aefbdbfedfcada7964cb9b08e40a7170e807eac76844af69280cd5

These files are created when the first instance is created. Then remain through the life span of the application. They must be automatically deleted when the single instance is closed. Any bugs on the single instance behaviour are notmally related to these files.

References

qsingleinstance's People

Contributors

juangburgos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.