linux的sync()在哪里定义。一旦关闭应用程序,写入QSettings的数据就不会保留

时间:2019-04-08 14:27:14

标签: qt ubuntu qsettings

我在Ubuntu中工作,想在QSettings中存储数据。在运行期间,数据是持久的,一旦应用程序重新启动,就不再读取数据。我读到需要额外的linux同步调用,但是我无法找到它的定义位置。

在运行程序期间,我已成功将数据写入QSettings对象并从存储的QSettings对象读取了值。

我有一个可以从QJSonObject读写数据的用户类


class User
{
public:
    User() {}
    User(const QString& ruser_name, const QImage& rdisplay_image)
    {
        user_name = ruser_name;
        display_image = rdisplay_image;
    }

    QString get_user_name() const { return user_name; }

    void write(QJsonObject &json) const
    {
        json["user_name"] = user_name;
        json["display_image"] = getImageInJsonValueFormat(display_image);

    }

    void read(const QJsonObject &json)
    {
        if (json.contains("user_name") && json["user_name"].isString())
            user_name = json["user_name"].toString();

        if (json.contains("display_image"))
            display_image = getImageFromJsonValueFormat(json["display_image"]);
    }
private:
    QString user_name;
    QImage  display_image;
};

有两个用于处理image和Json的全局功能


QJsonValue getImageInJsonValueFormat(const QImage& image)
{
    QByteArray image_ByteArray;
    if (image.isNull() != true)
    {
        QBuffer image_buffer(&image_ByteArray);
        image_buffer.open(QIODevice::WriteOnly);
        image.save(&image_buffer, "png");
        auto const encoded = image_buffer.data().toBase64();
        return {QLatin1String(encoded)};
    }
    return QLatin1String("");
}

QImage getImageFromJsonValueFormat(const QJsonValue& imageJsonValue)
{
    QImage image;
    auto const encoded = imageJsonValue.toString().toLatin1();
    image.loadFromData(QByteArray::fromBase64(encoded), "png");
    return image;
}

还有三个功能,但是从QSettings中读取列表,以创建默认列表并将列表存储到QSettings。

QMap<QString, User> Build_user_list()
{
    QMap<QString, User> user_list;
    User User1("baur",QImage("/home/Downloads/bechambaur.png"));
    user_list["baur"] = User1;

    User User2("Muller",QImage("/home/Downloads/GerdMuller.png"));
    user_list["Muller"] = User2;
    return user_list;
}

void StoreUserList(const QMap<QString, User> user_list)
{
    QJsonObject userListObject;
    QJsonArray userListArray;

    QMapIterator<QString,User> iterator(user_list);

    while (iterator.hasNext())
    {
        iterator.next();
        QJsonObject userJsonObject;
        User userObj = iterator.value();
        userObj.write(userJsonObject);
        userListArray.append(userJsonObject);
    }
    userListObject["user_list"] = userListArray;
    QJsonDocument userListDoc(userListObject);

    QSettings user_settings;

    user_settings.setValue("QSettingsApplication", userListDoc);
    user_settings.sync();
}

QMap<QString, User> GetStoredList()
{
    QMap<QString, User> user_list;
    QSettings user_settings;
    QJsonDocument userListDocRead = user_settings.value("QSettingsApplication", "").toJsonDocument();
    QJsonObject userListObjectRead = userListDocRead.object();//["userlist"];

    if (userListObjectRead.contains("user_list"))
    {
        if(userListObjectRead["user_list"].isArray())
        {
            QJsonArray userlistarray = userListObjectRead["user_list"].toArray();
            user_list.clear();
            for(int index = 0; index < userlistarray.size(); ++index)
            {
                QJsonObject json_user_object = userlistarray[index].toObject();
                User userObject;
                userObject.read(json_user_object);
                user_list[userObject.get_user_name()] = userObject;
            }
        }
    }

    return user_list;
}

此应用程序随后运行时无法从QSettings中读取值,该值在运行期间已成功读取


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // Why below list is empty after first successful run?
    QMap<QString, User> user_listRead = GetStoredList();

    if (user_listRead.size() == 0)
    {
        QMap<QString, User> user_list = Build_user_list();
        StoreUserList(user_list);
        QMap<QString, User> user_listRead = GetStoredList();
        user_listRead = GetStoredList();
        // This is is not empty, it reads contents successfully. What's wrong?
    }
    MainWindow w;
    w.show();

    return a.exec();
}

我希望数据是持久的。哪里出了问题?

整个文件的内容是

#include "mainwindow.h"
#include <QApplication>

#include <QString>
#include <QImage>
#include <QJsonObject>
#include <QBuffer>
#include <QSettings>
#include <QJsonArray>
#include <QJsonDocument>

