Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Error "use of undeclared identifier 'qmlRegisterType'"
QtWS25 Last Chance

Error "use of undeclared identifier 'qmlRegisterType'"

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 7 Posters 16.1k Views
  • 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 Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      Guigui
      wrote on last edited by
      #2

      qmlRegisterType() is defined in the QQmlEngine class.

      Try adding @#include <QQmlEngine>@

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            Guigui
            wrote on last edited by
            #5

            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
            0
            • S Offline
              S Offline
              stereomatching
              wrote on last edited by
              #6

              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
              0
              • S Offline
                S Offline
                stefan0x53
                wrote on last edited by
                #7

                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
                0
                • S Offline
                  S Offline
                  soleyla
                  wrote on last edited by
                  #8

                  ...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
                  0
                  • B Offline
                    B Offline
                    beanhead
                    wrote on last edited by
                    #9

                    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
                    0
                    • jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #10

                      [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
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved