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();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…