QSettings子类没有任何键

时间:2015-07-27 10:03:20

标签: c++ qt

我正在将项目作为现有Java应用程序的补充。这个Java应用程序有它的配置文件,如下所示:

screen.600800=false
payment.commerceid=
machine.printer=printer\:(Default),receipt
paper.standard.mediasizename=A4
machine.printername=(Show dialog)
payments.textoverlay=false
paper.receipt.height=546
till.receiptsize=1
swing.defaultlaf=org.pushingpixels.substance.skinpack.StreetlightsSkin
paper.standard.y=72
paper.standard.x=72
table.tablecolour=black
till.SCRate=0
format.integer=\#,\#\#0
user.language=sl
paper.receipt.width=190
till.receiptprintoff=false
db.user=user
start.text=
machine.uniqueinstance=false
menu.autohide=false
till.pricewith00=false
table.waitercolour=red
format.double=\#,\#\#0.\#
user.country=
till.amountattop=false
till.pickupsize=1
till.autoLogoffrestaurant=false
machine.screenmode=window
screen.receipt.columns=42
machine.printer.6=Not defined
machine.printer.5=Not defined
db.engine=MySQL
machine.printer.4=Not defined
user.variant=
machine.printer.3=screen
machine.printer.2=printer\:(Show dialog),standard
payment.gateway=Not defined
till.SCOnOff=false
start.logo=logo.png
format.percent=\#,\#\#0.\#\#%
db.driver=com.mysql.jdbc.Driver
table.showwaiterdetails=true
format.date=
machine.ticketsbag=simple
db.driverlib=/opt/uBlagajna/./lib/mysql-connector-java-5.1.26-bin.jar
table.showcustomerdetails=true
db.password=test
till.receiptprefix=
paper.standard.height=698
table.customercolour=blue
till.taxincluded=false
paper.standard.width=451
till.autotimer=100
machine.display=screen
payment.testmode=true
format.datetime=
machine.scanner=Not defined
machine.hostname=work002
till.autoLogoff=false
paper.receipt.y=10
paper.receipt.x=10
payment.magcardreader=Not defined
payment.commercepassword=password
format.time=
format.currency='EUR' \#,\#\#0.00
db.URL=jdbc\:mysql\://127.0.0.1\:3306/testdb
till.hideinfo=false
machine.scale=Not defined
paper.receipt.mediasizename=A4
till.SCRestaurant=false
till.marineoption=false

现在,无论我在Qt C ++方面做什么,我都无法从密钥中获取值。因此我将QSettings类子类化为:

#ifndef UEPOSSESTTINGS_H
#define UEPOSSESTTINGS_H

#include <QtCore/qglobal.h>
#include <QSettings>
#include <QDirIterator>
#include <QStringList>
#include <QString>
#include <QFile>
#include <QDebug>
#include <QFile>

#include "settings/uedefaults.h"

class UePOSSesttings : public QSettings
{
    Q_OBJECT

private:
    QString m_strSettingsFilePath;
    QString m_strParsedData;
    QString m_strDbUrl;

    inline void ueSetConfigurationFilePath(const QString& path)
        { this->m_strSettingsFilePath=path; }

    inline QString ueParsedData() const
        { return this->m_strParsedData; }
    inline void ueSetParsedData(const QString& data)
        { this->m_strParsedData=data; }

    inline QString ueDbUrl() const
        { return this->m_strDbUrl; }
    inline void ueSetDbUrl(const QString& dbUrl)
        { this->m_strDbUrl=dbUrl; }

    void ueParseData(const QString& filename);

public:
    UePOSSesttings(const QString& organization,
                   const QString & application=QString(),
                   QObject* parent = 0);
    UePOSSesttings(const QString& fileName,
                   Format format,
                   QObject* parent = 0);
    ~UePOSSesttings();

    static QString ueSearchConfigurationFile();

    inline QString ueConfigurationFilePath() const
        { return this->m_strSettingsFilePath; }

    QString ueReadDbDriver() const;
    QString ueReadDbPassword() const;
    QString ueReadDbUrl() const;
    QString ueReadDbDriverLib() const;
    QString ueReadDbEngine() const;
    QString ueReadDbUser() const;
    QString ueReadDbHostname() const;
    QString ueReadDbName() const;
    int ueReadDbPort() const;
};


    #endif // UEPOSSESTTINGS_H   

和实施:

#include "uepossesttings.h"

UePOSSesttings::UePOSSesttings(const QString& organization,
                               const QString& application,
                               QObject* parent)
    : QSettings(organization,
                application,
                parent)
{
    this->setDefaultFormat(QSettings::NativeFormat);

    this->ueSetConfigurationFilePath(UePOSSesttings::ueSearchConfigurationFile());
    this->ueParseData(this->ueConfigurationFilePath());
}

