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. Custom type with typedef's in QML
Qt 6.11 is out! See what's new in the release blog

Custom type with typedef's in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
c++qmlqgadget
2 Posts 1 Posters 836 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.
  • S Offline
    S Offline
    St.Stanislav
    wrote on last edited by St.Stanislav
    #1

    Hello all! I have the issue connected with the custom types in QML. I have tried a lot of different approaches, so my actual code is little messy now to be posted here as "what I have done" thing. What is the correct way to use/register/declare (qRegisterMetaType, Q_DECLARE_METATYPE) custom type in QML in the following situation. I have some custom typedefs like:

    typedef quint8 ADCValue;
    typedef quint32 ValuePosition;
    

    Then I have structure with this variables:

    struct Example {
        Q_GADGET
        Q_PROPERTY(PhotodiodePosition position MEMBER m_position)
        Q_PROPERTY(PhotodiodeADC whiteLevel MEMBER m_whiteLevel)
    public:
        ADCValue adc = 0;
        ValuePosition value = 0;
    }
    

    I want to use aforementioned structure in the QML to edit and pass back to C++. I use QAbstractListModel with the class, which has struct Example as a member. So my model is working well, all parameters are being passed well through data/setData mechanism. One of the roles is connected with the Example.

    In my QML delegate file I have created appropriate property:

    property var currentExample: model.exampleRole
    

    In the model I have return Example's copy:

    ...
        case ExampleRole: {
            return QVariant::fromValue(item->example());
        }
    ...
    

    And since this place everything works unexpected (with different combinations of registerging/declaring ADCValue, ValuePosition and Example): sometimes I cannot show in console my struct (output just stops to display something after typedefs values), sometimes I cannot modify this object in QML:

    currentExample.adc = 10
    console.log(currentExample) // here it's still 0

    and so on...

    So my question can be a slightly wide, but: What is the correct way to work with typedefs, gadgets in QML correctly?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      St.Stanislav
      wrote on last edited by
      #2

      I have found partial "solution":

      1. QML can not correctly work with quint8 when it was typedefed and qRegistered. Changing initial type to quint32 solved the issue when QML can not correctly display its value.
      2. Passing Q_GADGET to QML via model's method data with using QVariant as a "transport" type works incorrectly (Q_GADGET inner variables cannot be changed for some reason), but passing it directly with separate model's method as
      Example getExpample(int index);
      

      works well. Anyway copying the Example's object in the both cases works well, but the last one allows to modify Example's members in QML.

      I can not say that my issue was solved (now I'm going to use quint32 and the second approach to pass Q_GADGET copy to QML), but I'm wondering, if this is a correct QML/QVariant/Q_GADGET behaviour to avoid such issues further.

      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