[Qt-interest] [very OT] How to create single instance app?

Bob Hood bhood2 at comcast.net
Mon May 10 16:20:16 CEST 2010


On 5/10/2010 8:02 AM, Konrad Rosenbaum wrote:
> ....I think we digressed a little bit from the topic of this list.
> Didn't we?

Indeed.

To address the original question, I always place something like the
following in my stand-alone apps:

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        ...
    private:
        QTcpServer          instance_lock;
        ...
    };

and then in the constructor, I choose and hard code a random port number
between 1024 and 65534:

    if(!instance_lock.listen(QHostAddress::Any, <assigned_port_number>))
    {
        show_message(tr("Another instance of <application_name> seems to
be running."));
        exit(0);
    }

Works well so far.




More information about the Qt-interest-old mailing list