KernelPanic
KernelPanic

Reputation: 2432

QSettings value method returns wrong QString

I have ordinrary text file with settings, which is generated by java application. Inside this file, there is a key db.url, which has value db.URL=jdbc\:mysql\://192.168.0.101\:3306/dbuser. I parse this file with QSettings class in QSettings::Native mode, everything is ok, but this db.URL gets messed up if I read it via value() method. Whatever I do (if I transform it into QString or QUrl), I get same result: jdbcmysql//192.,168.0.1013306/user. Why this key gets messed up?? I am using Qt 5.4 on Kubuntu 14.10 with kernel Linux desktop001 3.16.0-30-generic #40-Ubuntu SMP Mon Jan 12 22:06:37 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux. Here is a simple method that wrongly reads value of key:

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

and constants:

#ifndef UEDEFAULTS
#define UEDEFAULTS

#include <QString>

namespace UeDefaults
{
    namespace UeDbKeys
    {
        static const QString KEY_DB_DRIVER="db.driver";
        static const QString KEY_DB_PASSWORD="db.password";
        static const QString KEY_DB_URL="db.URL";
        static const QString KEY_DB_DRIVER_LIB="db.driverlib";
        static const QString KEY_DB_ENGINE="db.engine";
        static const QString KEY_DB_USER="db.user";
    }
}

#endif // UEDEFAULTS

Upvotes: 0

Views: 965

Answers (2)

KernelPanic
KernelPanic

Reputation: 2432

I did it:

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

    qDebug() << this->ueParsedData();

    settingsFile.close();
}

and now I get this url:

db.URL=jdbc:mysql://192.168.0.101:3306/dbuser

which is ok!

Upvotes: 0

svlasov
svlasov

Reputation: 10455

QSettings clears up the string from unsupported escape sequences, in this case \:. Remove \ slashes before reading the value or don't use QSettings for parsing unsupported file formats.

Perhaps not the most optimal solution but you could processes the settings file to escape all \: before reading it with QSettings.

QFile oldSettings("settings.txt");
oldSettings.open(QIODevice::ReadOnly);
QString data = QString::fromAscii(oldSettings.readAll().constData());
oldSettings.close();

data.replace("\\:", "\\\\:");

QFile newSettings("/tmp/settings.txt");
newSettings.open(QIODevice::WriteOnly);
newSettings.write(data.toAscii(), data.size());
newSettings.close();

Upvotes: 2

Related Questions