Search code examples
c++qtqt5qsettings

Can I make QSettings settings for 2 or more applications?


There are 2 applications:

  1. creates a file somewhere, and

  2. should go along some path and pick up the contents of the file.

Can I in the first application specify the path to the file in QSettings, and the second to take this path from the registry and go through it to the file?

Please tell me a simple example.


Solution

  • I cannot suggest a cross-platform method based on registry, but want to show another ways.


    Method 1 (based on common ini file)

    1. Store the path in some common ini file, for example in <some_common_path>/settings_common.ini. Both applications shall have access to the file:

    App 1 (Writer)

    #include <QtCore/QCoreApplication>
    
    #include <QSettings>
    #include <QDebug>
    
    #include <QThread>
    
    const QString CommonSettingsFilePath = "c:/temp/settings_common.ini";
    const QString CommonKey = "common/path";
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        for(;;)
        {
            static int it = 0;
    
            QString pathString = QString("path_%1").arg(it++);
    
            QSettings settings(CommonSettingsFilePath, QSettings::IniFormat);
            settings.setValue(CommonKey, pathString);
    
            QThread::sleep(1);
        }
    
        return a.exec();
    }
    

    App 2 (Reader)

    #include <QtCore/QCoreApplication>
    
    #include <QSettings>
    #include <QDebug>
    
    #include <QThread>
    
    const QString CommonSettingsFilePath = "c:/temp/settings_common.ini";
    const QString CommonKey = "common/path";
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        for (;;)
        {    
            QSettings settings(CommonSettingsFilePath, QSettings::IniFormat);
            QString pathString = settings.value(CommonKey, "").toString();
    
            qDebug() << "Path read from ini =" << pathString;
    
            QThread::sleep(2);
        }
    
        return a.exec();
    }
    

    Possible output of the App 2:

    Path read from ini = "path_6"
    Path read from ini = "path_8"
    Path read from ini = "path_9"
    Path read from ini = "path_11"
    ...
    

    Method 2 (based on Inter-process communication)

    IPC using Qt Remote Objects API:
    App1 is Source, App2 (reader) will be Replica, the path is a property.

    Only the main points are shown.

    In both App1 and App2 .pro files add remoteobjects module:

    QT  += core remoteobjects ...
    

    path.rep file:

    #include <QtCore>
    #include <QString>
    
    class Path
    {
        PROP(QString path);
    };
    

    In App1 pro:

    REPC_SOURCE = path.rep
    

    App1 class:

    #include "rep_path_source.h"
    
    class CommonPath : public PathSimpleSource
    {
        Q_OBJECT
    };
    

    Then in App1:

    CommonPath sourcePath;
    
    QRemoteObjectHost sourceNode(QUrl("local:path")); // create host node
    sourceNode.enableRemoting(&sourcePath); // enable remoting/sharing
    
    ...
    
    sourcePath.setPath("some_path_aaa");
    

    In App2 pro:

    REPC_REPLICA = path.rep
    

    In App2 furthermore:

    #include "rep_path_replica.h"
    
    QSharedPointer<PathReplica> replica;
    
    QRemoteObjectNode repNode; // create remote object node
    repNode.connectToNode(QUrl("local:path")); // connect with remote host node
    
    replica.reset(repNode.acquire<PathReplica>()); // acquire replica of source from host node
    
    replica->waitForSource();
    
    ...
    
    qDebug() << replica->path();
    

    Of course you can distribute all the necessary data in this way, not just the path.