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. Read c++ struct from QML
QtWS25 Last Chance

Read c++ struct from QML

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 6 Posters 16.6k 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.
  • D Offline
    D Offline
    deimos
    wrote on last edited by
    #1

    hi,

    I need to read struct from qml but I dont know how.
    Here some code:

    @struct ThumbStruct {
    int ID;
    QString description_row1;
    QString description_row2;
    QString image_tburl;
    QString image_url;
    } ;
    Q_DECLARE_METATYPE(ThumbStruct)

    class ThumbItem : public QDeclarativeItem
    {
    Q_OBJECT

    Q_PROPERTY(ThumbStruct properties READ properties WRITE setProperties NOTIFY propertiesChanged)
    

    public:
    ThumbItem(QDeclarativeItem *parent=0);

    ThumbStruct properties() const;
    void setProperties(const ThumbStruct &properties);
    
    ThumbStruct m_properties;
    

    signals:
    void propertiesChanged();
    }@

    Reading from QML for example in a Text component like above, give me "Unable to assign [undefined] to QString text"

    @Text {
    id: thumbText
    text: properties.description_row1
    }@

    if I add other Q_PROPERTY, they work
    I am new to QML, so maybe the syntax is wrong ?

    Thanks in advance

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MichK
      wrote on last edited by
      #2

      Maybe ThumbStruct should derive from QObject and description_row1 and other fields should be declared as Q_PROPERTY.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deimos
        wrote on last edited by
        #3

        bq. Maybe ThumbStruct should derive from QObject

        So I have to declare ThumbStruct as a class instead of a struct ? Or how I can derive a struct type from QObject ?

        bq. description_row1 and other fields should be declared as Q_PROPERTY

        If I do this I can read description_row1 because of a known type, but the problem is that seems ThumbStruct isn't a known type within QML even if I declared it with Q_DECLARE_METATYPE(ThumbStruct)

        Since ThumbStruct has a lot more fields ( and also other structs ), I would like to read it directly instead of making many Q_PROPERTY with get, write and notify functions.
        Thanks for your time

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MichK
          wrote on last edited by
          #4

          I don't know how it is about Q_OBJECT macro so it will be better to make ThumbStruct a class instead of struct.
          I would change properties property in your ThumbItem to be of type QObject*!
          Then after adding Q_PROPERTY items to ThumbStruct it should work, I guess :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deimos
            wrote on last edited by
            #5

            I will try that

            thank you

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

              Yep, to access object properties in QML class should be derived from QObject, contain Q_OBJECT macro, have Q_PROPERTY (or Q_INVOKABLE/slot in case of methods) and if it is returned by other method (not exposed as context property) you should register it with qmlRegisterType (or qmlRegisterUncreatableType)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mikero175
                wrote on last edited by
                #7

                The above discussion was helpful however it does not explicitly say that it is impossible to read/write a C/C++ struct that does not inherit from QObject or have Q_PROPERTYs in QML. Is there any way to register a non-QObject data type so that it can be used in a QML context?

                It would be very helpful for our project if this were possible because there is a lot of code already written that has data which the UI needs to access but is not in a class that derives from QObject. This was possible with the widget based UI we currently have by using the Q_DECLARE_METATYPE() macro and the qRegisterMetaType<>() method. We would very much like to change over to QML for the UI but the amount of work may be prohibitive.

                Thank you!

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Denis was quite clear, I think:

                  [quote author="Denis Kormalev" date="1312616496"]Yep, to access object properties in QML class should be derived from QObject, contain Q_OBJECT macro, have Q_PROPERTY (or Q_INVOKABLE/slot in case of methods) and if it is returned by other method (not exposed as context property) you should register it with qmlRegisterType (or qmlRegisterUncreatableType)[/quote]

                  So, the answer is no, you cannot export structs to a QML context.

                  edit:
                  OK, structs as you would have then in C: POD's.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ixSci
                    wrote on last edited by
                    #9

                    bq. So I have to declare ThumbStruct as a class instead of a struct ? Or how I can derive a struct type from QObject ?

                    bq. it will be better to make ThumbStruct a class instead of struct.

                    There is no difference between class and structure in C++ except the default "access restriction"(public or private). So don't be so panic about making class from your structure as well as feel free to inherit your structure from the QObject. Just choose the way you like more

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MichK
                      wrote on last edited by
                      #10

                      But remember that "Q_OBJECT":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#Q_OBJECT macro must appear in private section of your class.

                      You mat try to use "QVariant":http://developer.qt.nokia.com/doc/qt-4.8/qvariant.html to export data from C++ to QML.

                      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