QList find subclass class objects using qobject_cast



  • Hi All,

    It's getting late and my brain has stopped working, so apologies if this is a stupid question.

    If I have a qlist<QObject*> containing a load of object that have been inherited from QObject (and have the Q_OBJECT macro), what would be the best way to construct a find operation that would return a list of a particular base class type.

    E.g A function prototype could look like :
    @QList<T> find(T type)@

    Can I intergrate it into a iterator?


  • Lifetime Qt Champion

    Hi,

    Do you mean

    @
    template<T>
    QList<T > find(QList<QObject> inputList)
    {
    QList<T *> objectList;
    foreach (QObject *inputObject, inputList) {
    T *object = qobject_cast<T *>(inputObject);
    if (object) {
    objectList << object;
    }
    }
    return objectList
    }
    @

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.