QMap or QHash doesn't return proper value
-
Hi,
In my MainWindow.cc, I have thisstruct Item { Item(QString path, QString type) : _path(path), _type(type) {} QString _path, _type; }
QMap<QString, Item> _myDB;
I'm also populating _myDB in the same file using some method called push.
push(_search, MainWindow::Item(localPath, localType));
This push method internally calls insert of QMap.
In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.
QVariant ModelTable::data(const QModelIndex& index, int role) const { Item o = _myDB.value(search); // here seems to be an issue. switch (o._type) { case "EE" : return o._path; break; case "FE" : return o._path; break; default: break; } }
However, I always get an empty string when I do QMap.value(key).
What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
QMap searching is somehow not working as expected. -
@jsulm
_myDB.contains(search) is returning me empty. That is the problem.
But the search string exists. It is for sure.This line is not working: Item o = _myDB.value(search);
@vijaychsk what is the output of
qDebug() << search << _myDB.keys();
? -
Hi,
In my MainWindow.cc, I have thisstruct Item { Item(QString path, QString type) : _path(path), _type(type) {} QString _path, _type; }
QMap<QString, Item> _myDB;
I'm also populating _myDB in the same file using some method called push.
push(_search, MainWindow::Item(localPath, localType));
This push method internally calls insert of QMap.
In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.
QVariant ModelTable::data(const QModelIndex& index, int role) const { Item o = _myDB.value(search); // here seems to be an issue. switch (o._type) { case "EE" : return o._path; break; case "FE" : return o._path; break; default: break; } }
However, I always get an empty string when I do QMap.value(key).
What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
QMap searching is somehow not working as expected.@vijaychsk I did not reproduce the error:
#include <QMap> #include <QDebug> struct Item { Item(const QString & path="", const QString & type="") : _path(path), _type(type) {} QString _path, _type; }; QDebug operator<<(QDebug debug, const Item &item) { QDebugStateSaver saver(debug); debug.nospace() << '(' << item._path << ", " << item._type << ')'; return debug; } int main() { QMap<QString, Item> _myDB; _myDB["Foo"] = Item("foopath", "footype"); _myDB["Bar"] = Item("barpath", "bartype"); _myDB["Baz"] = Item("bazpath", "baztype"); qDebug() << _myDB.value("Foo"); qDebug() << _myDB.value("Bar"); qDebug() << _myDB.value("Baz"); }
Output:
("foopath", "footype") ("barpath", "bartype") ("bazpath", "baztype")
-
@vijaychsk I did not reproduce the error:
#include <QMap> #include <QDebug> struct Item { Item(const QString & path="", const QString & type="") : _path(path), _type(type) {} QString _path, _type; }; QDebug operator<<(QDebug debug, const Item &item) { QDebugStateSaver saver(debug); debug.nospace() << '(' << item._path << ", " << item._type << ')'; return debug; } int main() { QMap<QString, Item> _myDB; _myDB["Foo"] = Item("foopath", "footype"); _myDB["Bar"] = Item("barpath", "bartype"); _myDB["Baz"] = Item("bazpath", "baztype"); qDebug() << _myDB.value("Foo"); qDebug() << _myDB.value("Bar"); qDebug() << _myDB.value("Baz"); }
Output:
("foopath", "footype") ("barpath", "bartype") ("bazpath", "baztype")
@eyllanesc
On search of QMap, we are getting a value of type Item.
Can you try assign the contents of the Item and try to print them?QString output; output = _myDB.value("foo")._path; std::cout << output.toStdString() << std::endl;
In my code, I'm not using qdebug but doing assignment. Perhaps there might be some issue there.
-
Hi,
In my MainWindow.cc, I have thisstruct Item { Item(QString path, QString type) : _path(path), _type(type) {} QString _path, _type; }
QMap<QString, Item> _myDB;
I'm also populating _myDB in the same file using some method called push.
push(_search, MainWindow::Item(localPath, localType));
This push method internally calls insert of QMap.
In the ModelTable.cc, I'm trying to access the _myDB that was already populated. I just want to search and display the results in this file.
QVariant ModelTable::data(const QModelIndex& index, int role) const { Item o = _myDB.value(search); // here seems to be an issue. switch (o._type) { case "EE" : return o._path; break; case "FE" : return o._path; break; default: break; } }
However, I always get an empty string when I do QMap.value(key).
What am I doing wrong? Why does QMap doesn't return as expected? It is guaranteed that _myDB has some values and my search will always have a result. During the push operation, I can see what is being inserted. But when accessing the data(...), the results aren't as expected. I also tried using QMap<QString, QPair<QString, QString> instead of a struct, still the same issue.
QMap searching is somehow not working as expected.@vijaychsk said in QMap or QHash doesn't return proper value:
Item o = _myDB.value(search);
Are you sure there is such key in your _myDB?
What does _myDB.contains(search) return?
value() returns a default constructed value if there is no such key. -
@vijaychsk said in QMap or QHash doesn't return proper value:
Item o = _myDB.value(search);
Are you sure there is such key in your _myDB?
What does _myDB.contains(search) return?
value() returns a default constructed value if there is no such key. -
@vijaychsk said in QMap or QHash doesn't return proper value:
Item o = _myDB.value(search); // here seems to be an issue.
Where does "search" come from? It is not a parameter to the function, and it is not the same as "_search" that you used when you "push" an item onto the map. Is its value what you expect?
QMap does not have a push() function. Are you sure that what you implemented as push() is doing what you think?
-
@jsulm
_myDB.contains(search) is returning me empty. That is the problem.
But the search string exists. It is for sure.This line is not working: Item o = _myDB.value(search);
@vijaychsk what is the output of
qDebug() << search << _myDB.keys();
? -
@jsulm
_myDB.contains(search) is returning me empty. That is the problem.
But the search string exists. It is for sure.This line is not working: Item o = _myDB.value(search);
@vijaychsk said in QMap or QHash doesn't return proper value:
_myDB.contains(search) is returning me empty.
What do you mean by "empty"? contains() returns a boolean. So, if it returns false then the map does not contain this key.
Print out all keys returned by https://doc.qt.io/qt-5/qmap.html#keys.
Do you by any chance have more than one QMap<QString, Item> in your code?