Utils: add filter and transform function for qobject container

Add function that casts QObjects of a given container to a desired type
and return the result via the same container with the desired value
type.

Change-Id: I45949c982c8dbdd0cce069d62cd2db5c8517f9f5
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
David Schulz
2017-11-14 09:26:38 +01:00
parent b577db56b6
commit 2453d20dd0

View File

@@ -32,6 +32,7 @@
#include <algorithm>
#include <tuple>
#include <QObject>
#include <QStringList>
namespace Utils
@@ -371,6 +372,21 @@ C filteredUnique(const C &container)
return result;
}
//////////////////
// qobject_container_cast
/////////////////
template <class T, template<typename> class Container, typename Base>
Container<T> qobject_container_cast(const Container<Base> &container)
{
Container<T> result;
auto ins = inserter(result);
for (Base val : container) {
if (T target = qobject_cast<T>(val))
ins = target;
}
return result;
}
//////////////////
// sort
/////////////////