Registering enum in QML: does not work in QtQuick2 [SOLVED]



  • Hi

    I'm porting an application from Quick1 to Quick2, and got problems with registering an enum.
    I have the following which works with Quick1:

    keyboardcontrol.h
    @
    #include <QObject>

    class iDrive:public QObject
    {
    Q_OBJECT
    Q_ENUMS(iDriveCommands)
    public:

    enum iDriveCommands
    {
        IUndef = 0,
        IUp,
        IRight,
        IDown,
        ILeft,
        IPush,
        IRotLeft,
        IRotRight
    };
    

    };
    @

    This was registered in the main.cpp:

    @

    #include <QApplication>
    #include "qmlapplicationviewer.h"
    #include <QDeclarativeComponent>
    #include <QDeclarativeEngine>
    #include <QDeclarativeContext>
    #include "keyboardcontrol.h"

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {

    QScopedPointer<QApplication> app(createApplication(argc, argv));
    

    QmlApplicationViewer viewer;

    qmlRegisterType<iDrive>("iDriveLib", 1, 0,"iDrive");
    

    .....
    }
    @

    and was used in the qml:

    @
    import QtQuick 1.1
    import iDriveLib 1.0

    Rectangle
    {
    id: mainWindow
    width: 800
    height: 480

    function keyEventHandling( key) //Is called from somewhere, not important for this problem
    {
        switch (key)
        {
            case iDrive.IUp:
            {
                console.log("Up");
                break;
            }
    }
    

    }
    @

    When working with Quick1, this works and I will run in the case iDrive.IUp

    For porting it into Quick2, I changed only the main.cpp

    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QtQuick/QQuickPaintedItem>
    #include <QSurface>
    #include <QSurfaceFormat>
    #include "keyboardcontrol.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QtQuick2ApplicationViewer viewer;
    qmlRegisterType<iDrive>("iDriveLib", 1, 0,"iDrive");

    }
    @

    When running this, I got the following error:

    @
    ReferenceError: iDrive is not defined
    @

    What did I miss?


  • Moderators

    I think the qmlname in qmlRegisterType must begin with a capital case.
    So use
    @
    qmlRegisterType<iDrive>("iDriveLib", 1, 0,"iDrive");
    @
    as
    @
    qmlRegisterType<iDrive>("iDriveLib", 1, 0,"IDrive");
    @

    and hence,
    @
    case IDrive.IUp:
    @



  • That solves the problem, thanks a lot.


Log in to reply
 

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