Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-120541

Add template <T...> QList<QObject *> QObject::findChildren() overload

XMLWordPrintable

    • Icon: Suggestion Suggestion
    • Resolution: Unresolved
    • Icon: Not Evaluated Not Evaluated
    • None
    • 6.8
    • Core: Object Model
    • None
    • All

      When there is a need to find children of many types in a row, it would be handy to have just one function, like:

      template <T...>
      QList<QObject *> QObject::findChildren() const;
      

      that would return a QList<QObject *> (instead of QList<T>) for a passed list of children types T....

      Motivation:

      1. This would be more optimal to get the common list during just one traversal.
      2. This would be more handy to have all results in one common list and the code repetition could be easily avoided.

      Currently, one needs to do:

      const QList<QComboBox *> comboChildren = widget->findChildren<QComboBox *>();
      for (auto child : comboChildren) {
          // process child
      }
      
      const QList<QAbstractSpinBox *> spinChildren = widget->findChildren<QAbstractSpinBox *>(); // 2nd lookup
      for (auto child : spinChildren) { // code repetition
          // process child
      }
      
      // Can't easily merge comboChildren and spinChildren lists into one common list, since they are of different types.
      

      Use case: setWheelScrollingWithoutFocusBlockedForChildren in https://codereview.qt-project.org/c/qt-creator/qt-creator/+/512132


      Possible additional feature: Make the return type of the QList<T> customizable. E.g. in the example above, we might want to get the QList<QWidget *> as a return type. This might be an additional template parameter type. Alternatively, maybe it could be automatically deduced as a common denominator of all types passed as T... template parameter

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            thiago Thiago Macieira
            jkobus Jarek Kobus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes