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. QML call to ContextObject function with Enum Parameter
Qt 6.11 is out! See what's new in the release blog

QML call to ContextObject function with Enum Parameter

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.6k 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.
  • DongD Offline
    DongD Offline
    Dong
    wrote on last edited by
    #1

    Hi everyone !

    I have problem with this simple situation.
    I use setContextObject to implement M-V-VM pattern.
    And in QML, I want to call to VM function with enum parameter
    but I get

    Error: Unknown method parameter type: AdsProvider::EnumAdsProvider
    

    This is my ViewModel Class

    class MainWindowViewModel : public DynamicObject
    {
        Q_OBJECT
    public:
        MainWindowViewModel(QObject *parent = 0);
    
        Q_INVOKABLE void showLoginDialog(AdsProvider::EnumAdsProvider engine);
    };
    

    This is the Enum declaration

    class AdsProvider : public QObject
        {
            Q_OBJECT
        public:
            //static const QString GOOGLE;
            //static const QString BING;
            static QString Username;
    
            enum EnumAdsProvider
            {
                GOOGLE,
                BING,
                FACEBOOK
            };
            Q_ENUMS(EnumAdsProvider)
    
            static void registerType()
            {
                qmlRegisterType<AdsProvider>("SystemConstants", 1, 0, "AdsProvider");
            }
        };
    

    I'm already register the custom Type & Enum like this

        AdsProvider::registerType();
        qmlRegisterType<DynamicObject>("core.dynamicobject", 1, 0, "DynamicObject");
        qmlRegisterType<MainWindowViewModel>("Presentation.ViewModel", 1, 0, "MainWindowViewModel");
    

    Here is the QML code call to VM function

    import Presentation.ViewModel 1.0
    import SystemConstants 1.0
    
    ApplicationWindow {
        id:root
        ...
        ToolButton{
            iconSource: "qrc:/login.png"
            onClicked:
            {
                showLoginDialog(AdsProvider.FACEBOOK);
            }
        }
        ...
    }
    

    If I change the the parameter type in VM's function to int, it worked !!!
    But I want to know exactly Enum type instead of int for any Enum parameters.

    Please Help !!! Thanks a lots !!!

    T 1 Reply Last reply
    0
    • DongD Dong

      Hi everyone !

      I have problem with this simple situation.
      I use setContextObject to implement M-V-VM pattern.
      And in QML, I want to call to VM function with enum parameter
      but I get

      Error: Unknown method parameter type: AdsProvider::EnumAdsProvider
      

      This is my ViewModel Class

      class MainWindowViewModel : public DynamicObject
      {
          Q_OBJECT
      public:
          MainWindowViewModel(QObject *parent = 0);
      
          Q_INVOKABLE void showLoginDialog(AdsProvider::EnumAdsProvider engine);
      };
      

      This is the Enum declaration

      class AdsProvider : public QObject
          {
              Q_OBJECT
          public:
              //static const QString GOOGLE;
              //static const QString BING;
              static QString Username;
      
              enum EnumAdsProvider
              {
                  GOOGLE,
                  BING,
                  FACEBOOK
              };
              Q_ENUMS(EnumAdsProvider)
      
              static void registerType()
              {
                  qmlRegisterType<AdsProvider>("SystemConstants", 1, 0, "AdsProvider");
              }
          };
      

      I'm already register the custom Type & Enum like this

          AdsProvider::registerType();
          qmlRegisterType<DynamicObject>("core.dynamicobject", 1, 0, "DynamicObject");
          qmlRegisterType<MainWindowViewModel>("Presentation.ViewModel", 1, 0, "MainWindowViewModel");
      

      Here is the QML code call to VM function

      import Presentation.ViewModel 1.0
      import SystemConstants 1.0
      
      ApplicationWindow {
          id:root
          ...
          ToolButton{
              iconSource: "qrc:/login.png"
              onClicked:
              {
                  showLoginDialog(AdsProvider.FACEBOOK);
              }
          }
          ...
      }
      

      If I change the the parameter type in VM's function to int, it worked !!!
      But I want to know exactly Enum type instead of int for any Enum parameters.

      Please Help !!! Thanks a lots !!!

      T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      @Dong
      I usually put Q_ENUMS macro right afte Q_OBJECT. Maybe that helps?

      1 Reply Last reply
      0
      • EmilieGE Offline
        EmilieGE Offline
        EmilieG
        wrote on last edited by
        #3

        I know this in an old topic, but I'm having the exact same issue, and can't find a solution. Anyone know how to fix that properly ?

        KroMignonK ODБOïO 2 Replies Last reply
        0
        • EmilieGE EmilieG

          I know this in an old topic, but I'm having the exact same issue, and can't find a solution. Anyone know how to fix that properly ?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @EmilieG said in QML call to ContextObject function with Enum Parameter:

          Anyone know how to fix that properly ?

          as @t3685 already write, you have to ensure the enumeration is registered with Q_ENUM() or Q_ENUMS()

          ==> take a look at https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#enumeration-types for more details

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • EmilieGE EmilieG

            I know this in an old topic, but I'm having the exact same issue, and can't find a solution. Anyone know how to fix that properly ?

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @EmilieG hi
            this one way to do it

            class MyClass: public QObject
            {
                Q_OBJECT
            public :
              enum class CurrentState : qint32{
                    STAND_BY,
                    RUNNING,
                    ERROR   
                };
                Q_ENUM(CurrentState)
            
                Q_INVOKABLE void testEnumArg(CurrentState s){
                    qDebug() << s;
                }
            
            
            };
            Q_DECLARE_METATYPE(MyClass::CurrentState)
            
            //in main.cpp for example 
            
            qmlRegisterType<MyClass>("com.MyClass.test", 1, 0, "MyClass"); // make enum accessible in qml
            // +  create an instance  of that class and use setContextProperty() to make it avalable in qml and call the invokable method
            
            // in qml 
            import com.MyClass.test 1.0 
            ...
            classObject.testEnumArg(MyClass.STAND_BY)
            
            
            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