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. Registering enum in QML: does not work in QtQuick2 [SOLVED]

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.7k 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.
  • O Offline
    O Offline
    outdoor_guy
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      1 Reply Last reply
      0
      • O Offline
        O Offline
        outdoor_guy
        wrote on last edited by
        #3

        That solves the problem, thanks a lot.

        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