PDA

View Full Version : How to call cpp file function from qml??



naufalahad
24th January 2012, 15:20
Hi All,
As i am new to qt quick i'm in need of calling a function from qml to cpp file, I am not able to write code for this. I tried this code...


I am trying to add two numbers!!!I tried writing it in qml but could not get the result.. so i am looking to write a function for adding the nos. in the cpp file .
i would call from qml to cpp function...

QML CODE:

property string i;
property string j;

Button {
id: one
x: 59
y: 131
width: 41
height: 42
text: "1"
onClicked:
{

i = text
textfield.text = textfield.text + i
}
}
Button {
id: two
x: 119
y: 131
text: "2"
onClicked:
{

i = text
textfield.text = textfield.text + i

}

}

Button {
id: add
x: 247
y: 131
text: "+"
onClicked:
{
j = textfield.text
ans = j

textfield.text = ""

funct.buttonclicked()

CPP CODE:

void funct::buttonclicked()
{

QString *s = new QString;
s->show();
.....???
.....???

}

I am not able write from this!!!!

Thanks in advance and Regards,
Naufal.A

wysota
24th January 2012, 15:34
Export your C++ object to QML using QDeclarativeContext::setContextProperty().

naufalahad
24th January 2012, 15:37
Hi,
Can i have some more explanation on it sir.
I could not understand it clearly!!!!

Thanks and Regards,
Naufal.A

wysota
24th January 2012, 16:16
What's wrong with the examples from Qt docs on using context properties in QML?

naufalahad
30th January 2012, 06:25
Hi,
I am getting that cleared now.. i have one more doubt in this!!!
How to send the integer value from cpp fn to qml fn??
I have tried using QDeclarative view and also Qdeclarative context and i am getting only errors..

Error: cannot convert 'QGraphicsObject*' to 'QObject*' in initialization


QDeclarativeView view;
QObject *object = view.rootObject();
QObject* listView = object->findChild<QObject *>("events");
QVariant returnedValue;
QMetaObject::invokeMethod(listView, "addd",Q_RETURN_ARG(QVariant, returnedValue));

Can some one help me to find whats the mistake is

Thanks in advance and Regards,
Naufal.A

wysota
30th January 2012, 09:52
It's likely you are missing #include <QGraphicsObject>