Important: Please read the Qt Code of Conduct -

Save QSet using QSettings

  • I need to have a list of unique strings, for which I prefer to use QSet, because I also need to do searches. However, I'd like to save this list saved in settings, i.e. use QSettings to save it. However, there's no QSet => QVariant conversion so I'm stuck.

    [Edit: code tags, typos ~~ @Wieland]

  • Seems to work with setValue()

    QSet<QString> set;
    QVariant var; 
    var.setValue(set);  // set -> variant
    set=var.value<QSet<QString>>(); // variant -> set

  • this is what i did:

    void Game::readSettings() {
        QSettings settings { settingsFile, QSettings::IniFormat };
        const auto usernames = settings.value("usernames");
        m_usernames = usernames.value<QSet<QString>>();
    void Game::writeSettings() const {
        QSettings settings { settingsFile, QSettings::IniFormat };
        QVariant varUsernames;
        settings.setValue("usernames", varUsernames);

    but i get runtime error on variant conversion (can't convert types)

  • I've got this error with QSettings:

    QVariant::save: unable to save type 'QSet<QString>' (type id: 1092).
    QVariant::load: unable to load type 1092.

    I have no clue unfortunately ...

  • i guess i'll just use QStringList...

  • well, and another question i have, is...

    i read the settings file from the "main" class where i have all my widgets. however, every widget has some specific properties that other widgets don't. and i want to read that property from the settings file. is it considered a good practice to read the same settings file from a few places?

  • Yes, it is what QSettings is made for.
    From Qt doc:

    If you use QSettings from many places in your application, 
    you might want to specify the organization name and the application name using 
    QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), 
    and then use the default QSettings constructor:
          QCoreApplication::setApplicationName("Star Runner");
          QSettings settings;

  • Lifetime Qt Champion

    @mpergand said in Save QSet using QSettings:

    QVariant::load: unable to load type 1092

    You have to implement the corresponding stream operators see qRegisterMetaTypeStreamOperators

  • @SGaist
    Yes, I supposed something like that.
    That's strange, you can serialize a QSet as is with QDataStream, but not with QVariant !

  • @SGaist

    thanks, that's what i needed.

    this is how i did it. i set breakpoints to on operator << and >> to see how they work, but i can't spot the mistake. when i read the settings, i.e writeSettings() function, i get one more invalid entry read from in-stream. of course i can check its validity but i wanna understand where it comes from:

    void HighScores::readSettings() {
    	QSettings settings { m_sSettingsFile, QSettings::IniFormat };
    	const auto highScores = settings.value("highScores");
    		m_highScores = highScores.value<QVariantMultiMap_t>();
    		m_highScores = QVariantMultiMap_t {};
    void HighScores::writeSettings() const {
    	QSettings settings { m_sSettingsFile, QSettings::IniFormat };
    	settings.setValue("highScores", qVariantFromValue(m_highScores));
    QDataStream &operator<<(QDataStream &out, const HighScores::QVariantMultiMap_t &mmap) {
    	const auto mmapEnd = mmap.constEnd();
    	for(auto it = mmap.constBegin(); it != mmapEnd; ++it)
    		out << it.key() << it.value();
    	return out;
    QDataStream &operator>>(QDataStream& in, HighScores::QVariantMultiMap_t& mmap) {
    	QVariant key;
    	QString value;
    	while(!in.atEnd()) {
    		in >> key >> value;
    		mmap.insert(key, value);
    	return in;


    using QVariantMultiMap_t = QMultiMap<QVariant, QString>;
    QMultiMap<QVariant, QString> m_highScores;

    i've put Q_DECLARE_METATYPE(HighScoresPage::QVariantMultiMap_t) in header file after class declaration, and qRegisterMetaTypeStreamOperators<QVariantMultiMap_t>("QVariantMultiMap_t"); in constructor

    which part am i doing wrong?

  • I found a similar topic with a clear anwser :
    [](link url)

    I made a test with a QSet<QString>

    typedef QSet<QString>  QStringSet;
    int main(int argc, char *argv[])
        QString home=QDir::homePath();
        QString iniFileName="config.ini";
        QString iniPath=home+"/"+iniFileName;
        QSettings settings(iniPath,QSettings::IniFormat);
        QStringSet setIn;
        QVariant varIn;
        // save
        // load
        QVariant varOut=settings.value("Set");
        QStringSet setOut=varOut.value<QStringSet>();
    qDebug()<<"in "<<setIn;
    return 0;

    For QSet, no << operator needed, just to declare the type.
    All seems to work and the ini file looks good.

  • my code actually works, but not correctly

    the part that goes wrong in my code is in while loop in QDataStream &operator>>(QDataStream& in, HighScores::QVariantMultiMap_t& mmap). i check the stream using

    while(!in.atEnd()) {
        // read from stream 

    when i have, say, 1 element in the container, the saving of it in stream is done correctly. however, when reading from the stream, the execution enter the loop one time, and then one more extra time and i get an invalid element saved in container

    i tried with QStringSet but with this things are worse, writing to settings is okay, but reading always returns QVariant::Invalid:

    void UsernamePage::readSettings() {
    	QSettings settings { filePath, QSettings::IniFormat };
    	const auto usernames = settings.value("usernames"); // <-- returns Invalid
    		m_usernames = usernames.value<QStringSet_t>();
    		m_usernames = QStringSet_t {};
    void UsernamePage::writeSettings() const {
    	QSettings settings { filePath, QSettings::IniFormat };
    	settings.setValue("usernames", qVariantFromValue(m_usernames));
    QDataStream &operator<<(QDataStream &out, const ChooseUsernamePage::QStringSet_t &sset) {
    	const auto ssetEnd = sset.constEnd();
    	for(auto it = sset.constBegin(); it != ssetEnd; ++it)
    		out << *it;
    	return out;
    QDataStream &operator>>(QDataStream &in, ChooseUsernamePage::QStringSet_t &sset) {
    	QString username;
    	while(!in.atEnd()) {
    		in >> username;
    	return in;

  • If you are using QSet or QMap, you don't need to implement the << operator cause it's alreday the case. You only need to declare the type.

    Example with a QMap:

    typedef QMap<QString,QVariant>  Map;
        Map mapIn;
        QVariant varIn;
        // save
        // load
        Map mapOut=varOut.value<Map>();
    qDebug()<<"in "<<mapIn;


    in  QMap(("num", QVariant(int, 1000))("string", QVariant(QString, "Hello")))
    out QMap(("num", QVariant(int, 1000))("string", QVariant(QString, "Hello")))

    You only need to implement the stream ops for your custom classes.

    [edit] QMap is a wrong example because it's already managed by QVariant :)

    It's only needed fot QSet !

  • @mpergand i still get invalid qvariant :(

  • My Example of HighScores implementation.
    I'm using a custom class Gamer:

    struct Gamer
        QString name;
        int highScore;
    QDataStream &operator<<(QDataStream &out, const Gamer &gamer) {
        return out;
    QDataStream &operator>>(QDataStream &in, Gamer &gamer) {
        return in;
    // for qDebug logs
    QDebug operator<<(QDebug debug, const Gamer &gamer)
        debug<<<<", "<<gamer.highScore;
        return debug;

    Saving HighScores in a QList<Gamer>:

    using  HighScores=QList<Gamer>;
    Gamer g1{"John", 1200};
    Gamer g2{"Irvin", 2200};
    Gamer g3{"Lisa", 2140};
    HighScores scores;
    QVariant v; v.setValue(scores);
    // load

    Output of scores:

    ("John", 1200, "Irvin", 2200, "Lisa", 2140)

  • @mpergand that works but something's wrong with my code i guess?

  • i have a deadline so for now i'll use QStringList instead of QSet<Qstring>. after it i'll (hopefully) try to find what was the problem

Log in to reply