I have a function, which parses the file and fills a QMap<QString, int> with entries and returns a QVariant with value of that map:
QVariant FileParser::parseFile(QString filePath)
{
QMap<QString, int> toReturn;
... (parse the file and fill the map)
return QVariant::fromValue(toReturn);
}
When i check the contents of the map after calling it, i see an empty map:
QMap<QString, QVariant> words = FileParser().parseFile(QCoreApplication::applicationDirPath() + "/Input.txt").toMap();
qDebug()<<words; //Gives QMap()
This function works fine if i return QMap<QString, int>, but wrapping it in QVariant gives an empty map. Why might that be?
There is a mismatch where you put QMap<QString, int>
into the variant, but tried to get QMap<QString, QVariant>
back from the result. Either build QMap<QString, QVariant>
in parseFile and put that into the variant, or use QVariant::value
to extract the correct type on the caller side, i.e.:
qDebug() << words.value<QMap<QString, int>>();