Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Error "use of undeclared identifier 'qmlRegisterType'"

    QML and Qt Quick
    7
    10
    15183
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stereomatching last edited by

      The compiler always complain "use of undeclared identifier 'qmlRegisterType'"
      Weird, but I can't find out why

      .pro

      @
      QT += qml quick

      SOURCES +=
      main.cpp
      fileReader.cpp

      HEADERS +=
      fileReader.hpp

      @

      fileReader.hpp

      @
      #ifndef FILEREADER_HPP
      #define FILEREADER_HPP

      #include <QObject>

      class FileReader : public QObject
      {
      Q_OBJECT
      public:
      explicit FileReader(QObject *parent = 0);

      Q_INVOKABLE QString read_file&#40;QString const &fileName&#41;;
      

      };

      #endif // FILEREADER_HPP
      @

      fileReader.cpp

      @
      #include <QDir>
      #include <QFile>
      #include <QFileInfo>
      #include <QTextStream>

      #include "fileReader.hpp"

      FileReader::FileReader(QObject *parent) :
      QObject(parent)
      {
      }

      QString FileReader::read_file(QString const &fileName)
      {
      QString content;
      QFile file(fileName);
      if (file.open(QIODevice::ReadOnly)) {
      QTextStream stream(&file);
      content = stream.readAll();
      }
      return content;
      }
      @

      main.cpp

      @
      #include <QtQuick/QQuickView>
      #include <QGuiApplication>

      #include "fileReader.hpp"

      int main(int argc, char *argv[])
      {
      QGuiApplication app(argc, argv);

      qmlRegisterType<FileReader>("File", 1, 0, "FileReader");
      
      return app.exec(&#41;;
      

      }

      @

      Almost same as the example of PieChart, the weird thing is
      the codes work under the PieChart example but can't work under
      the new project I open

      compiler : clang 3.2
      os : mac osx 10.8.3
      Qt: 5.1Beta

      1 Reply Last reply Reply Quote 0
      • G
        Guigui last edited by

        qmlRegisterType() is defined in the QQmlEngine class.

        Try adding @#include <QQmlEngine>@

        1 Reply Last reply Reply Quote 0
        • S
          stereomatching last edited by

          added in main.cpp

          @
          #include <QtQuick/QQuickView>
          #include <QGuiApplication>
          #include <QQmlEngine>

          #include "fileReader.hpp"

          int main(int argc, char *argv[])
          {
          QGuiApplication app(argc, argv);

          qmlRegisterType<FileReader>("File", 1, 0, "FileReader");
          
          return app.exec(&#41;;
          

          }

          @

          pop out same error message(exactly, I tried it before)

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Isn't qmlRegisterType declared in QDeclarativeEngine ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • G
              Guigui last edited by

              It depends on the Qt version you're using.

              According to "this":http://qt-project.org/doc/qt-5.0/qtquick/qtquick-porting-qt5.html#c-code document:

              • QDeclarativeEngine for Qt 4.7/4.8
              • QQmlEngine for Qt 5

              Should be QQmlEngine in this case.

              1 Reply Last reply Reply Quote 0
              • S
                stereomatching last edited by

                After adding "#include <QtDeclarative/QDeclarativeEngine>" into the main.cpp, can't work either.

                Edit:
                I found the problem, I should include
                "#include <QtQuick/QQuickPaintedItem>"
                Else the codes can not compile, I didn't expect this is needed
                for qmlRegisterType, is this a correct solution?Is this a bug?
                Qt team forget to include some headers?

                1 Reply Last reply Reply Quote 0
                • S
                  stefan0x53 last edited by

                  This is still valid in 5.2
                  However, I found that implementation lies in "qqml.h" header.
                  I added both to be on the safe side:
                  @#include <QtQml/QQmlEngine>
                  #include <qqml.h>@

                  1 Reply Last reply Reply Quote 0
                  • S
                    soleyla last edited by

                    ...and also valid in 5.3

                    I couldn't solve the problem by including QDeclarativeEngine or QQmlEngine, although I added declarative and qml to my .pro-file. Including QQuickPaintedItem or QQuickItem solved the problem. In the end I decided to add

                    @#include <QtQml>@

                    1 Reply Last reply Reply Quote 0
                    • B
                      beanhead last edited by

                      as soleyla suggested, I tried:
                      @#include <QtQml>@

                      For me, this worked without having to make any of the other changes listed above in the previous posts...

                      1 Reply Last reply Reply Quote 0
                      • jeremy_k
                        jeremy_k last edited by

                        [quote author="beanhead" date="1411234589"]as soleyla suggested, I tried:
                        @#include <QtQml>@

                        For me, this worked without having to make any of the other changes listed above in the previous posts...[/quote]

                        That's the intended route.

                        The documentation is confusing. The various incarnations of qmlRegisterType are documented as related non-members of QQmlEngine (For Qt 5.3 anyway). Searching and then scrolling to the top of the page leads to the impression that <QQmlEngine> in the correct header file.

                        Some of the qmlRegister* function documentation snippets do mention including <QtQml>. It is easy to overlook, and violates the usual pattern.

                        Asking a question about code? http://eel.is/iso-c++/testcase/

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post