03-29-2014 09:57 PM
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.
Solved! Go to Solution.
03-31-2014 11:43 PM
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).
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.