Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList find subclass class objects using qobject_cast
Forum Updated to NodeBB v4.3 + New Features

QList find subclass class objects using qobject_cast

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 971 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suths
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      }
      @

      ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved