c++ - 我可以为 2 个或更多应用程序进行 QSettings 设置吗?

标签 c++ qt qt5 qsettings

有2个应用:

  1. 在某处创建一个文件,然后

  2. 应该沿着一些路径获取文件的内容。

我能否在第一个应用程序中指定 QSettings 中文件的路径,然后在第二个应用程序中从注册表中获取此路径并通过它找到文件?

请告诉我一个简单的例子。

最佳答案

我不能建议基于注册表的跨平台方法,但想展示另一种方法。


方法一(基于普通ini文件)

  1. 将路径存储在一些常见的 ini 文件中,例如 <some_common_path>/settings_common.ini .两个应用程序都应有权访问该文件:

App 1(作家)

#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();
}

应用 2(阅读器)

#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();
}

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"
...

方法二(基于进程间通信)

IPC使用 Qt Remote Objects API :
App1 是 Source , App2(阅读器)将是 Replica ,路径是一个属性。

只显示要点。

在 App1 和 App2 .pro 文件中添加 remoteobjects模块:

QT  += core remoteobjects ...

path.rep 文件:

#include <QtCore>
#include <QString>

class Path
{
    PROP(QString path);
};

在 App1 pro 中:

REPC_SOURCE = path.rep

App1 类:

#include "rep_path_source.h"

class CommonPath : public PathSimpleSource
{
    Q_OBJECT
};

然后在 App1 中:

CommonPath sourcePath;

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

...

sourcePath.setPath("some_path_aaa");

在 App2 pro 中:

REPC_REPLICA = path.rep

此外在 App2 中:

#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();

当然,您可以通过这种方式分发所有必要的数据,而不仅仅是路径。

关于c++ - 我可以为 2 个或更多应用程序进行 QSettings 设置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59478635/

相关文章:

c++ - 如果 QTreeView 项没有子项,则禁用 QAction

c++ - for循环到while转换

android - 如何在 Qt (QML) 应用程序中使用适用于 Android 的 Tensorflow?

qt - 无法将进度条扩展到状态栏的整个宽度

c++ - 如何在鼠标按下事件上显示 QToolTip 并使其弹出一段时间? Qt

c++ - QTableWidget - 组合框委托(delegate)我如何允许每个单元格有不同的选项

c++ - QQuickView 和 QQuickWindow 有什么区别?

c++ - 使用旧的 C 库调用临时对象的方法会导致编译器错误

c++ - 默认构造函数递归崩溃

c++ - 如果不在结构中初始化 vector ,它会自动为空还是会具有随机内存位置的值?