UePOSSesttings::UePOSSesttings(const QString& fileName,
               Format format,
               QObject* parent)
    : QSettings(fileName,
                format,
                parent)
{
    this->ueSetConfigurationFilePath(fileName);
    this->ueParseData(this->ueConfigurationFilePath());
}

UePOSSesttings::~UePOSSesttings()
{
}

QString UePOSSesttings::ueSearchConfigurationFile()
{
    QDirIterator dirIterator("/home",
                             QStringList() << UeDefaults::UeApplication::APPLICATION_SETTINGS_FILE,
                             QDir::Files,
                             QDirIterator::Subdirectories);

    while(dirIterator.hasNext())
    {
        dirIterator.next();

        if(dirIterator.fileName()==UeDefaults::UeApplication::APPLICATION_SETTINGS_FILE)
        {
            return dirIterator.filePath();
        }   // if
    }   // while

    return dirIterator.filePath();
}

QString UePOSSesttings::ueReadDbDriver() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_DRIVER).toString();
}

QString UePOSSesttings::ueReadDbPassword() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_PASSWORD).toString();
}

QString UePOSSesttings::ueReadDbUrl() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_URL).toString();
}

QString UePOSSesttings::ueReadDbDriverLib() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_DRIVER_LIB).toString();
}

QString UePOSSesttings::ueReadDbEngine() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_ENGINE).toString();
}

QString UePOSSesttings::ueReadDbUser() const
{
    return this->value(UeDefaults::UeDbKeys::KEY_DB_USER).toString();
}

QString UePOSSesttings::ueReadDbHostname() const
{
    int iDbHostnameIndexStart=this->ueDbUrl().indexOf("//")+2;
    int iDbHostnameIndexStop=this->ueDbUrl().lastIndexOf(":");

    QString strHostname=this->ueDbUrl().mid(iDbHostnameIndexStart,
                                            (iDbHostnameIndexStop-iDbHostnameIndexStart));

    return strHostname;
}

QString UePOSSesttings::ueReadDbName() const
{
    int iDbNameIndexStart=this->ueDbUrl().lastIndexOf("/")+1;
    int iDbNameIndexStop=this->ueDbUrl().size();

    QString strDbName=this->ueDbUrl().mid(iDbNameIndexStart,
                                          (iDbNameIndexStop-iDbNameIndexStart));

    return strDbName;
}

int UePOSSesttings::ueReadDbPort() const
{
    int iDbPortIndexStart=this->ueDbUrl().lastIndexOf(":")+1;
    int iDbPortIndexStop=this->ueDbUrl().lastIndexOf("/");

    QString strPort=this->ueDbUrl().mid(iDbPortIndexStart,
                                        (iDbPortIndexStop-iDbPortIndexStart));

    return strPort.toInt();
}

void UePOSSesttings::ueParseData(const QString& filename)
{
    QFile settingsFile(filename);
    QString data;

    settingsFile.open(QIODevice::ReadOnly);
    data=QString::fromLatin1(settingsFile.readAll().constData());
    data.replace("\\:",
                 ":");
    this->ueSetParsedData(data);

    int iDbUrlIndexStart=data.indexOf(UeDefaults::UeDbKeys::KEY_DB_URL);
    int iDbUrlIndexStop=data.indexOf("\n",
                                     iDbUrlIndexStart);
    QString strDbUrl=data.mid(iDbUrlIndexStart,
                              (iDbUrlIndexStop-iDbUrlIndexStart));
    this->ueSetDbUrl(strDbUrl);

    settingsFile.close();
}

现在,无论我做什么,我通过ueRead ....的那个简短方法,例如,ueReadUser()返回空字符串。有没有人知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

我已经转储了QSettings,这里正在运行代码。部首:

#ifndef UEPOSSETTINGS_H
#define UEPOSSETTINGS_H

#include <QtCore/qglobal.h>
#include <QDirIterator>
#include <QStringList>
#include <QString>
#include <QFile>
#include <QDebug>
#include <QFile>

#include "uedefaults.h"

class UePOSSettings : public QObject
{
    Q_OBJECT

private:
    QString m_strSettingsFilePath;
    QString m_strParsedData;
    QString m_strDbUrl;
    QString m_strUser;
    QString m_strPassword;
    QString m_strHostname;
    QString m_strDbName;
    int m_iDbPort;

    inline void ueSetConfigurationFilePath(const QString& path)
        { this->m_strSettingsFilePath=path; }
    inline QString ueParsedData() const
        { return this->m_strParsedData; }
    inline void ueSetParsedData(const QString& data)
        { this->m_strParsedData=data; }

    inline QString ueDbUrl() const
        { return this->m_strDbUrl; }
    inline void ueSetDbUrl(const QString& dbUrl)
        { this->m_strDbUrl=dbUrl; }
    inline void ueSetDbUser(const QString& dbUser)
        { this->m_strUser=dbUser; }
    inline void ueSetDbPassword(const QString& dbPassword)
        { this->m_strPassword=dbPassword; }
    inline void ueSetDbHostname(const QString& dbHostname)
        { this->m_strHostname=dbHostname; }
    inline void ueSetDbName(const QString& dbName)
        { this->m_strDbName=dbName; }
    inline void ueSetDbPort(const int& dbPort)
        { this->m_iDbPort=dbPort; }

