Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido
Accepted Solution

How to store QHash of custom objects to QSetting


I have a custom class Host which extends QObjects, I wanna store a bunch of Host instances in QSetting. However, it got the error "within this context" when i try to compile. Here is my code

//create the server
Host host = new Host();
...some initialization for the host

//load setting
QSettings settings(m_author, m_appName);
QHash<QString, QVariant> *hosts = new QHash<QString, QVariant>();

//put the new host to the hash
hosts.insert(host->name(), QVariant(host)); //this is where i got "within this context"
settings.setValue("hosts", hosts); //same error on this line


I dont know if this is a right approach, I am even not sure if settings can store QHash, as far as i test, i can put <k,v> as <QString, QString> but i guess because QString is Qvariant so it worked.

Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: How to store QHash of custom objects to QSetting


Please see if this helps:



Basically, you'll need to implement serialization operators for custom type and register them using qRegisterMetaTypeStreamOperators.


Btw, QSettings writes aren't atomic and it can lose data occassionaly (this isn't fixed even in Qt 5). Smiley Sad

If you need to store the data reliably, consider serializing it to the file manually. Ideally, the data should be saved to a temporary file, renaming the file into original one, so the operation is atomic.

Andrey Fidrya, @zmeyc on twitter