QmlRegisterType, Qt5.1 and QtQuick2



  • Hi.

    I am new to Qt, and have trouble using qmlRegisterType. I simply cannot get it working!

    What I am trying to do:
    Use Qml and c++
    Have an object created in c++ and have this binded to the GUI (my goal is a list of objects, but starting simple)

    I'm following the Qt docs:

    System Info:
    Windows 8 64 bit
    Installed: "Qt 5.1.0 for Windows 64-bit (VS 2012, 525 MB)" from here "http://qt-project.org/downloads"

    What I've done:

    Started a new project using the "Qt Quick 2 Application (Built-in Elements)" template

    Added the Timer class (from first doc)

    Changed the main.cpp (from first doc)

    As you can see in the main.cpp I've out commented 3 lines. Without these the application starts fine. When I added the line
    @qmlRegisterType<Timer>("CustomComponents", 1, 0, "Timer");@

    i got compiler error: C2065: 'qmlRegisterType' : undeclared identifier.
    After searching the forum i found this link http://qt-project.org/forums/viewthread/28488 where the solution was to include QQuickPaintedItem. So I added this and QQmlEngine which should be the correct one according to QtDocs for version 5.1.

    This lead me to the following errors, where I am now stuck. The solution is probably simple but I just cannot find it!

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Timer::metaObject(void)const " (?metaObject@Timer@@UEBAPEBUQMetaObject@@XZ)

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl Timer::qt_metacast(char const *)" (?qt_metacast@Timer@@UEAAPEAXPEBD@Z)

    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl Timer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Timer@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Timer::Timer(class QObject *)" (??0Timer@@QEAA@PEAVQObject@@@Z) referenced in function "public: __cdecl QQmlPrivate::QQmlElement<class Timer>::QQmlElement<class Timer>(void)" (??0?$QQmlElement@VTimer@@@QQmlPrivate@@QEAA@XZ)

    main.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const Timer::staticMetaObject" (?staticMetaObject@Timer@@2UQMetaObject@@B)

    @
    //
    // Timer.h
    //
    #ifndef TIMER_H
    #define TIMER_H

    #include <QObject>
    class QTimer;

    class Timer : public QObject
    {
    Q_OBJECT
    public:
    Timer(QObject parent = 0);
    private:
    QTimer
    m_timer;
    };

    #endif // TIMER_H
    @
    @
    //
    // Timer.cpp
    //
    #include "timer.h"
    #include <QTimer>

    Timer::Timer(QObject *parent)
    : QObject(parent),
    m_timer(new QTimer(this))
    {
    m_timer->setInterval( 1000 );
    m_timer->start();
    }
    @

    @
    //
    // main.cpp
    //
    #include <QGuiApplication>
    //#include <QtQuick/QQuickPaintedItem>
    //#include <QQmlEngine>
    #include "qtquick2applicationviewer.h"
    #include "timer.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    //qmlRegisterType<Timer>("CustomComponents", 1, 0, "Timer");

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/ObjectTest2/main.qml"&#41;);
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @


  • Moderators

    To add a QObject to QML, just set it's pointer as contextProperty of the engine:
    @
    viewer->engine()->setContextProperty(new Timer(), "MyTimer");
    @

    qmlRegisterType can be used for registering classes that can be instantiated by QML engine and put inside QML files (like Rectangle, Item, etc.). For it to work, you need to inherit your Timer from QQuickItem, AFAIK.



  • Thanks... I do however get the following error after having changed the code.

    error: C2039: 'setContextProperty' : is not a member of 'QQmlEngine'

    @#include <QGuiApplication>
    #include <QtQml/QQmlEngine>
    #include <qqmlcontext.h>
    #include "qtquick2applicationviewer.h"
    #include "timer.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    //qmlRegisterType<Timer>("CustomComponents", 1, 0, "Timer");

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/ObjectTest2/main.qml"&#41;);
    viewer.showExpanded();
    viewer.engine()->setContextProperty(new Timer(), "MyTimer");
    
    return app.exec();
    

    }@


  • Moderators

    It might be in rootContext() instead of engine(), I don't remember out of the top of my head. I don't use QtQuick2ApplicationViewer - maybe that method is not exported at all there. If so, then you need to modify that class (or simply revert to plain old QQuickView).



  • Thanks a lot... I'll look into it :)



  • I soved this issue using this post (Clean all, run qmake, build all)

    http://qt-project.org/forums/viewthread/24574

    The latter part has not yet been solved.



  • I don't know whether It will be help.

    use
    viewer.rootContext()->setContextProperty("MyTimer", new Timer());

    instead of
    viewer.engine()->setContextProperty("MyTimer", new Timer());

    and then you can use MyTimer as Component in your qml files



  • @Hykkel Amazing what a Clean All, Rebuild will fix. This solved the same problem for me. Thanks!


Log in to reply
 

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