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

Consider adding an iterable API for types in QVariant

    XMLWordPrintable

Details

    • 01fb843af88d949cd38b494a60bb64b730a045d2

    Description

      Sometimes a QVariant can contain a container, such as QList<int>. Methods taking the QVariant as an argument may want to iterate over the content without knowing exactly which type of container it is, or what its element_type is (as long as that element_type is also defined as a metatype). Both QtDBus and QtScript have particular systems for doing this. Something in QtCore might be suitable. It might also have consequences for the (currently awkward QDeclarativeListProperty class)

      One proposal would be to have API and function like this:

      int main(int argc, char **argv)
      {
      QList<QVector<int> > listVec;

      { QVector<int> myVector; myVector << 3 << 7 << 8; listVec << myVector; } { QVector<int> myVector; myVector << 6 << 5 << 4; listVec << myVector; }

      QVariant var = QVariant::fromValue(listVec);
      qDebug() << "The variant" << var;

      QVariantIterator itr = var.begin();
      QVariantIterator end = var.end();
      while (itr != end)
      {
      qDebug() << "List element" << *itr;

      QVariantIterator itr2 = (*itr).begin();
      QVariantIterator end2 = (*itr).end();
      while (itr2 != end2)

      { qDebug() << "Vector element" << *itr2; ++itr2; }

      ++itr;
      }
      }

      // Output:
      // The variant QVariant(QList<QVector<int> >, )
      // List element QVariant(QVector<int>, )
      // Vector element QVariant(int, 3)
      // Vector element QVariant(int, 7)
      // Vector element QVariant(int, 8)
      // List element QVariant(QVector<int>, )
      // Vector element QVariant(int, 6)
      // Vector element QVariant(int, 5)
      // Vector element QVariant(int, 4)

      That would involve adding typedefs and function pointers to QVariant::Handler for getting begin and end iterators in the container and for getting a particular element at a position.

      An alternative API might be possible without putting the begin() and end() methods on the QVariant class.

      Attachments

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

        Activity

          People

            nierob Nierob
            stephen.kelly Stephen Kelly (Unused account) (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes