Calling C++ overloaded function from QML
-
wrote on 25 Jun 2015, 09:48 last edited by
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.
-
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.
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 isoperationID
in QML ? -
wrote on 25 Jun 2015, 11:14 last edited by
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 =)
-
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 =)
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.
-
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.
-
@p3c0
I tried again to use the enumeration type instead of int, but unfortunately I got the same error.@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") }
-
@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") }
wrote on 25 Jun 2015, 13:41 last edited by p3c0In 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.
-
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.
@Khachatur Can you explain what you are trying to do ?
AFAIK you cannot addTestButton
toMyObject
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. -
@Khachatur Can you explain what you are trying to do ?
AFAIK you cannot addTestButton
toMyObject
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.wrote on 26 Jun 2015, 09:39 last edited byYes, 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())
-
@Khachatur Can you explain what you are trying to do ?
AFAIK you cannot addTestButton
toMyObject
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. -
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.
@Khachatur That's fine. I'm glad that its working :)
Happy Coding ...
1/11