QJsonValue getImageInJsonValueFormat(const QImage& image)
{
    QByteArray image_ByteArray;
    if (image.isNull() != true)
    {
        QBuffer image_buffer(&image_ByteArray);
        image_buffer.open(QIODevice::WriteOnly);
        image.save(&image_buffer, "png");
        auto const encoded = image_buffer.data().toBase64();
        return {QLatin1String(encoded)};
    }
    return QLatin1String("");
}

QImage getImageFromJsonValueFormat(const QJsonValue& imageJsonValue)
{
    QImage image;
    auto const encoded = imageJsonValue.toString().toLatin1();
    image.loadFromData(QByteArray::fromBase64(encoded), "png");
    return image;
}

class User
{
public:
    User() {}
    User(const QString& ruser_name, const QImage& rdisplay_image)
    {
        user_name = ruser_name;
        display_image = rdisplay_image;
    }

    QString get_user_name() const { return user_name; }

    void write(QJsonObject &json) const
    {
        json["user_name"] = user_name;
        json["display_image"] = getImageInJsonValueFormat(display_image);

    }

    void read(const QJsonObject &json)
    {
        if (json.contains("user_name") && json["user_name"].isString())
            user_name = json["user_name"].toString();

        if (json.contains("display_image"))
            display_image = getImageFromJsonValueFormat(json["display_image"]);
    }
private:
    QString user_name;
    QImage  display_image;
};

QMap<QString, User> Build_user_list()
{
    QMap<QString, User> user_list;
    User User1("baur",QImage("/home/Downloads/bechambaur.png"));
    user_list["baur"] = User1;

    User User2("Muller",QImage("/home/Downloads/GerdMuller.png"));
    user_list["Muller"] = User2;
    return user_list;
}

void StoreUserList(const QMap<QString, User> user_list)
{
    QJsonObject userListObject;
    QJsonArray userListArray;

    QMapIterator<QString,User> iterator(user_list);

    while (iterator.hasNext())
    {
        iterator.next();
        QJsonObject userJsonObject;
        User userObj = iterator.value();
        userObj.write(userJsonObject);
        userListArray.append(userJsonObject);
    }
    userListObject["user_list"] = userListArray;
    QJsonDocument userListDoc(userListObject);

    QSettings user_settings("userlist.conf", QSettings::NativeFormat);//(QSettings::IniFormat,QSettings::UserScope,"A","B");
    user_settings.setValue("QSettingsApplication", userListDoc.toVariant());

    user_settings.sync();
}

QMap<QString, User> GetStoredList()
{
    QMap<QString, User> user_list;
    QSettings user_settings("userlist.conf", QSettings::NativeFormat);

    QVariant qvarval = user_settings.value("QSettingsApplication");

    QJsonDocument userListDocRead = user_settings.value("QSettingsApplication").toJsonDocument();
    QJsonObject userListObjectRead = userListDocRead.object();//["userlist"];

    if (userListObjectRead.contains("user_list"))
    {
        if(userListObjectRead["user_list"].isArray())
        {
            QJsonArray userlistarray = userListObjectRead["user_list"].toArray();
            user_list.clear();
            for(int index = 0; index < userlistarray.size(); ++index)
            {
                QJsonObject json_user_object = userlistarray[index].toObject();
                User userObject;
                userObject.read(json_user_object);
                user_list[userObject.get_user_name()] = userObject;
            }
        }
    }

    return user_list;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QMap<QString, User> user_listreRead;



    // Why below list is empty after first successful run?
    QMap<QString, User> user_listRead = GetStoredList();

    if (user_listRead.size() == 0)
    {
        QMap<QString, User> user_list = Build_user_list();
        StoreUserList(user_list);
        user_listreRead = GetStoredList();
        // This is is not empty, it reads contents successfully. What's wrong?
    }


    MainWindow w;
    w.show();

    return a.exec();
}

userlist.conf的内容是

[General]
QSettingsApplication=@Variant(\0\0\0\b\0\0\0\x1\0\0\0\x12\0u\0s\0\x65\0r\0_\0l\0i\0s\0t\0\0\0\t\0\0\0\x2\0\0\0\b\0\0\0\x2\0\0\0\x12\0u\0s\0\x65\0r\0_\0n\0\x61\0m\0\x65\0\0\0\n\0\0\0\f\0M\0u\0l\0l\0\x65\0r\0\0\0\x1a\0\x64\0i\0s\0p\0l\0\x61\0y\0_\0i\0m\0\x61\0g\0\x65\0\0\0\n\0\0\0\0\0\0\0\b\0\0\0\x2\0\0\0\x12\0u\0s\0\x65\0r\0_\0n\0\x61\0m\0\x65\0\0\0\n\0\0\0\b\0\x62\0\x61\0u\0r\0\0\0\x1a\0\x64\0i\0s\0p\0l\0\x61\0y\0_\0i\0m\0\x61\0g\0\x65\0\0\0\n\0\0\0\0)

0 个答案:

没有答案