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

Add qVar() global function for creating QVariants

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • 4.0.0
    • None
    • Core: Other
    • None

    Description

      QVariant can hold a value of (almost) any type, given the requirements are met, but there is no easy, universal way of creating a QVariant of any value.

      The value is either basic type, and Qt has a easy, non-explicit c-tor for it.

      or

      The value is of custom type and the user must use the clunky and verbose QVariant::fromValue().

       

      My suggestion is to add a simple function "qVar()" to be able to easily create a qvariant from any value with a simple and concise syntax.

      Usage:

      auto var = isCustom ? qVar(getCustom()) : qVar(getStandard());
      

      Usage: 

      QVariant getValue(Data data, Type type)
      {
        switch(type)
        {
        case type_int:
          return data.someIntValue();
        case type_string:
          return data.someStringValue();
        case type_custom:
          return qVar(data.someCustomValue());
        }
      }

       

      The implementation is trivial:

      template<class T>
      QVariant qVar(T&& val)
      {
        return QVariant::fromValue(std::forward<T>(val));
      }

       

      One interesting side effect is that the user can override the function for his own type:

      QVariant qVar(const Custom& c)
      {
       // magic
      }

       

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            mnaydenov Mihail Naydenov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes