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. [CLOSED] Qt Q_PROPERTY: how to pass custom C struct in QML

[CLOSED] Qt Q_PROPERTY: how to pass custom C struct in QML

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 5.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.
  • T Offline
    T Offline
    Tycos
    wrote on last edited by
    #1

    Hi to everyone,

    this is my problem: I've a C struct (I cannot modify this) and I want pass it to a QML.
    For example:

    @
    typedef struct
    {
    int a;
    int b;
    } INT_CUSTOM_STRUCT;

    typedef struct
    {
    char c;
    char b;
    } CHAR_CUSTOM_STRUCT;

    typedef struct
    {
    INT_CUSTOM_STRUCT num;
    CHAR_CUSTOM_STRUCT varChar;
    } CUSTOM_C_TYPE;
    @

    and in the header of my object (c++)
    @
    class MyCustomClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(CUSTOM_C_TYPE myStruct READ getMyStruct CONSTANT)

    public:
    explicit MyCustomClass(QObject *parent = 0);
    CUSTOM_C_TYPE getMyStruct() { return myObj;}

    private:
    CUSTOM_C_TYPE myObj;
    };
    @

    in main.cpp, I've add this line:
    @
    int main(int argc, char *argv[])
    {
    qmlRegisterType<MyCustomClass>("com.example.myClass", 1, 0, "MyCustomClass");
    QApplication app(argc, argv);

    MyCustomClass *myClass = new MyCustomClass();
    
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    engine.rootContext()->setContextProperty("myClass",myClass);
    
    
    return app.exec&#40;&#41;;
    

    }@

    in my Qml:
    @
    import QtQuick 1.2
    import com.example.myClass 1.0

    Item {
    Component.onCompleted: console.log(myClass.myStruct.num.a);
    }
    @

    It doesn't works...

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Custom properties need to inherit QObject. So you probably need to write a wrapper class for your C struct.

      Check the Q_PROPERTY documentation for more information:

      http://qt-project.org/doc/qt-5/properties.html#requirements-for-declaring-properties

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tycos
        wrote on last edited by
        #3

        But it's very difficult create a wrapper class.... this is only an example... but I have a very big structure to pass in a qml
        I've modify my files in this way:

        mycustomclass.h
        @
        typedef struct
        {
        int a;
        int b;
        } INT_CUSTOM_STRUCT;

        typedef struct
        {
        char c;
        char b;
        } CHAR_CUSTOM_STRUCT;

        typedef struct
        {
        int test;
        INT_CUSTOM_STRUCT num;
        CHAR_CUSTOM_STRUCT varChar;
        } CUSTOM_C_TYPE;

        Q_DECLARE_METATYPE(CUSTOM_C_TYPE)

        class MyCustomClass : public QObject
        {
        Q_OBJECT
        Q_PROPERTY(CUSTOM_C_TYPE myStruct READ getMyStruct CONSTANT)
        public:
        explicit MyCustomClass(QObject *parent = 0);
        CUSTOM_C_TYPE getMyStruct() { return myObj;}

        private:
        CUSTOM_C_TYPE myObj;

        };
        @

        mycustomclass.cpp
        @
        MyCustomClass::MyCustomClass(QObject *parent) :
        QObject(parent)
        {
        myObj.num.a = 10;
        myObj.test = 50;
        }
        @

        main.cpp
        @
        int main(int argc, char *argv[])
        {
        qmlRegisterType<MyCustomClass>("com.example.myClass", 1, 0, "MyCustomClass");
        qRegisterMetaType<CUSTOM_C_TYPE>("CUSTOM_C_TYPE");
        QApplication app(argc, argv);

        MyCustomClass *myClass = new MyCustomClass();
        
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("myClass",myClass);
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
        
        
        return app.exec&#40;&#41;;
        

        }
        @

        main.qml
        @
        import QtQuick 2.2
        import QtQuick.Controls 1.1
        import com.example.myClass 1.0

        ApplicationWindow {

        Component.onCompleted: console.log(myClass.myStruct)
        

        }
        @

        output:
        @
        qml: QVariant(CUSTOM_C_TYPE)
        @

        if i change my qml file in this way:
        @
        import QtQuick 2.2
        import QtQuick.Controls 1.1
        import com.example.myClass 1.0

        ApplicationWindow {

        Component.onCompleted: console.log(myClass.myStruct.test)
        

        }
        @

        my new output is:
        @
        qml: undefined
        @

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

          Please read the documentation some more. The Q_PROPERTY must be a subclass of QObject.
          You are using your C-struct as the Q_PROPERTY.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tycos
            wrote on last edited by
            #5

            thank you for the reply

            there isn't an other way to pass this c-native struct to a Qml?

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              Not without wrapping it in a QObject-derived class.

              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