In Qt C ++ QML interact with

###main.c部分
int main(int argc, char *argv[])
{
    QString info1 = "xxxxxxxxxxx";
    QString info2 = "yyyyyyyyy";
    。。。。
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *root = engine.rootObjects().first();
    QObject *info1 = root->findChild<QObject *>("info1");
    info1->setProperty("text", "info1: "+QString(info1));
    QObject *info2 = root->findChild<QObject *>("info2");
    info2->setProperty("text", "info2: "+QString(info2));
    。。。
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
###main.qml部分
Item{
    id:window
    ....
    Text {
        objectName:"info"
        id: info1
        x: 20
        y: 20
        z: 1
        width: 700
        height: 130
       text: "info1:"
       color: "green"
       font.pixelSize: 50
       font.family: "WenQuanYi Micro Hei"
       font.bold: true
    }

    Text {
        objectName:"info2"
        id: info2
        x: 20
        y: 180
        z: 1
        width: 700
        height: 130
       text: "info2:"
       color: "green"
       font.pixelSize: 50
       font.family: "WenQuanYi Micro Hei"
       font.bold: true
    }
    ....
}

=============================================================================================================

Reference: https: //blog.csdn.net/xi_gua_gua/article/details/56984028

A, C ++ objects called QML
all QML object types, including internal QML engine to achieve or implement third-party libraries, are QObject subclass, allows the engine to use Qt QML element object QML system dynamically instantiated objects of any type.
QML at startup, initializes a QQmlEngine as QML engine, then use QQmlComponent object to load QML document, QML engine will provide a default QQmlContext context object as the top-level execution, used to perform functions and expressions defined in the document QML.
QQmlEngine :: rootContext () Returns the context of the current engine QML, unique, QQmlContext * QQuickView :: rootContext ()
QQuickItem * QQuickView :: rootObject () returns the current root QQuickView, which is the root of QML

1, the use of C ++ objects loaded QML
(1) using QQmlComponent loaded, QML document reading, it will be converted to C ++ objects, for assignment.
Examples:
QQmlEngine Engine; // QML engine
QQmlComponent component (& engine, QUrl ( QStringLiteral ( "qrc: ///main.qml"))); // loading QML
// create a component with QQmlComponent example, assigned to the object and *, this operation is very critical, Object type can be converted to any other types, such as QQuickItem
QObject * = component.create Object ();                   
object-> the setProperty ( "width", 500); // meta-Object system assignment operator
QQmlProperty (object , "width") write (500 );. // meta-Object system assignment
QQuickItem * item = qobject_cast <QQuickItem * > (object); // QObject * is converted into the type QQuickItem *
tiem-> setWidth (500); // QQuickItem * assignment

(2) loading QQuickView, QQuickView inherited QWindow, all QML can load a visual object, and can be fused with a graphical user interface of the application.
Examples:
QQuickView View; // QQuickView objects
view.setSource (QUrl (QStringLiteral ( "qrc : ///main.qml"))); // loading QML
view.show (); // QQuickView may display visual objects QML
QQuickItem * item = view.rootObject (); // returns the current root node QQuickView
tiem-> setWidth (500); // QQuickItem * assignment


2, using the name of the object to access the object loaded QML
All nodes QML bindings to the root tree, QObject :: objectName This property holds particular object. QML component sub-objects may be found with objects defined in QML objectName in C ++ by QObject :: findChild ().

bool QObject :: setProperty setting function (const char * name, const QVariant & value) Element target system
T QObject :: findChild (const QString & name = QString (), Qt :: FindChildOptionsoptions = Qt :: FindChildrenRecursively) const is a type QObject the template function, means that you can turn into any type such as:

QPushButton* button = root.findChild<QPushButton*>("qml_button")

QObject* object = root.findChild<QObject*>("qml_object")

QQuickItem* item = root.findChild<QQuickItem*>("qml_item")

If there are multiple objects using objectName: "qml_button" mark of the same name, QObject :: findChild QML objects return the last mark, QObject :: findChildren return QML objects stored in all marked QList type of list.

Examples:
QQuickView View; // QQuickView objects
view.setSource (QUrl (QStringLiteral ( "qrc : ///main.qml"))); // loading QML
view.show (); // QQuickView may display visual objects QML
QQuickItem * root = view.rootObject (); // returns the current QQuickView root node, the bottom can be bound to many nodes
                                                                             // Find the root of the root node has objectName: "qml_button" this flag is saved QML node qml_Button
QObject * the Button = directory root-> findChild <QObject *> ( "qml_button");       
button-> the setProperty ( "width", 500);
                                                   // Find the root root there objectName: "qml_item" This flag is saved QML node qml_item, replaced QQuickItem * type
QQuickItem * Item = root-> findChild <QQuickItem *> ( "qml_item");   
item-> setProperty ( "color", "red" );


3, using C ++ QML access object member
(1) All objects will be exposed to QML Qt's meta-object system, C ++ QML can be registered in the meta-object system function calls by the meta-object system QMetaObject :: invokeMethod ().
Example:
Function qml defined:
function qmlFunction (MSG)
{
    the console.log ( "GET QML Message:", MSG);
}
C ++ function calls QML:
QQmlEngine Engine; // QML engine
QQmlComponent component (& engine, QUrl ( QStringLiteral ( "qrc: ///main.qml"))) ; // load the QML
QObject * = component.create Object (); // create an instance of a component with QQmlComponent, and assigned to the object *, this operation is very critical, Object types can be converted to any other type, such as QQuickItem
the QVariant rValue;
the QVariant MSG = "C ++ for the Hello";
QMetaObject::invokeMethod(object,  "qmlFunction",  Q_RETURN_ARG(QVariant,rValue),  Q_ARG(QVariant, msg));

(2) C ++ can receive all signals QML, C ++ QML signals can be received, in C ++ can QObject :: connect () received signal slots.
Examples:
QML define a signal:
Signal qmlSignal (String MSG)
C ++ connect signal:
QQuickView View; // QQuickView objects
view.setSource (QUrl (QStringLiteral ( "qrc : ///main.qml"))); // loading QML
view.show (); // QQuickView may display visual objects QML
QQuickItem * root = view.rootObject (); // returns the current root node QQuickView, many nodes can bind under
QObject :: connect (root, SIGNAL ( qmlSignal (QString)), this, SLOT (Slotqml (QString)));

 

Guess you like

Origin www.cnblogs.com/newjiang/p/11014099.html