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. Typedefination of basic datatypes is not recognised in qml
Qt 6.11 is out! See what's new in the release blog

Typedefination of basic datatypes is not recognised in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 3.8k 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.
  • P Phadnis

    Hi ,
    To Satisfy MISRA Coding Guidlines
    Guidline....
    3-9-2 Typedefs that indicate size and signedness should be used in place of the basic numerical types.

    the following function
    Q_INVOKABLE int getTemperature1(bool init=false) const;
    is rewritten as
    Q_INVOKABLE SINT getTemperature1(bool init=false) const;

    where SINT is a typedef
    but in qml I am getting this error
    Unknown method return type: SINT

    So what is to be done.
    Regards

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

    @Phadnis No, this not possible with QML.

    To do this, you have to register to SINT type with qRegisterMetaType, this will enable QML engine to work with this new type.
    Take a look at https://doc.qt.io/qt-5/qtqml-cppintegration-data.html for more details.

    regards

    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
    2
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #3

      The only types I think you can make in QML are QObject based types, at least that is all I can find.

      This would mean that in order to interface to QML you may have to use only QObject based types if you want to follow MISRA for your C++. Otherwise if you want to use basic types with QML you would have to break the rules.

      Are there exceptions for interfacing to systems that are inflexible?

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by fcarney
        #4

        Is SINT a typedef or can it be a #define? Does the rule mandate it must be a different type or that it just cannot be a naked "int" and could be a "#define SINT int" or something similar? The reason I ask is what is the intent? Is it to make it readable and recognizable or is there some reason it needs to be a different "type"? Because if SINT is defined like this: typedef int SINT, then the resulting code won't be any different than if you used a naked int. So it just becomes a readability issue and not a functionality issue. Using a #define gives you the readability without sacrificing the ability to pass an int to QML as it just gets replaces with int when compiling.

        C++ is a perfectly valid school of magic.

        KroMignonK 1 Reply Last reply
        0
        • fcarneyF fcarney

          Is SINT a typedef or can it be a #define? Does the rule mandate it must be a different type or that it just cannot be a naked "int" and could be a "#define SINT int" or something similar? The reason I ask is what is the intent? Is it to make it readable and recognizable or is there some reason it needs to be a different "type"? Because if SINT is defined like this: typedef int SINT, then the resulting code won't be any different than if you used a naked int. So it just becomes a readability issue and not a functionality issue. Using a #define gives you the readability without sacrificing the ability to pass an int to QML as it just gets replaces with int when compiling.

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

          @fcarney You asked: "So what is to be done."

          Just add in you main.cpp

          qRegisterMetaType<SINT>("SINT")
          

          So QML engine will be aware about SINT type, and can work with.

          That's all.

          Have you tried this?

          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
          3
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #6

            @KroMignon said in Typedefination of basic datatypes is not recognised in qml:

            qRegisterMetaType<SINT>("SINT")

            I looked all over for that. Where did you find that so I can bookmark the reference?

            C++ is a perfectly valid school of magic.

            KroMignonK 1 Reply Last reply
            0
            • fcarneyF fcarney

              @KroMignon said in Typedefination of basic datatypes is not recognised in qml:

              qRegisterMetaType<SINT>("SINT")

              I looked all over for that. Where did you find that so I can bookmark the reference?

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

              @fcarney said in Typedefination of basic datatypes is not recognised in qml:

              I looked all over for that. Where did you find that so I can bookmark the reference?

              Take at look at https://doc.qt.io/qt-5/custom-types.html and https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType

              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
              4
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by
                #8

                @KroMignon said in Typedefination of basic datatypes is not recognised in qml:

                Take at look at https://doc.qt.io/qt-5/custom-types.html and https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType

                Not sure that will work for "int".
                "Registers the type name typeName for the type T. Returns the internal ID used by QMetaType. Any class or struct that has a public default constructor, a public copy constructor and a public destructor can be registered."

                C++ is a perfectly valid school of magic.

                KroMignonK 1 Reply Last reply
                0
                • fcarneyF fcarney

                  @KroMignon said in Typedefination of basic datatypes is not recognised in qml:

                  Take at look at https://doc.qt.io/qt-5/custom-types.html and https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType

                  Not sure that will work for "int".
                  "Registers the type name typeName for the type T. Returns the internal ID used by QMetaType. Any class or struct that has a public default constructor, a public copy constructor and a public destructor can be registered."

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

                  @fcarney said in Typedefination of basic datatypes is not recognised in qml:

                  Not sure that will work for "int".

                  Why you are so resistant and don't try it out?

                  I've done a little test class

                  class MisraInfo : public QObject
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MisraInfo(QObject * parent = Q_NULLPTR) : QObject(parent)
                      {
                      }
                  
                      Q_INVOKABLE SINT touchMe()
                      {
                          static int counter = 0;
                      
                          return ++counter;
                      }
                  };
                  

                  And:

                  int main(int argc, char *argv[])
                  {
                  ...
                     MisraInfo misra;
                     engine->rootContext()->setContextProperty("misra", &misra);
                  ...
                  }
                  

                  Fails with: Error: Unknown method return type: SINT

                  But:

                  int main(int argc, char *argv[])
                  {
                  ...
                     qRegisterMetaType<SINT>("SINT");
                     MisraInfo misra;
                     engine->rootContext()->setContextProperty("misra", &misra);
                  ...
                  }
                  

                  Works!

                  This is the way how Qt/QML type convertion between C++ and QML/JavaScript world works.
                  If you want to define each base type as new type, you have to register it to be able to use it with QML and/or signal/slots.

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

                  P 1 Reply Last reply
                  6
                  • fcarneyF Offline
                    fcarneyF Offline
                    fcarney
                    wrote on last edited by
                    #10

                    Wow! Thanks for explaining this:

                    #include <QGuiApplication>
                    #include <QQmlApplicationEngine>
                    #include <QQmlContext>
                    
                    typedef int SINT;
                    
                    class MisraInfo : public QObject
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MisraInfo(QObject * parent = Q_NULLPTR) : QObject(parent)
                        {
                        }
                    
                        Q_INVOKABLE SINT touchMe()
                        {
                            static SINT counter = 0;
                    
                            return ++counter;
                        }
                    };
                    
                    int main(int argc, char *argv[])
                    {
                        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                    
                        QGuiApplication app(argc, argv);
                    
                        QQmlApplicationEngine engine;
                    
                        qRegisterMetaType<SINT>("SINT"); // register custom type
                        MisraInfo misra;
                        engine.rootContext()->setContextProperty("misra", &misra);
                    
                        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                        if (engine.rootObjects().isEmpty())
                            return -1;
                    
                        return app.exec();
                    }
                    
                    #include "main.moc"
                    

                    qml:

                    import QtQuick 2.9
                    import QtQuick.Window 2.2
                    
                    Window {
                        visible: true
                        width: 640
                        height: 480
                        title: qsTr("Hello World")
                    
                        Component.onCompleted: {
                            console.log(misra.touchMe())
                            console.log(misra.touchMe())
                            console.log(misra.touchMe())
                            console.log(misra.touchMe())
                            console.log(misra.touchMe())
                        }
                    }
                    

                    C++ is a perfectly valid school of magic.

                    1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @fcarney said in Typedefination of basic datatypes is not recognised in qml:

                      Not sure that will work for "int".

                      Why you are so resistant and don't try it out?

                      I've done a little test class

                      class MisraInfo : public QObject
                      {
                          Q_OBJECT
                      
                      public:
                          explicit MisraInfo(QObject * parent = Q_NULLPTR) : QObject(parent)
                          {
                          }
                      
                          Q_INVOKABLE SINT touchMe()
                          {
                              static int counter = 0;
                          
                              return ++counter;
                          }
                      };
                      

                      And:

                      int main(int argc, char *argv[])
                      {
                      ...
                         MisraInfo misra;
                         engine->rootContext()->setContextProperty("misra", &misra);
                      ...
                      }
                      

                      Fails with: Error: Unknown method return type: SINT

                      But:

                      int main(int argc, char *argv[])
                      {
                      ...
                         qRegisterMetaType<SINT>("SINT");
                         MisraInfo misra;
                         engine->rootContext()->setContextProperty("misra", &misra);
                      ...
                      }
                      

                      Works!

                      This is the way how Qt/QML type convertion between C++ and QML/JavaScript world works.
                      If you want to define each base type as new type, you have to register it to be able to use it with QML and/or signal/slots.

                      P Offline
                      P Offline
                      Phadnis
                      wrote on last edited by
                      #11

                      @KroMignon
                      Thank You.

                      KroMignonK 1 Reply Last reply
                      0
                      • P Phadnis

                        @KroMignon
                        Thank You.

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

                        @Phadnis your welcome

                        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

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved