Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QmlRegisterType, Qt5.1 and QtQuick2
Forum Updated to NodeBB v4.3 + New Features

QmlRegisterType, Qt5.1 and QtQuick2

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 7.2k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    Hykkel
    wrote on last edited by
    #1

    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:

    • http://qt.digia.com/Global/Images/Qt/Files/QtEssentialsQtQuick/qml-cpp-integration.pdf
    • http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html

    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();
    

    }
    @

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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.

      (Z(:^

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hykkel
        wrote on last edited by
        #3

        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();
        

        }@

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          (Z(:^

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Hykkel
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hykkel
              wrote on last edited by
              #6

              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.

              T 1 Reply Last reply
              0
              • K Offline
                K Offline
                KHBCompany
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • H Hykkel

                  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.

                  T Offline
                  T Offline
                  tbirkulosis
                  wrote on last edited by
                  #8

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

                  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