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. Signal from C++ to QML using custom parameter type

Signal from C++ to QML using custom parameter type

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 3.4k 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.
  • C Offline
    C Offline
    cp_mark
    wrote on last edited by
    #1

    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?

    A VRoninV 2 Replies Last reply
    0
    • C cp_mark

      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?

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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>();
      

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        2
        • C Offline
          C Offline
          cp_mark
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • C cp_mark

            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?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • C Offline
              C Offline
              cp_mark
              wrote on last edited by cp_mark
              #6

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

              E 1 Reply Last reply
              0
              • C cp_mark

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

                E Offline
                E Offline
                Eeli K
                wrote on last edited by
                #7

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

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  cp_mark
                  wrote on last edited by
                  #8

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

                  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