Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

qt - How to use QtSingleApplication?

  1. MacOSX10.8 + Qt5.1 with clang 64bit
  2. Create Qt Gui application
  3. in the main.cpp I just add #include <QtSingleApplication>

Compile error:

../untitled/main.cpp:3:10: fatal error: 'QtSingleApplication' file not found
#include <QtSingleApplication>
         ^
1 error generated.
make: *** [main.o] Error 1

What did I miss?


Additional information
project file has already set QT += widgets

I google already, it seems need qtlocalpeer.cpp,qtlocalpeer.h,qtsingleapplication.cpp, qtsingleapplication.h,qtsinglecoreapplication.cpp,qtsinglecoreapplication.h, but how to generate these files?

some posts said it needs include qtsingleapplication.pri, but where is it? I searched my disk I cannot find it.


reference
SinleApplication from Qt

QtSingleApplication Class Reference

Sample by Qt but where to download source code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are looking for a better alternative to the QtSingleApplication class, especially one that works on Qt5 and supports QtQuick take a look at the SingleApplication project.

It uses QSharedMemory to ensure no race condition occur and is really easy to use. You just replace the call to QApplication with SingleApplication similar to how QtSingleApplication works:

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

    return app.exec();
}

It also uses Local Sockets to notify your original (first) process for the initialization of a new one so the parent can raise it's window. The dull documentation and code are available on GitHub.

Among other things it supports sending messages between the newly spawned instance and the primary instance. In this example new instances send the primary instance a message containing their arguments.

int main(int argc, char *argv[])
{
    SingleApplication app( argc, argv, true );

    if( app.isSecondary() ) {
        app.sendMessage(  app.arguments().join(' ')).toUtf8() );
        app.exit( 0 );
    }

    return app.exec();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...