PDA

View Full Version : Saving a single value QVariantlist to QSettings(ini format)



Zanyinj
17th September 2012, 15:24
Good day.

I have an application that has its own settings file, which I generate with Qt and load with Qt. I am using QSettings::IniFormat, because i want to be able to change these settings by hand if needed.

new QSettings("myconfig.cfg", QSettings::IniFormat);
It works like a charm, the only problem is when i do a setValue() with a QVariantList that only has one value.
My QVariantLists QVariants are all QRects.

e.g: When the list contains two rectangles,


QList<QRect> testList;
testList.append(QRect(0,0,1,1));
testList.append(QRect(1,1,1,1));

QList<QVariant> variantList;
foreach(QRect rect, testList)
{
variantList.append(rect);
}

m_settings->setValue(QString("rememberRectList"), variantList);


the configuration file's entry looks ok:

rememberRectList=@Rect(0 0 1 1), @Rect(1 1 1 1)

But, when the list contains only a single entry:


...
QList<QRect> testList;
testList.append(QRect(0,0,1,1));

QList<QVariant> variantList;
foreach(QRect rect, testList)
{
variantList.append(rect);
}

m_settings->setValue(QString("rememberRectList"), variantList);
...


I get this:

rememberRectList=@Variant(\0\0\0\t\0\0\0\x1\0\0\0\ x13\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0)

I am wondering if this is a bug or am I doing something wrong.

Thank you.

pkj
17th September 2012, 19:23
Are you saying that you are not able to retrieve the QVariantList if there is just one value in it. Or you are just intrigued by the way it looks in the file?

Zanyinj
17th September 2012, 21:56
No, no, the list retrieves fine, the only problem is the way it looks in the file - it's not human readable which makes it not human writable. Typing a couple of @Rect(x y w h) is easy and it would be nice if a single value also looked the same way.

I'm probably going to check for single values and just save them as a single QRect, which looks right in the file, or just implement a QStringList with custom syntax to generate these rects.

pkj
18th September 2012, 04:46
Don't you need to keep the xml format if to be made user readable?
Besides as soon as a stream operator of any class not hand crafted by you is used, it is bound to put in all kind of things including like QByteArray. That definitely is not human readable.
So you cannot rely upon datastream operators out of the box. Going into datastream function of QVariantList might be a good idea to know exactly what happens. But whatever happens is its behaviour and you will have to find your way around it by handcrafting your own operator overload function.