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

Unable to reset properties in gadgets

    XMLWordPrintable

Details

    • 1b6069798a (qt/qtdeclarative/dev)

    Description

      QQmlBinding::slowWrite() does not check if MyGadget::value is resetable, but only if MyObject::gadget is: http://code.qt.io/cgit/qt/qtdeclarative.git/tree/src/qml/qml/qqmlbinding.cpp#n339

      #include <QtQml>
      
      class MyGadget
      {
          Q_GADGET
          Q_PROPERTY(int value READ value WRITE setValue RESET resetValue)
      
      public:
          int value() const { return m_value; }
          void setValue(int value) { m_value = value; }
          void resetValue() { setValue(-1); }
      
          bool operator==(const MyGadget &other) const { return m_value == other.m_value; }
          bool operator!=(const MyGadget &other) const { return m_value != other.m_value; }
      
      private:
          int m_value = -1;
      };
      
      class MyObject : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(MyGadget gadget MEMBER m_gadget NOTIFY gadgetChanged)
      
      public:
          MyObject(QObject *parent = nullptr) : QObject(parent) { }
      
      signals:
          void gadgetChanged();
      
      private:
          MyGadget m_gadget;
      };
      
      QML_DECLARE_TYPE(MyObject)
      Q_DECLARE_METATYPE(MyGadget)
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
      
          qRegisterMetaType<MyGadget>();
          qmlRegisterType<MyObject>("MyObject", 1, 0, "MyObject");
      
          QQmlEngine engine;
          QQmlComponent component(&engine);
          component.setData("import QtQml 2.0; import MyObject 1.0; MyObject { gadget.value: undefined }", QUrl());
          if (!component.create())
              qDebug() << component.errorString(); // <Unknown File>:1:65: Unable to assign [undefined] to int
          return component.status() == QQmlComponent::Ready;
      }
      
      #include "main.moc"
      

      Notice that if MyObject::gadget is resetable, MyObject::resetGadget() gets called and thus it resets the whole grouped property.

      class MyObject : public QObject
      {
           ...
           Q_PROPERTY(MyGadget gadget MEMBER m_gadget RESET resetGadget NOTIFY gadgetChanged)
          void resetGadget() { qDebug("FAIL"); }
          ...
      };
      

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            jpnurmi J-P Nurmi
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes