if you need a pointer to the element you could use std::find_if:
#include <QCoreApplication>
#include <functional>
#include <QDebug>
#include <QString>
class SomeClass
{
public:
QString key;
QString someData;
int otherField;
SomeClass(QString key, QString someData, int otherField)
{
this->key = key;
this->someData = someData;
this->otherField = otherField;
}
QString getKey() { return key; }
};
void print(QList<SomeClass*>& list)
{
for(auto* someclass : list) {
qDebug() << someclass->key << someclass->someData << someclass->otherField;
}
qDebug() << "";
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// create list
QList<SomeClass*> list {
new SomeClass("first", "first_data", 100),
new SomeClass("mykey1", "second_data", 100)
};
// print
print(list);
// search for element and if found change data
auto itr = std::find_if(list.begin(), list.end(), [](SomeClass* someclass) { return someclass->getKey() == "mykey1"; });
if(itr != list.end()) {
(*itr)->someData = "NEW";
}
// print
print(list);
return app.exec();
}
Prints:
"first" "first_data" 100
"mykey1" "second_data" 100
"first" "first_data" 100
"mykey1" "NEW" 100