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

qmltc: require required properties in the generated constructors

    XMLWordPrintable

Details

    • Task
    • Resolution: Won't Do
    • P3: Somewhat important
    • 6.x
    • None
    • QML: Compiler

    Description

      Qmltc does not emit any errors or warnings on qml files where the root component has required properties:

       

      // TypeWithRequiredProperty.qml
      import QtQuick
      
      Item {
          required property int myProperty
      }
      
      // This file cannot be compiled with qmltc because the required property is missing
      // alwaysBadFile.qml
      import MyModule
      import QtQuick
      
      Item {
          TypeWithRequiredProperty {
              id: bad
          }
      }
      
      // This file can be compiled (but without neither warning nor error, while a QQmlComponent::create() would return nullptr if myProperty is still not set at runtime)
      // sometimesBadFile.qml
      import MyModule
      import QtQuick
      
      TypeWithRequiredProperty {
       id: sometimesBad
      }

       

       

      Where

      QQmlComponent::create()

      would return an error during runtime if

      QQmlComponent::setInitialProperties()

      was not called with the QVariantMap nameOfRequiredProperties -> values,

       

      the class generated by qmltc can be initialized with the default empty constructor

      sometimesBadFile created();

      and does not warn that a required property is missing (until much later when that property is used for something and just contains its default value).

       

       

      Therefore, qmltc should stop generating the default empty constructors for types with required properties and instead generate a constructor like

       

      sometimesBadFile::sometimesBadFile(int myProperty);

      or

       

       

      sometimesBadFile::sometimesBadFile(const QPropertyBinding<int>& myProperty);

      when the property is bound to a value, such that setBinding() can be called on the required property.

       

      Note 1: which of these overloads should be accepted? (see Qbindable::setBinding() overloads)

      sometimesBadFile::sometimesBadFile(const QPropertyBinding<int>& myProperty);
       
      sometimesBadFile::sometimesBadFile(const QUntypedPropertyBinding& myProperty);
       
      template <typename Functor>
      sometimesBadFile::sometimesBadFile(
          Functor &&f,
          const QPropertyBindingSourceLocation &location =
               QT_PROPERTY_DEFAULT_BINDING_LOCATION,
           std::enable_if_t<std::is_invocable_v<Functor>> * = nullptr);
      

       

      Note 2: once one overload is chosen, how should the combinatorial explosion be handled (e.g. for n required properties there are 2^n possible constructors if every property can be passed in as a constant value or as a binding for the bindable). Maybe using something like a

      std::variant<QPropertyBinding<int>, int>

      might help?

       

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-105896
          # Subject Branch Project Status CR V

          Activity

            People

              sami.shalayel Sami Shalayel
              sami.shalayel Sami Shalayel
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes