Signal from C++ to QML using custom parameter type



  • Hi all,

    I would like to emit a signal from C++ to QML using a custom parameter type.
    ie. (in C++)
    emit mySignal(CustomParameterType param);

    In this case: 'CustomParameterType' is an extended QObject with exposed properties I want to access. (It's really just acting as a glorified struct.)

    I have exported my custom class using both:
    Q_DECLARE_METATYPE(CustomParameterType)
    qRegisterMetaType<CustomParameterType>()

    I can successfully catch the signal in QML, but cannot access the details of my 'CustomParameterType'. If I print() the parameter in QML I get: QVariant(CustomParameterType) printed to the log.

    I want to be able to access the properties inside 'CustomeParameterType' (ie. parameter.property1). However, I just get undefined errors when I do. It looks like 'CustomParameterType' is getting sent over as a QVariant object?



  • @cp_mark Everything gets sent as a QVariant to qml. You normally have to cast it out. I don't know how to do it in qml as I'm a qml noob but here is what you would do in c++:

    // assume variant is in a var call v
    CustomParameterType cpt = v.value<CustomParameterType>();
    


  • Use qqmlregistertype to register custom data type. This should fix your issue. If not let me know. I will give ready program.



  • Thanks for the replies everyone.
    @ambershark I can't for the life of me find any examples of how to cast a QVariant object in QML :-\
    @dheerendra I actually already have that in my code, but I didn't mention it as I thought it was used to instantiate custom objects in QML only. Either way here's what I've added:

    qmlRegisterType<CustomParameterType>("MyLib", 1, 0, "CustomParameterType");
    

    I would love to see a working example of my problem. Thanks!



  • @cp_mark said in Signal from C++ to QML using custom parameter type:

    emit mySignal(CustomParameterType param);

    @cp_mark said in Signal from C++ to QML using custom parameter type:

    'CustomParameterType' is an extended QObject

    Don't really know what's going on here but you can't pass QObjects by value. Also, QObject subclasses should work seamlessly in QML.

    Remove Q_DECLARE_METATYPE and qRegisterMetaType<CustomParameterType>() they are useless and change methods that accept CustomParameterType to use a pointer instead of a value.

    Once you did that you'll be able to access its properties from QML



  • @VRonin that worked. Thanks!
    Problem now is destroying the malloc'd CustomParameterType. I am a little familiar with QT memory management, but would like to manually free the object from the QML side of things. This is because I do not have access to a parent object that will end up being destructed (and in turn deconstruct my CustomParameterType).



  • @cp_mark Do you really use malloc() for C++ objects? See https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new.

    QML engine uses garbage collection, you don't free memory in QML. I'm not familiar with object ownership, but see http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership.



  • @Eeli-K Nope. I used new . Just come from a C background.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.