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. invoking QObject operators from JS/QML
Qt 6.11 is out! See what's new in the release blog

invoking QObject operators from JS/QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 763 Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I'm trying to invoke the (in)equality operator of a QObject-derived class from QML, but, according to the debugger, I never hit the operator.

    class Equipment : public QObject
    {
        Q_OBJECT
        QML_ELEMENT
    public:
        explicit Equipment(QObject *parent = nullptr);
        virtual bool operator == (const Equipment &rhs) const;
        virtual bool operator != (const Equipment &rhs) const { return !operator==(rhs); };
        
    // qml
    ColumnLayout {
        property Equipment equipmentCopy
        property Equipment equipmentOriginal: equipmentModel.getEquipment(equipmentCopy.uuid)
        Button {
            onClicked: {
                if (equipmentCopy !== equipmentModel.getEquipment(equipmentCopy.uuid)) {
                ...
    

    Is this not possible, or am I just doing something wrong?

    Thanks...

    kshegunovK 1 Reply Last reply
    0
    • mzimmersM mzimmers

      @kshegunov said in invoking QObject operators from JS/QML:

      @mzimmers said in invoking QObject operators from JS/QML:

      Well ... if it ain't invokable (or a slot), I suppose it's hard for the engine to know it should be exposed to QML, right?

      I did try using Q_INVOKABLE with no change in behavior. I should have mentioned that.

      Although, having an equality operator for a QObject is very unusual ...

      Really? Why is that?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #4

      @mzimmers said in invoking QObject operators from JS/QML:

      I did try using Q_INVOKABLE with no change in behavior. I should have mentioned that.

      I see. Well I'm not sure, to be honest, but you could quickly check if it works with a regular method.
      Also you could try/see if the comparison operators are registered with the meta-type system.
      ... although as I think of it, this doesn't make much sense, as QObjects are not meta-types to begin with ...

      Really? Why is that?

      Because QObjects are all different (i.e. they are identities)
      https://doc.qt.io/qt-6/object.html#identity-vs-value

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • mzimmersM mzimmers

        Hi all -

        I'm trying to invoke the (in)equality operator of a QObject-derived class from QML, but, according to the debugger, I never hit the operator.

        class Equipment : public QObject
        {
            Q_OBJECT
            QML_ELEMENT
        public:
            explicit Equipment(QObject *parent = nullptr);
            virtual bool operator == (const Equipment &rhs) const;
            virtual bool operator != (const Equipment &rhs) const { return !operator==(rhs); };
            
        // qml
        ColumnLayout {
            property Equipment equipmentCopy
            property Equipment equipmentOriginal: equipmentModel.getEquipment(equipmentCopy.uuid)
            Button {
                onClicked: {
                    if (equipmentCopy !== equipmentModel.getEquipment(equipmentCopy.uuid)) {
                    ...
        

        Is this not possible, or am I just doing something wrong?

        Thanks...

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #2

        @mzimmers said in invoking QObject operators from JS/QML:

        Is this not possible, or am I just doing something wrong?

        Well ... if it ain't invokable (or a slot), I suppose it's hard for the engine to know it should be exposed to QML, right?
        Although, having an equality operator for a QObject is very unusual ...

        Read and abide by the Qt Code of Conduct

        mzimmersM 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @mzimmers said in invoking QObject operators from JS/QML:

          Is this not possible, or am I just doing something wrong?

          Well ... if it ain't invokable (or a slot), I suppose it's hard for the engine to know it should be exposed to QML, right?
          Although, having an equality operator for a QObject is very unusual ...

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #3

          @kshegunov said in invoking QObject operators from JS/QML:

          @mzimmers said in invoking QObject operators from JS/QML:

          Well ... if it ain't invokable (or a slot), I suppose it's hard for the engine to know it should be exposed to QML, right?

          I did try using Q_INVOKABLE with no change in behavior. I should have mentioned that.

          Although, having an equality operator for a QObject is very unusual ...

          Really? Why is that?

          kshegunovK 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @kshegunov said in invoking QObject operators from JS/QML:

            @mzimmers said in invoking QObject operators from JS/QML:

            Well ... if it ain't invokable (or a slot), I suppose it's hard for the engine to know it should be exposed to QML, right?

            I did try using Q_INVOKABLE with no change in behavior. I should have mentioned that.

            Although, having an equality operator for a QObject is very unusual ...

            Really? Why is that?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #4

            @mzimmers said in invoking QObject operators from JS/QML:

            I did try using Q_INVOKABLE with no change in behavior. I should have mentioned that.

            I see. Well I'm not sure, to be honest, but you could quickly check if it works with a regular method.
            Also you could try/see if the comparison operators are registered with the meta-type system.
            ... although as I think of it, this doesn't make much sense, as QObjects are not meta-types to begin with ...

            Really? Why is that?

            Because QObjects are all different (i.e. they are identities)
            https://doc.qt.io/qt-6/object.html#identity-vs-value

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • mzimmersM mzimmers has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved