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. Calling C++ overloaded function from QML
Forum Updated to NodeBB v4.3 + New Features

Calling C++ overloaded function from QML

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 2 Posters 7.6k 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.
  • K Offline
    K Offline
    Khachatur
    wrote on 25 Jun 2015, 09:48 last edited by
    #1

    Hi All,

    In my C++ class I have function:
    Q_INVOKABLE void StartOperation(OperationController::OperationID theOperationID,
    QVariant theParam = QVariant());

    And I try to call it from QML:
    StartOperation(operationID)

    But function is not called and I receive an error:
    Error: Unable to determine callable overload. Candidates are:
    StartOperation(OperationController::OperationID)
    StartOperation(OperationController::OperationID,QVariant)

    In Qt docs in topic "Using QML Bindings in C++ Applications" I found:
    "QML supports the calling of overloaded C++ functions. If there are multiple C++ functions with the same name but different arguments, the correct function will be called according to the number and the types of arguments that are provided."

    Why my code is not working? Please, help.

    P 1 Reply Last reply 25 Jun 2015, 10:47
    0
    • K Khachatur
      25 Jun 2015, 09:48

      Hi All,

      In my C++ class I have function:
      Q_INVOKABLE void StartOperation(OperationController::OperationID theOperationID,
      QVariant theParam = QVariant());

      And I try to call it from QML:
      StartOperation(operationID)

      But function is not called and I receive an error:
      Error: Unable to determine callable overload. Candidates are:
      StartOperation(OperationController::OperationID)
      StartOperation(OperationController::OperationID,QVariant)

      In Qt docs in topic "Using QML Bindings in C++ Applications" I found:
      "QML supports the calling of overloaded C++ functions. If there are multiple C++ functions with the same name but different arguments, the correct function will be called according to the number and the types of arguments that are provided."

      Why my code is not working? Please, help.

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 25 Jun 2015, 10:47 last edited by
      #2

      Hi @Khachatur, Function overloading works in QML. I think the first parameter operationID that you are passing in QML is wrong. Can you post the relevant code in QML and specificaly what is operationID in QML ?

      157

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Khachatur
        wrote on 25 Jun 2015, 11:14 last edited by
        #3

        Hi p3c0,

        I have changed the function StartOperation, instead of enumeration type I am using the int type.
        Q_INVOKABLE void StartOperation(int theOperationID, QVariant theParam = QVariant());

        And it is start working.

        In my class OperationController I have enumeration Q_ENUMS(OperationID).
        I have registered OperationController as QML type: qmlRegisterType<OperationController>("OperationController", 1, 0, "OperationController");

        In QML I have control with property var operationID: OperationController.OperationName, where OperationName is value of OperationID enumeration

        I supposed that property var operationID will be converted to OperationID enumeration type, but I was wrong.

        Any way thank you for your replay =)

        P 1 Reply Last reply 25 Jun 2015, 11:20
        0
        • K Khachatur
          25 Jun 2015, 11:14

          Hi p3c0,

          I have changed the function StartOperation, instead of enumeration type I am using the int type.
          Q_INVOKABLE void StartOperation(int theOperationID, QVariant theParam = QVariant());

          And it is start working.

          In my class OperationController I have enumeration Q_ENUMS(OperationID).
          I have registered OperationController as QML type: qmlRegisterType<OperationController>("OperationController", 1, 0, "OperationController");

          In QML I have control with property var operationID: OperationController.OperationName, where OperationName is value of OperationID enumeration

          I supposed that property var operationID will be converted to OperationID enumeration type, but I was wrong.

          Any way thank you for your replay =)

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 25 Jun 2015, 11:20 last edited by
          #4

          @Khachatur

          In QML I have control with property var operationID: OperationController.OperationName, where OperationName is value of OperationID enumeration
          I supposed that property var operationID will be converted to OperationID enumeration type, but I was wrong.

          That should work too.

          157

          K 1 Reply Last reply 25 Jun 2015, 12:17
          0
          • P p3c0
            25 Jun 2015, 11:20

            @Khachatur

            In QML I have control with property var operationID: OperationController.OperationName, where OperationName is value of OperationID enumeration
            I supposed that property var operationID will be converted to OperationID enumeration type, but I was wrong.

            That should work too.

            K Offline
            K Offline
            Khachatur
            wrote on 25 Jun 2015, 12:17 last edited by
            #5

            @p3c0
            I tried again to use the enumeration type instead of int, but unfortunately I got the same error.

            P 1 Reply Last reply 25 Jun 2015, 12:49
            0
            • K Khachatur
              25 Jun 2015, 12:17

              @p3c0
              I tried again to use the enumeration type instead of int, but unfortunately I got the same error.

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 25 Jun 2015, 12:49 last edited by
              #6

              @Khachatur Strange.. I created a similar example which works:

              MyObject class

              #include <QObject>
              #include <QVariant>
              #include <QDebug>
              
              class MyObject : public QObject
              {
                  Q_OBJECT
                  Q_ENUMS(MyEnum)
              public:
                  enum MyEnum{
                      One,
                      Two
                  };
              
                  Q_INVOKABLE void sampleFunction(MyObject::MyEnum e,
                                                  QVariant param = QVariant()) {
                      qDebug() << e << param;
                  }
              };
              

              main.cpp //register MyObject and load the qml

              QGuiApplication app(argc, argv);
              qmlRegisterType<MyObject>("MyObject",1,0,"MyObject");
              
              QQuickView view;
              view.setSource(QUrl("qrc:/main.qml"));
              view.show();
              return app.exec();
              

              main.qml //access MyObject and invoke that function

              import QtQuick 2.4
              import MyObject 1.0
              
              Item {
                  width: 100
                  height: 100
              
                  property var prop: MyObject.Two
              
                  MyObject {
                      id :obj
                  }
              
                  Component.onCompleted: obj.sampleFunction(prop,"Hello")
              }
              

              157

              K 1 Reply Last reply 25 Jun 2015, 13:41
              0
              • P p3c0
                25 Jun 2015, 12:49

                @Khachatur Strange.. I created a similar example which works:

                MyObject class

                #include <QObject>
                #include <QVariant>
                #include <QDebug>
                
                class MyObject : public QObject
                {
                    Q_OBJECT
                    Q_ENUMS(MyEnum)
                public:
                    enum MyEnum{
                        One,
                        Two
                    };
                
                    Q_INVOKABLE void sampleFunction(MyObject::MyEnum e,
                                                    QVariant param = QVariant()) {
                        qDebug() << e << param;
                    }
                };
                

                main.cpp //register MyObject and load the qml

                QGuiApplication app(argc, argv);
                qmlRegisterType<MyObject>("MyObject",1,0,"MyObject");
                
                QQuickView view;
                view.setSource(QUrl("qrc:/main.qml"));
                view.show();
                return app.exec();
                

                main.qml //access MyObject and invoke that function

                import QtQuick 2.4
                import MyObject 1.0
                
                Item {
                    width: 100
                    height: 100
                
                    property var prop: MyObject.Two
                
                    MyObject {
                        id :obj
                    }
                
                    Component.onCompleted: obj.sampleFunction(prop,"Hello")
                }
                
                K Offline
                K Offline
                Khachatur
                wrote on 25 Jun 2015, 13:41 last edited by p3c0
                #7

                @p3c0

                In my case the Item is a Rectangle which behave like a button. My rectangle has a signal btnClicked(var theSender), where theSender is id of Rectangle.

                It would be better to change a little bit your example for more clarity. Could you please check my changes?
                Create file TestButton,qml and put code below in it:

                Rectangle {
                    id: myRectangle
                
                   signal btnClicked(var theSender)
                
                    width: 100
                    height: 100
                    property var prop: MyObject.Two
                
                    MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          btnClicked(myRectangle)
                      }
                   }
                }
                

                In some other qml file:

                 MyObject {
                        id : obj
                        TestButton 
                        {
                           id: myButton
                
                           Component.onCompleted: {
                             myButton.btnClicked.connect(onBtnClicked)
                           }
                      }
                }
                
                
                function onBtnClicked(theButton)
                {
                    obj.sampleFunction(theButton.prop, "Hello")
                }
                

                sorry for code formatting, but I didn't find the button for insert a code.

                P 1 Reply Last reply 26 Jun 2015, 06:04
                0
                • K Khachatur
                  25 Jun 2015, 13:41

                  @p3c0

                  In my case the Item is a Rectangle which behave like a button. My rectangle has a signal btnClicked(var theSender), where theSender is id of Rectangle.

                  It would be better to change a little bit your example for more clarity. Could you please check my changes?
                  Create file TestButton,qml and put code below in it:

                  Rectangle {
                      id: myRectangle
                  
                     signal btnClicked(var theSender)
                  
                      width: 100
                      height: 100
                      property var prop: MyObject.Two
                  
                      MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            btnClicked(myRectangle)
                        }
                     }
                  }
                  

                  In some other qml file:

                   MyObject {
                          id : obj
                          TestButton 
                          {
                             id: myButton
                  
                             Component.onCompleted: {
                               myButton.btnClicked.connect(onBtnClicked)
                             }
                        }
                  }
                  
                  
                  function onBtnClicked(theButton)
                  {
                      obj.sampleFunction(theButton.prop, "Hello")
                  }
                  

                  sorry for code formatting, but I didn't find the button for insert a code.

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 26 Jun 2015, 06:04 last edited by p3c0
                  #8

                  @Khachatur Can you explain what you are trying to do ?
                  AFAIK you cannot add TestButton to MyObject because it doesn't have a property which can hold it.
                  Use ``` (3 backticks) instead of [code] at the start and end. I have added it now.

                  157

                  K 2 Replies Last reply 26 Jun 2015, 09:39
                  0
                  • P p3c0
                    26 Jun 2015, 06:04

                    @Khachatur Can you explain what you are trying to do ?
                    AFAIK you cannot add TestButton to MyObject because it doesn't have a property which can hold it.
                    Use ``` (3 backticks) instead of [code] at the start and end. I have added it now.

                    K Offline
                    K Offline
                    Khachatur
                    wrote on 26 Jun 2015, 09:39 last edited by
                    #9

                    @p3c0

                    Yes, you are right. It is my bad. But you can put TestButton to another QML control, it is doesn't matter.

                    About what I am trying to do: I have a several buttons and I want to start appropriate operation on button clicked.

                    For that: for each button I have defined property var operationID, which type is enumeration defined in C++ OperationController class. On button clicked the event btnClicked(theSender) is raised. The Sender is id of clicked button.

                    In my OperationController I have function:
                    StartOperation(OperationController::OperationID theOperationID, QVariant theParam = QVariant())

                    In QML when I catch btnClicked event I am calling function:
                    operationController.StartOperation(theSender.operationID)

                    On calling StartOepration from QML I got such error: Error: Unable to determine callable overload. Candidates are:
                    StartOperation(OperationController::OperationID)
                    StartOperation(OperationController::OperationID,QVariant)

                    But if I will use int instead of enumeration type OperationController::OperationID in function StartOperation everything works fine:
                    StartOperation(int theOperationID, QVariant theParam = QVariant())

                    1 Reply Last reply
                    0
                    • P p3c0
                      26 Jun 2015, 06:04

                      @Khachatur Can you explain what you are trying to do ?
                      AFAIK you cannot add TestButton to MyObject because it doesn't have a property which can hold it.
                      Use ``` (3 backticks) instead of [code] at the start and end. I have added it now.

                      K Offline
                      K Offline
                      Khachatur
                      wrote on 26 Jun 2015, 09:49 last edited by
                      #10

                      @p3c0

                      You know, today morning I tried again, and everything is working, even with enumeration type.
                      I don't know what was the problem...

                      Sorry for wasting your time.

                      P 1 Reply Last reply 26 Jun 2015, 10:29
                      0
                      • K Khachatur
                        26 Jun 2015, 09:49

                        @p3c0

                        You know, today morning I tried again, and everything is working, even with enumeration type.
                        I don't know what was the problem...

                        Sorry for wasting your time.

                        P Offline
                        P Offline
                        p3c0
                        Moderators
                        wrote on 26 Jun 2015, 10:29 last edited by
                        #11

                        @Khachatur That's fine. I'm glad that its working :)
                        Happy Coding ...

                        157

                        1 Reply Last reply
                        0

                        1/11

                        25 Jun 2015, 09:48

                        • Login

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