introduce
QML and C + + objects can interact through, signals,slots, and property modifications. For a C + + object, any data can be exposed via QT's meta-object system to QML (the general method, described later), and any QML object data is accessed directly from the C + + side through the Meta-object system.
In actual projects many places use QML to interact with QT C + +. Summed up here a number of methods for your reference, you are welcome to guide and shoot bricks.
There are three different ways to do this:
1. Expose the objects or types in qt C + + to the QML end for use on the QML side. (The official saying is "embed" rather than "expose", more civilized. -B)
2. The signal Handler in QML (equivalent to the QT C + + send signal to the QML end, QML signal end of Handler to handle).
3. Create the Qml object on the QT C + + side, since the object is all there. Well, what do you want? (no use, looks also not very practical, but introduction, useful classmate Message ha).
All right, let's get started. Knowledge Preparation
Don't worry, let's take a look at something, and if you know it, you can skip this section.
The QML API has three main members--qdeclarativeengine,qdeclarativecomponent and Qdeclarativecontext.
The Qdeclarativeengine provides a QML operating environment.
Qdeclarativecomponent encapsulates the QML Documents.
Qdeclarativecontext allows programs to display data using the QML component.
The QML contains a very useful api--qdeclarativeview. Through it, the application can easily embed the QML component into the Qgraphicsview. Qdeclarativeview is primarily used to develop rapid prototyping during application development. exposing the QT C + + object or type to QML create data types that need to be exposed to QML
#ifndef myclass_h
#define MYCLASS_H
#include <QObject>
#include <QString>
class MYCLASS : Public QObject
{
q_object
q_property (QString myString READ myString WRITE setmystring NOTIFY mystringchanged) Public
:
explicit MyClass (QObject *parent = 0);
Q_invokable QString getmystring ();
Signals:
void mystringchanged ();
Public slots:
void setmystring (QString astring);
QString myString ();
Private:
QString m_string;
#endif//Myclass_h
If you want the method in the data element to be QML directly, there are 2 ways:
1. Add the Q_invokable macro before the function is declared.
2. Declare as public slots.
QML can directly access the properties of the modified data element, as stated by Qproperty.
For specific implementation please refer to the sample code.
exposing existing QT C + + objects to QML
Main.cpp
MyClass myobj;
Qdeclarativeengine *engine=viewer.engine ();
Qdeclarativecontext *context=engine->rootcontext ();
Context->setcontextproperty ("Myobjectexposebycxproperty", &myobj);
Myobjectexposebycxproperty objects can be used directly in QML.
Mainpage.qml ...
button{...
Id:btn1 ...
Text:qstr ("property")
//Here the Qproperty attribute called MyString to MyClass is not a method, so there is no parentheses.
onclicked:label.text=myobjectexposebycxproperty.mystring;
}
...
Register QT C + + class type to QML
Another way is to register a type
Main.cpp
qmlregistertype<myclass> ("Registermytype", 1, 0, "Myclasstype");
This is used in QML
Mainpage.qml ...
Import Registermytype 1.0
button{
id:btn2
...
Text:qstr ("inovkable")
//The method called at the time invokable, is not a property, so there are parentheses.
onclicked:label.text=myclassexposebyregtype.getmystring ();
}
Create an object, because QML is interpreted to execute, so it doesn't matter if you put it behind you.
Myclasstype
{
Id:myclassexposebyregtype
}
Steps:
1. Imports Import.
2. Create objects.
3. ID used directly.
the signal Handler in QML
Or use the example above, click on the button control in the QML, change the string of objects in it, this time in QT C + + sent a signal signal to the QML end, QML received the use of signal handler response, change Label2 value. The specific code is as follows.
The value of the string is modified in QML.
MAINPAGE.QML
button{
id:btn3
text:qstr ("Emit stringchanged Signal")
onclicked: myobjectexposebycxproperty.mystring= "xxxxx";
}
Qt C + + triggering signal
Myclass.cpp
void myclass::setmystring (QString astring)
{
if (astring==m_string)
{return
;
}
m_string=astring;
Emit mystringchanged ();
}
Connection Signal Handler response
MAINPAGE.QML
connections
{
target:myobjectexposebycxproperty
onmystringchanged: Label2.text= "Signal handler received"
}
functions that call QML directly in Qt C + +
The same QML function can also be invoked by QT C + + END.
All QML functions expose the QT C + + side through the Meta-object system and can be invoked directly on the QT C + + side using the Qmetaobject::invokemethod () method. Here is an example of this.
MYITEM.QML
Import Qtquick 1.0
Item {
function myqmlfunction (msg) {
console.log ("Got message:", msg) Return
' Some return value '
}
}
Main.cpp
qdeclarativeengine engine;
Qdeclarativecomponent component (&engine, "myitem.qml");
QObject *object = Component.create ();
Qvariant Returnedvalue;
Qvariant msg = "Hello from C + +";
Qmetaobject::invokemethod (Object, "Myqmlfunction",
q_return_arg (Qvariant, Returnedvalue),
Q_arg ( Qvariant, msg));
Qdebug () << "QML function returned:" << returnedvalue.tostring ();
Delete object;