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

Improve consistency between objects that are instantiated by QML vs. qmltc/C++

    XMLWordPrintable

Details

    Description

      Code
      // TestObject.qml

      import QtQuick
      
      QtObject {
          property int qmlNnumber: 42
      }
      

       

      // Main.qml

      import QtQuick
      
      Window {
          width: 640
          height: 480
          visible: true
      
          TestObject { objectName: "qmlObject" }
      }
      

       

      // main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "testobject.h"
      
      static void reportProperties(QObject* testObj)
      {
          qDebug() << "#####";
          qDebug() << testObj;
          qDebug() << "#####";
      
          auto metaObj = testObj->metaObject();
          for (int i = 0; i < metaObj->propertyCount(); ++i) {
              QMetaProperty prop = metaObj->property(i);
              qDebug() << "PROPERTY" << prop.name();
              qDebug() << "\tValue:" << prop.read(testObj);
              qDebug() << "\tBindable:" << prop.isBindable();
              qDebug() << "\thasNotifySignal:" << prop.hasNotifySignal();
          }
      }
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          QObject::connect(
              &engine,
              &QQmlApplicationEngine::objectCreationFailed,
              &app,
              []() { QCoreApplication::exit(-1); },
              Qt::QueuedConnection);
          engine.loadFromModule("PropertyStudy", "Main");
      
          reportProperties( engine.rootObjects().first()->findChild<QObject*>("qmlObject") );
          reportProperties( new PropertyStudy::TestObject(&engine));
      
          return app.exec();
      }
      

       

      Output

      #####
      TestObject_QMLTYPE_1(0x1b9bf750dd0, name = "qmlObject")
      #####
      PROPERTY objectName
          Value: QVariant(QString, "qmlObject")
          Bindable: true
          hasNotifySignal: true
      PROPERTY qmlNnumber
          Value: QVariant(int, 42)
          Bindable: false
          hasNotifySignal: true
      #####
      PropertyStudy::TestObject(0x1b9bf728b80)
      #####
      PROPERTY objectName
          Value: QVariant(QString, "")
          Bindable: true
          hasNotifySignal: true
      PROPERTY qmlNnumber
          Value: QVariant(int, 42)
          Bindable: true
          hasNotifySignal: false
      
      • The two objects are of different types
      • For the QML-instantiated object, "qmlNumber" is not bindable and has a notify signal
      • For the C++-instantiated object, "qmlNumber" is bindable and has no notify signal

      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
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes