自定义QSettings函数未调用

时间:2019-04-16 10:09:28

标签: c++ qt qsettings

我有以下一段代码,应该为Qsettings最终提供XML功能:

/**
 * @brief   Custom XML format reading function.
 * @param   device: ie. the read file.
 * @param   map: key + value to read
 * @return  non zero on succesfull read.
 */
static bool readXmlFile(QIODevice& device,  QSettings::SettingsMap& map)
{
    qDebug() << __func__ << " called";
    (void)device;
    (void)map;
    return false;
}

/**
 * @brief   Custom XML format writing function.
 * @param   device: ie. the written file.
 * @param   map: key + value to read
 * @return  non zero on succesfull write.
 */
static bool writeXmlFile(QIODevice& device, const QSettings::SettingsMap& map)
{
    qDebug() << __func__ << " called";
    (void)device;
    (void)map;
    return false;
}

/**
 * @brief   Initializes the settings object.
 * @param   path: Full path to the file. If it doesnt exist, it is created.
 * @param   parent: the parent object.
 */
Cxmlsettings::Cxmlsettings(const QString& path, QObject *parent) :
    QObject(parent)
{
    QFileInfo fi(path);
    if (!fi.exists() || !fi.isFile())
    {
        // config file doesnt exist, create it.
        QFile cFile(path);
        if (!cFile.open(QIODevice::NewOnly))
        {
            qCritical() << "Failed to create " << path << " file";
            return;
        }

        cFile.close();
    }

    const QSettings::Format xmlformat =
            QSettings::registerFormat("xml", &readXmlFile, &writeXmlFile);

    m_settings.setPath(xmlformat, QSettings::UserScope, path);
    m_settings.setValue("abc", 10);
    volatile const int test = m_settings.value("abc").toInt();
}

我的问题是,在设置m_settings变量的路径并运行写入和读取函数之后,我的自定义函数readXmlFilewriteXmlFile没有被调用。 path下的文件始终存在,因为如果在调用setValue之前不存在该文件,则会创建该文件。

0 个答案:

没有答案