    void ueParseData(const QString& filename);

public:
    explicit UePOSSettings(QObject *parent = 0);

    ~UePOSSettings();

    Q_INVOKABLE static QString ueSearchConfigurationFile();

    inline QString ueConfigurationFilePath() const
        { return this->m_strSettingsFilePath; }

    Q_INVOKABLE inline QString ueDbUser() const
        { return this->m_strUser; }
    Q_INVOKABLE inline QString ueDbPassword() const
        { return this->m_strPassword; }
    Q_INVOKABLE inline QString ueDbHostname() const
        { return this->m_strHostname; }
    Q_INVOKABLE inline QString ueDbName() const
        { return this->m_strDbName; }
    Q_INVOKABLE inline int ueDbPort() const
        { return this->m_iDbPort; }
};

#endif // UEPOSSETTINGS_H

实现:

#include "uepossettings.h"

UePOSSettings::UePOSSettings(QObject *parent)
    : QObject(parent)
{
    this->ueSetConfigurationFilePath(UePOSSettings::ueSearchConfigurationFile());
    this->ueParseData(this->ueConfigurationFilePath());
}   // default constructor

UePOSSettings::~UePOSSettings()
{
}   // destructor

QString UePOSSettings::ueSearchConfigurationFile()
{
    QDirIterator dirIterator("/home",
                             QStringList() << UeDefaults::UeApplication::APPLICATION_SETTINGS_FILE,
                             QDir::Files,
                             QDirIterator::Subdirectories);

    while(dirIterator.hasNext())
    {
        dirIterator.next();

        if(dirIterator.fileName()==UeDefaults::UeApplication::APPLICATION_SETTINGS_FILE)
        {
            return dirIterator.filePath();
        }   // if
    }   // while

    return dirIterator.filePath();
}   // ueSearchConfigurationFile

void UePOSSettings::ueParseData(const QString& filename)
{
    QFile settingsFile(filename);
    QString data;

    settingsFile.open(QIODevice::ReadOnly);
    data=QString::fromLatin1(settingsFile.readAll().constData());
    data.replace("\\:",
                 ":");
    this->ueSetParsedData(data);

    int iDbUrlIndexStart=data.indexOf(UeDefaults::UeDbKeys::KEY_DB_URL);
    int iDbUrlIndexStop=data.indexOf("\n",
                                     iDbUrlIndexStart);
    QString strDbUrl=data.mid(iDbUrlIndexStart,
                             (iDbUrlIndexStop-iDbUrlIndexStart));
    this->ueSetDbUrl(strDbUrl);

    int iDbHostnameIndexStart=this->ueDbUrl().indexOf("//")+2;
    int iDbHostnameIndexStop=this->ueDbUrl().lastIndexOf(":");
    this->ueSetDbHostname(this->ueDbUrl().mid(iDbHostnameIndexStart,
                                             (iDbHostnameIndexStop-iDbHostnameIndexStart)));

    int iDbNameIndexStart=this->ueDbUrl().lastIndexOf("/")+1;
    int iDbNameIndexStop=this->ueDbUrl().size();

    this->ueSetDbName(this->ueDbUrl().mid(iDbNameIndexStart,
                                         (iDbNameIndexStop-iDbNameIndexStart)));

    int iDbPortIndexStart=this->ueDbUrl().lastIndexOf(":")+1;
    int iDbPortIndexStop=this->ueDbUrl().lastIndexOf("/");

    QString strPort=this->ueDbUrl().mid(iDbPortIndexStart,
                                       (iDbPortIndexStop-iDbPortIndexStart));

    this->ueSetDbPort(strPort.toInt());

    int iDbUserIndexStart=data.indexOf(UeDefaults::UeDbKeys::KEY_DB_USER)+UeDefaults::UeDbKeys::KEY_DB_USER.length()+1;
    int iDbUserIndexStop=data.indexOf("\n",
                                      iDbUserIndexStart);
    this->ueSetDbUser(data.mid(iDbUserIndexStart,
                              (iDbUserIndexStop-iDbUserIndexStart)));

    int iDbPasswordIndexStart=data.indexOf(UeDefaults::UeDbKeys::KEY_DB_PASSWORD)+UeDefaults::UeDbKeys::KEY_DB_PASSWORD.length()+1;
    int iDbPasswordIndexStop=data.indexOf("\n",
                                          iDbPasswordIndexStart);
    QString test=data.mid(iDbPasswordIndexStart,
                          iDbPasswordIndexStop);
    this->ueSetDbPassword(data.mid(iDbPasswordIndexStart,
                                  (iDbPasswordIndexStop-iDbPasswordIndexStart)));

    settingsFile.close();
}   // ueParseData