Calling QML functions from C++
Hi,
I use this code in "main.cpp" and that is Ok.
But I do not know How can I use this code in another c++ class.
When I put this code in another class (for example myserialport.cpp), program does not work.
Code:
int main(int argc, char *argv[])
{
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
QObject * object
= engine.
rootObjects().
value(0);
return app.exec();
}
Re: Calling QML functions from C++
It is usally a bad idea to call a QML function from C++.
Why do you want to do that?
Do you need the functions return value?
Cheers,
_
Re: Calling QML functions from C++
Quote:
Originally Posted by
anda_skoa
It is usally a bad idea to call a QML function from C++.
Why do you want to do that?
_
Thank you,
I want to change source icon of toolbutton, show dialog with specific message , change value of several property and enable or disable controls (TextField,GroupBox , ....).
Quote:
Originally Posted by
anda_skoa
Do you need the functions return value?
_
No
Re: Calling QML functions from C++
That all sounds like it would be better handled on the QML side, reacting to changes of C++ data.
But maybe you can post the function you are attempting to call?
Because needing the return value is basically the only use case where one would even consider a C++ -> QML dependency and even that often has better alternatives.
Cheers,
_
Re: Calling QML functions from C++
Thank you,
I have this code in "MySerialPort" class:
Code:
void MySerialPort::readData()
{
if(data[0]=='0') // For example
Show qml dialog
if(data[1]=='1') // For example
disable GroupBox
...
...
}
How can I handled this code on the QML side?
Re: Calling QML functions from C++
The usual way is to have a property on a C++ object that is exposed to QML.
I think we've already discussed this before.
In this case maybe an int or enum property.
QML then reacts to changes of that property the usual way, e.g. via property bindings.
Or you emit signals and handle these in QML, e.g. using a Connection element
Cheers,
_