How can i remove a value from a qlist?
-
Hi,
How can i remove a value from a QList< QMap<QString,QString> > test;?
i tried this : @test.removeOne(label);@ but i get an error
otherwise, how can i get the position of it to remove it?
label is a qstring.
-
Since your value of QList is of type QMap < QString, QString > you need to use such a type for using "removeOne()":http://developer.qt.nokia.com/doc/qt-4.8/qlist.html#removeOne .
So it is probably more convenient for you to use "removeAt()":http://developer.qt.nokia.com/doc/qt-4.8/qlist.html#removeAt -
I also tried this:
@test.removeOne("test_id",label_cbx);@
but i always get an error (no matching function)!!
-
Why should it match? You give two arguments to a method that takes one... That should make you wonder firsthand...
To remove an entry from a QList< QMap<QString,QString> >, that is a list of maps, you must construct a map or use a reference found elsewhere that denotes the map to remove from the list.
-
hm...what are you want to do with QList<QMap<QString, QString>>? Mb I can told you better idea.
-
As Volker already lined out.
However, I think this question is related to "your other post ":http://developer.qt.nokia.com/forums/viewthread/13330/ If this is really the case you are in a dead end anyhow.
To delete anything in your QList of QMaps with removeOne will be a bit painful, if you are not referencing. -
about delete you want to delete all map that contains key? or just key from map?
-
stima_ua, I want to remove a specific key of the map.
-
I would appreciate it if sb writes a small example to understand it better
-
@
#include <QMap>
#include <QList>#include <QDebug>
typedef QMap<QString, QString> Map;
void removeKey(QList<Map>& list, const QString& key)
{
int ret = 0;
QList<Map>::iterator i;
for ( i = list.begin(); i != list.end() && !ret; i++ )
{
ret = (*i).remove(key); // but only find first
}
}int main()
{
QList<Map> list;Map aMap; Map bMap; aMap["aKey1"] = "aValue1"; aMap["aKey2"] = "aValue2"; bMap["bKey1"] = "bValue1"; bMap["bKey2"] = "bValue2"; list.append(aMap); list.append(bMap); qDebug() << list; removeKey(list, "aKey1"); qDebug() << list; return 0;
}
@