#ifndef QOBJECTLISTCAST_H #define QOBJECTLISTCAST_H #include template /** * @brief Statically cast a QObjectList (aka QList) to a QList of any type (must be a QObject derivative) * * Best used to convert the list from a QObject's children() list to a list of another type, assuming it's known that * all children are of a certain type. * * As a static cast, the objects in the QObjectList must all be of the SAME type or the behavior * is undefined. */ QList static_qobjectlist_cast(const QObjectList& list) { QList new_list; new_list.reserve(list.size()); for (int i=0;i(list.at(i))); } return new_list; } template /** * @brief Dynamically cast a QObjectList (aka QList) to a QList of any type (must be a QObject derivative) * * Best used to convert the list from a QObject's children() list to a list of another type, assuming it's known that * all children are of a certain type. * * Unlike static_qobjectlist_cast(), not all of the objects must be of the same type. Objects that are of a different * type are not added to the list. Therefore it is guarantee the list will contain valid objects of the type you * specify (or an empty list if there are none). */ QList dynamic_qobjectlist_cast(const QObjectList& list) { QList new_list; new_list.reserve(list.size()); for (int i=0;i(list.at(i)); // Test cast (casted_obj will be nullptr if the dynamic_cast fails) if (casted_obj != nullptr) { new_list.append(casted_obj); } } return new_list; } #endif // QOBJECTLISTCAST_H