PDA

View Full Version : QVariant



durus90
25th July 2017, 09:35
Hello every1!

I have this code:


class Model
{
public:
Model(const QString& path);
double getVehicleWeight()
{
m_settings->beginGroup("vehicle");
const QStringList childKeys = m_settings->childKeys();
foreach (const QString &childKey, childKeys)
{
//Here I want to return a double using QVariant, each line of my config file have (ex: weight = 60)
}
m_settings->endGroup();
}
private:
void initSettings(const QString& path);
protected:
std::unique_ptr<QSettings> m_settings;
};

Model::Model(const QString& path)
{
initSettings(path);
}

void Model::initSettings(const QString& path)
{
m_settings = std::make_unique<QSettings>(path, QSettings::IniFormat);
}

int main()
{
Model obj("/d/users/F27279C/Desktop/TESTINI/TestINI/New folder");
qDebug() << "PLS WORK!";
obj.getVehicleWeight();
}

I know the ,,foreach" will go through all my document, but i dont want that
I want ot make a getter for every line of my config file which will return the value from it
All the values are double
so what I ask is:
- how can I access a specific line of my config for ex weight or speed?
- how can I return a double using QVariant?

thx for help!

high_flyer
25th July 2017, 09:55
If you want help, try not to make it hard for people to read your post.
Don't use sms style text, use proper english.
Enclose code in the code sections.

Since you are using QSettings, what is the problem?
QSettings allows you to access each key directly through the value() method.
QSettings::value() returns your values as QVariant already.
I would strongly recommend you read QSettings docs first.

d_stranz
25th July 2017, 17:51
I know the ,,foreach" will go through all my document, but i dont want that

Why would you want to go through -all- of the child keys in your settings when you know the one you want is named "weight"?



double getVehicleWeight() const
{
return m_settings->value( "vehicle/weight" ).toDouble();
}


beginGroup() and endGroup() are unnecessary, and by eliminating them you can make the getVehicleWeight() method const which might help the compiler optimize your code more efficiently.