Using ENUM as signal slot parameter in c++ / qml



  • Hello,

    i want to use my own enum as parameter for signal and slot functions.
    The functions are used in c++ code and in qml code.

    I'm getting this error message:
    QObject::connect: No such signal QQuickWindowQmlImpl_QML_0::qmlOperatingMode( OperatingModes::Value ) in main.cpp:16

    How can I resolve this problem?

    Here is my code:
    Enums.h
    @
    #ifndef ENUMS_H
    #define ENUMS_H

    #include <QtQml>

    class OperatingModes : public QObject
    {
    Q_OBJECT
    Q_ENUMS( Value )

    public:
    OperatingModes( QObject *parent = 0 );
    enum Value {
    IDManual,
    IDAutomatic
    };

    private:
    int Value_m;
    };

    Q_DECLARE_METATYPE( OperatingModes::Value )

    #endif // ENUMS_H
    @

    MyClass.h
    @
    #ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>
    #include <QtQml>
    #include "Enums.h"

    class MyClass : public QObject
    {
    Q_OBJECT

    public:
    MyClass( QObject *parent = 0 );
    public slots:
    void setOperatingMode( OperatingModes::Value );

    signals:

    private:
    };

    #endif // MYCLASS_H
    @

    MyClass.cpp
    @
    #include "MyClass.h"

    MyClass::MyClass( QObject *parent ) : QObject( parent )
    {
    setOperatingMode( OperatingModes::IDAutomatic );
    }

    void MyClass::setOperatingMode( OperatingModes::Value value )
    {
    qDebug() << value;
    }
    @

    main.cpp
    @
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>

    #include "MyClass.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    MyClass myClass;

    qmlRegisterType<OperatingModes>( "MyClass", 1, 0, "OperatingModes" );
    qRegisterMetaType<OperatingModes::Value>("OperatingModes::Value");
    
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    engine.connect( engine.rootObjects().first(), \
    SIGNAL( qmlOperatingMode( OperatingModes::Value ) ), &myClass, \
    SLOT( setOperatingMode( OperatingModes::Value ) ) );
    
    return app.exec();
    

    }
    @

    main.qml
    @
    import QtQuick 2.3
    import QtQuick.Window 2.2

    import MyClass 1.0

    Window {
    visible: true
    width: 360
    height: 360

    signal qmlOperatingMode( OperatingModes myOperatingMode )
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            qmlOperatingMode( OperatingModes.IDAutomatic )
        }
    }
    
    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
    

    }
    @



  • From the documentation on : "http://qt-project.org/doc/qt-5/qml-enumeration.html":http://qt-project.org/doc/qt-5/qml-enumeration.html

    bq. Using the enumeration type in QML

    The enumeration type is a representation of a C++ enum type. It is not possible to refer to the enumeration type in QML itself; instead, the int or var types can be used when referring to enumeration values from QML code.



  • Thanks for this information.

    So i can't use type save enum paramters in my c++ functions and must use int values like this?!

    MyClass.h
    @
    (...)
    public slots:
    // doesn't work whith qml
    void setOperatingMode( OperatingModes::Value );
    // works with qml but is not type save
    void setOperatingMode( int );
    (...)
    @


Log in to reply
 

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