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

QMetaType enable registerConverter for built in types

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Won't Do
    • Not Evaluated
    • None
    • None
    • None

    Description

      The registerConverter function fails when I am trying extend the core conversion(s).

      The fail is caused by the static assert. Commit: f7b313e6d86
      "QMetaType::registerConverter: At least one of the types must be a custom type."

      My proposal:
      Allow to extend the conversions.
      Pre-check before registration with: QMetaType::hasRegisteredConverterFunction

       

       

          // member function as in "QString QFont::toString() const"
          template<typename From, typename To>
          static bool registerConverter(To(From::*function)() const)
          {
              bool registerConverterResult = false;
      
              const int fromTypeId = qMetaTypeId<From>();
              const int toTypeId = qMetaTypeId<To>();
              const bool hasRegisteredConverterFunction = QMetaType::hasRegisteredConverterFunction(fromTypeId, toTypeId);
      
              if (hasRegisteredConverterFunction == true)
              {
                  static const QtPrivate::ConverterMemberFunction<From, To> f(function);
                  registerConverterResult = QMetaType::registerConverterFunction(&f, fromTypeId, toTypeId);
              }
      
              return registerConverterResult;
          }
      
          // member function as in "double QString::toDouble(bool *ok = nullptr) const"
          template<typename From, typename To>
          static bool registerConverter(To(From::*function)(bool*) const)
          {
              bool registerConverterResult = false;
      
              const int fromTypeId = qMetaTypeId<From>();
              const int toTypeId = qMetaTypeId<To>();
              const bool hasRegisteredConverterFunction = QMetaType::hasRegisteredConverterFunction(fromTypeId, toTypeId);
      
              if (hasRegisteredConverterFunction == true)
              {
                  static const QtPrivate::ConverterMemberFunction<From, To> f(function);
                  registerConverterResult = QMetaType::registerConverterFunction(&f, fromTypeId, toTypeId);
              }
      
              return registerConverterResult;
          }
      
          // functor or function pointer
          template<typename From, typename To, typename UnaryFunction>
          static bool registerConverter(UnaryFunction function)
          {
              bool registerConverterResult = false;
      
              const int fromTypeId = qMetaTypeId<From>();
              const int toTypeId = qMetaTypeId<To>();
      
              const bool hasRegisteredConverterFunction = QMetaType::hasRegisteredConverterFunction(fromTypeId, toTypeId);
      
              if (hasRegisteredConverterFunction == true)
              {
                  static const QtPrivate::ConverterFunctor<From, To, UnaryFunction> f(function);
                  registerConverterResult = QMetaType::registerConverterFunction(&f, fromTypeId, toTypeId);
              }
      
              return registerConverterResult;
          }
      

       

      My conversion were the following ones:

      QString -> QBitArray
      QBitArray -> QString

      OR

      QByteArray-> QBitArray
      QBitArray -> QByteArray
       

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            szendvics Marcell Varga
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes