第五章:C++中调用QML交互

实现方法:

第一步:获取QML中对象

QList<QObject*> rootObjects = engine.rootObjects();
    int count = rootObjects.size();
    // 找到指定对象
    for(int i = 0; i < count; i++)
    {
        if(rootObjects.at(i)->objectName() == "rootObject")
        {
            root = rootObjects.at(i);
            break;
        }
    }

第二步:C++中改变QML中属性

root->setProperty(color, color);    //qml颜色属性

示例:动态改变背景颜色

changeColor.h

#ifndef CHANGECOLOR_H
#define CHANGECOLOR_H
#include <QObject>
#include <QTimer>
class ChangeQmlColor : public QObject
{
    Q_OBJECT
public:
    ChangeQmlColor(QObject *target, QObject *parent = 0);
    ~ChangeQmlColor();
protected slots:
    void onTimeout();
private:
    QTimer m_timer;
    QObject *m_target;
};
#endif // CHANGECOLOR_H

changeColor.cpp

#include changeColor.h
#include <QDateTime>
#include <QColor>
#include <QVariant>
ChangeQmlColor::ChangeQmlColor(QObject *target, QObject *parent)
    : QObject(parent), m_timer(this), m_target(target)
{
    qsrand(QDateTime::currentDateTime().toTime_t());
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
    m_timer.start(1000);
}
ChangeQmlColor::~ChangeQmlColor(){}
void ChangeQmlColor::onTimeout()
{
    QColor color = QColor::fromRgb(qrand()%256, qrand()%256, qrand()%256);
    m_target->setProperty(color, color);    //qml颜色属性
}

main.cpp

#include <QtGui/QGuiApplication>
#include <QQmlApplicationEngine>
#include changeColor.h
#include <QMetaObject>
#include <QDebug>
#include <QColor>
#include <QVariant>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl("qrc:///main.qml)");
    QObject * root = NULL;
    QList<QObject*> rootObjects = engine.rootObjects();
    int count = rootObjects.size();
    qDebug() << "rootObjects"-  << count;
    // 找到指定对象
    for(int i = 0; i < count; i++)
    {
        if(rootObjects.at(i)->objectName() == "rootObject")
        {
            root = rootObjects.at(i);
            break;
        }
    }
    // 定时器改变背景颜色
    new ChangeQmlColor(root);
    // 退出按钮
    QObject * quitButton = root->findChild<QObject*>(quitButton);
    if(quitButton)
    {
        QObject::connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()));
    }
    // 文本框
    QObject *textLabel = root->findChild<QObject*>(textLabel);
    if(textLabel)
    {
        textLabel->setProperty(color, QColor::fromRgb(0,255,0)); // 设置文本显示颜色
    }
    return app.exec();
}

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Window 2.1
Window {
    objectName: "rootObject";
    width: 360;
    height: 360;
    visible: true;
    Text {
        objectName: "textLabel";
        text: "Hello World";
        anchors.centerIn: parent;
        font.pixelSize: 26;
    }
    Button {
        objectName: "quitButton";
        text: "quit";
        anchors.right: parent.right;
        anchors.rightMargin: 4;
        anchors.bottom: parent.bottom;
        anchors.bottomMargin: 4;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40602000/article/details/109277373