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. Using ENUM as signal slot parameter in c++ / qml
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 4.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.
  • B Offline
    B Offline
    bart6674
    wrote on last edited by
    #1

    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
    }
    

    }
    @

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bart6674
        wrote on last edited by
        #3

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

        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