Uploaded image for project: 'Qt Creator'
  1. Qt Creator
  2. QTCREATORBUG-26159

"bar" does not have members

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reported
    • Priority: Not Evaluated
    • Resolution: Unresolved
    • Affects Version/s: Qt Creator 4.15.2
    • Fix Version/s: None
    • Component/s: Quick / QML Support
    • Labels:
      None
    • Environment:
      MinGw64
    • Platform/s:
      Windows

      Description

      Problem:

      I tried to use the example for template <typename T> int qmlRegisterAnonymousType(const char *uri, int versionMajor)

      https://doc.qt.io/qt-5/qqmlengine.html

       

      Error message: 

      Qt Creator returns the error "bar" does not have members

       

      Here is the full code:

       

      bar.h

      #ifndef BAR_H
      #define BAR_H
      
      #include <QObject>
      
      class Bar : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString baz READ baz WRITE setBaz NOTIFY bazChanged)
      
      public:
          Bar() {}
      
          QString baz() const { return mBaz; }
      
          void setBaz(const QString &baz)
          {
              if (baz == mBaz)
                  return;
      
              mBaz = baz;
              emit bazChanged();
          }
      
      signals:
          void bazChanged();
      
      private:
          QString mBaz;
      };
      
      
      #endif // BAR_H
      
      

      foo.h

       

      #ifndef FOO_H
      #define FOO_H
      #include <QObject>
      #include "bar.h"
      
      
      class Foo : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(Bar *bar READ bar CONSTANT FINAL)
      
      public:
          Foo() {}
      
          Bar *bar() { return &mBar; }
      
      private:
          Bar mBar;
      };
      #endif // FOO_H
      
      

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      
      #include "foo.h"
      #include "bar.h"
      
      int main(int argc, char *argv[])
      {
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
      
          QGuiApplication app(argc, argv);
      
      
          QQmlApplicationEngine engine;
      
          qmlRegisterType<Foo>("App", 1, 0, "Foo");
          qmlRegisterAnonymousType<Bar>("App", 1);
      
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      

      main.qml

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import App 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Foo {
              bar.baz: "abc"
              Component.onCompleted: print(bar.baz)
          }
      
      }
      
      

       

        Attachments

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

          Activity

            People

            Assignee:
            fawzi Fawzi Mohamed
            Reporter:
            shazter Dirk Büttner
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes