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

QML and nullable "primitive" types

    XMLWordPrintable

Details

    Description

      Let's say I want return `std::optional<int>` or `std::optional<QColor>` or `std::optional<QString>` from C++ to QML via `Q_INVOKABLE` function.

      I see only one option to do it now: usage of ​`QVariant()` and `QVariant(T)`.
      In other words it would be dynamic typing: ​type `T` or invalid type.

      I can define new types like `OptionalT`:

      struct OptionalT {
         Q_GADGET
         bool isValid;
         T data;
         Q_PROPERTY(...)
      };
      

      because of `Q_GADGET` not supported as return type for `Q_INVOKABLE` (https://bugreports.qt.io/browse/QTBUG-79330).

      The propblem that on QML type become `var`, and qmllint can not reports any warning for code like this:

              const depTime = obj.getDepartureTime()
              if (depTime) {
                  return depTime.toLocalString(Qt.locale(), "hh:mm\nt") // <-- here error, no "e" on Locale
              } else
                  return qsTr("Departure time")
      

      It would be nice if there is option to annotate nullable types on C++/QML border.

      Attachments

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

        Activity

          People

            qtqmlteam Qt Qml Team User
            davemilter davemilter
            Votes:
            4 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes