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. Returning and Enum to QML using a Q_INVOKABLE method

Returning and Enum to QML using a Q_INVOKABLE method

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 9.4k 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.
  • K Offline
    K Offline
    kzawada
    wrote on last edited by
    #1

    Is it possible to pass enums from a C++ Q_OBJECT class using Q_INVOKABLE to a QML implementation. There is an error that I am getting with it:

    @main.qml:185: Error: Unknown method return type: FaultWarningItem::ItemType@

    This happens when I attempt to return a enum as part of the Q_INVOKABLE:
    @Q_INVOKABLE FaultWarningItem::ItemType getItemErrorType(void);@

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you register your enum ?

      See this "thread":http://qt-project.org/forums/viewthread/16497 for an example

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chezgi
        wrote on last edited by
        #3

        there is some limits on using enums from qml.
        you can't use another classes enums for Q_INVOKABLE functions.
        your enums only can be used for primary classes functions.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kzawada
          wrote on last edited by
          #4

          Indeed this is the case there is a problem with this. I was hoping to use this to explicitly do apple-to-apple comparison: (1) make the *.qml file easily readable by replacing hard coded numbers, (2) force errors when interpreting *.qml files when enum changes, and (3) make the code easily searchable to enum item.

          However, there is a limitation here that I will not be able to get around.

          My code is the following:

          FaultWarningItem.h
          @#ifndef FAULTWARNINGITEM_H
          #define FAULTWARNINGITEM_H
          #include <QObject>

          // Fault Warning Container Class
          class FaultWarningItem
          {
          Q_GADGET
          Q_ENUMS(ItemType)
          public:

          enum ItemType
          {
              TypeUnspecified = -1,
              TypeNoError = 0,
              TypeWarning,
              TypeFault
          };
          

          };

          #endif // FAULTWARNINGITEM_H@

          FaultWarningModel.h
          @#ifndef FAULTWANRINGMODEL_H
          #define FAULTWANRINGMODEL_H

          #include <QObject>
          #include "FaultWarningItem.h"

          class FaultWarningModel: public QObject {
          Q_OBJECT
          public:
          FaultWarningModel(QObject *parent=0);

          ~FaultWarningModel();
          
          Q_INVOKABLE FaultWarningItem::ItemType getItemErrorType(void) const;
          Q_INVOKABLE int getItemErrorTypeAsInt(void) const;
          
          Q_INVOKABLE void setNextFaultWarning();
          
          int getCount() const;
          

          private:
          int m_count;
          FaultWarningItem::ItemType m_FaultWarningItem;
          signals:
          void countChanged(int);
          };

          #endif // FAULTWANRINGMODEL_H@

          FaultWarningModel.cpp
          @#include "FaultWarningModel.h"
          #include <QQml.h>
          #include <QDebug>

          FaultWarningModel::FaultWarningModel(QObject *parent) :
          QObject(parent)
          {

          m_FaultWarningItem = FaultWarningItem::TypeUnspecified;
          
          int statusType = qmlRegisterUncreatableType<FaultWarningItem>
                           (
                               "ItemType",                                          // const char * uri,
                               1,                                                   // int versionMajor,
                               0,                                                   // int versionMinor,
                               "ItemType",                                          // const char * qmlName,
                               "ItemType type cannot be created." // const QString & message
                           );
          
          qDebug() << "statusType: " << statusType;
          

          }

          FaultWarningItem::ItemType FaultWarningModel::getItemErrorType(void) const
          {
          return m_FaultWarningItem;
          }

          int FaultWarningModel::getItemErrorTypeAsInt() const
          {
          int iValue = (int) m_FaultWarningItem;
          return iValue;
          }
          @

          main.qml

          @import QtQuick 2.0

          // The enum types need to be imported before they can be used.
          import ItemType 1.0

          Rectangle {
          width: 200
          height: 300

          property int currentFaultWarningType: ItemType.TypeUnspecified
          
          Column {
              anchors.horizontalCenter: parent.horizontalCenter
              anchors.verticalCenter: parent.verticalCenter
              spacing: 10
          
              // Toggles the printer type
              Rectangle {
                  id: buttonFaultWaring
          
                  width: 100
                  height: 50
          
                  color: "gainsboro"
          
                  border.color: "black"
                  border.width: 1
          
                  Text {
                      text: "TOGGLE Fault/Warning Type"
                      width: parent.width
                      anchors.centerIn: parent
                      wrapMode: Text.WordWrap
                      horizontalAlignment: Text.AlignHCenter
                  }
          
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          FaultWarningModel.setNextFaultWarning();
                          currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt();
                      }
                  }
              }
          
              // Shows the type of the printer
              Rectangle {
                  id: statusFaultWarning
          
                  width: 100
                  height: 50
          
                  Text {
                     text: {
                         // Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
                         //currentFaultWarningType = FaultWarningModel.getItemErrorType()
          
                         // Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
                         /*if (ItemType.TypeUnspecified === FaultWarningModel.getItemErrorType())
                         {
                             "Yay, it works"
                         }*/
          
                         currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt()
                         if (ItemType.TypeUnspecified === currentFaultWarningType)
                         {
                             "TypeUnspecified (-1)"
                         }
                         else if (ItemType.TypeNoError === currentFaultWarningType)
                         {
                             "TypeNoError (0)"
                         }
                         else if (ItemType.TypeWarning === currentFaultWarningType)
                         {
                             "TypeWarning (1)"
                         }
                         else if (ItemType.TypeFault === currentFaultWarningType)
                         {
                             "TypeFault (2)"
                         }
                         else
                         {
                             "?"
                         }
                     }
          
                     anchors.centerIn: parent
                 }
              }
          }
          

          }@

          Is there any future plans in Qt to have this changed so that it can be used without getting "Error: Unknown method return type"?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chezgi
            wrote on last edited by
            #5

            see https://codereview.qt-project.org/#change,73812
            it seems that something changed at 5.3.0
            try it.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kafanti
              wrote on last edited by
              #6

              It is already old topic but I would like to reply for documentation purposes for further questions,
              If you like to use an enum on a Q_INVOKABLE function, then this enum should be registered.

              https://bugreports.qt.io/browse/QTBUG-19741

              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