Error "use of undeclared identifier 'qmlRegisterType'"



  • 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



  • qmlRegisterType() is defined in the QQmlEngine class.

    Try adding @#include <QQmlEngine>@



  • 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)


  • Lifetime Qt Champion

    Hi,

    Isn't qmlRegisterType declared in QDeclarativeEngine ?



  • 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.



  • 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?



  • 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>@



  • ...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>@



  • 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 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.