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 getQMap<QString, QVariant>back from the result. Either buildQMap<QString, QVariant>in parseFile and put that into the variant, or useQVariant::valueto extract the correct type on the caller side, i.e.: