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
Forum Updated to NodeBB v4.3 + New Features

Typedefination of basic datatypes is not recognised in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
12 Posts 3 Posters 1.7k 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 Offline
    P Offline
    Phadnis
    wrote on 4 Apr 2019, 12:31 last edited by
    #1

    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

    K 1 Reply Last reply 4 Apr 2019, 14:55
    0
    • P Phadnis
      4 Apr 2019, 12:31

      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

      K Offline
      K Offline
      KroMignon
      wrote on 4 Apr 2019, 14:55 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
      • F Offline
        F Offline
        fcarney
        wrote on 4 Apr 2019, 15:02 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
        • F Offline
          F Offline
          fcarney
          wrote on 4 Apr 2019, 15:10 last edited by fcarney 4 Apr 2019, 15:15
          #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.

          K 1 Reply Last reply 4 Apr 2019, 15:24
          0
          • F fcarney
            4 Apr 2019, 15:10

            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.

            K Offline
            K Offline
            KroMignon
            wrote on 4 Apr 2019, 15:24 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
            • F Offline
              F Offline
              fcarney
              wrote on 4 Apr 2019, 15:26 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.

              K 1 Reply Last reply 4 Apr 2019, 15:27
              0
              • F fcarney
                4 Apr 2019, 15:26

                @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?

                K Offline
                K Offline
                KroMignon
                wrote on 4 Apr 2019, 15:27 last edited by KroMignon 4 Apr 2019, 15:29
                #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
                • F Offline
                  F Offline
                  fcarney
                  wrote on 4 Apr 2019, 16:13 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.

                  K 1 Reply Last reply 4 Apr 2019, 17:44
                  0
                  • F fcarney
                    4 Apr 2019, 16:13

                    @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."

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 4 Apr 2019, 17:44 last edited by KroMignon 4 Jun 2019, 07:37
                    #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 5 Apr 2019, 12:05
                    6
                    • F Offline
                      F Offline
                      fcarney
                      wrote on 4 Apr 2019, 19:49 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
                      • K KroMignon
                        4 Apr 2019, 17:44

                        @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 5 Apr 2019, 12:05 last edited by
                        #11

                        @KroMignon
                        Thank You.

                        K 1 Reply Last reply 5 Apr 2019, 19:56
                        0
                        • P Phadnis
                          5 Apr 2019, 12:05

                          @KroMignon
                          Thank You.

                          K Offline
                          K Offline
                          KroMignon
                          wrote on 5 Apr 2019, 19:56 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

                          1/12

                          4 Apr 2019, 12:31

                          • Login

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