c++修改、访问qml属性、调用qml方法(参数传递)、qml信号绑定到c++对象的槽上,传递各种类型的参数:
main.cpp 设置qml属性、访问qml属性、调用qml函数、关联qml信号与c++对象的槽函数
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQmlContext>
#include <QObject>
#include <QDebug>
#include <QQmlProperty>
#include <QQuickItem>
#include <QMetaObject>
#include "cppclass.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView viewer;
viewer.setTitle("qml_c++对象交互"); //标题
viewer.setResizeMode(QQuickView::SizeRootObjectToView); //resize
viewer.setSource(QUrl("qrc:/main.qml")); //qml
viewer.show();
viewer.setProperty("width",640); //设置属性的值
viewer.setProperty("height",480);
//查找qml对象,并修改其属性
//viewer.rootObject()->findChild<QObject *>("text")->setProperty("text","Hello World.");
//修改qml属性
viewer.rootObject()->setProperty("value",1000);
QQmlProperty::write(viewer.rootObject(),"value",1024);
qDebug()<<"property value:"<<QQmlProperty::read(viewer.rootObject(),"value").toInt();
//调用qml函数myQmlFunc
QVariant ret;
QVariant msg = "Hello Wolrd";
QMetaObject::invokeMethod(viewer.rootObject(),"myQmlFunc",Q_RETURN_ARG(QVariant,ret),Q_ARG(QVariant,msg));
qDebug()<<ret.toString();
//关联qml的信号到c++的槽
cppClass cls;
//QString参数
//QObject::connect(viewer.rootObject(),SIGNAL(qmlSignal(QString)),&cls,SLOT(cppSlots(QString)));
//对象+int参数
QObject::connect(viewer.rootObject(),SIGNAL(qmlSignal(QVariant,QVariant,int,int)),&cls,SLOT(cppSlots(QVariant,QVariant,int,int)));
return app.exec();
}
cppclass.h 定义c++槽函数,设置qml对象的属性
#ifndef CPPCLASS_H
#define CPPCLASS_H
#include <QObject>
#include <QString>
#include <QDebug>
#include <QQuickItem>
class cppClass : public QObject
{
Q_OBJECT
public slots:
//string类型的槽
//void cppSlots(QString str)
//{
// qDebug()<<"cpp slots recv:" + str;
//}
//对象+int类型的槽
void cppSlots(const QVariant item,const QVariant text,int x,int y)
{
QQuickItem * it = qobject_cast<QQuickItem *>(item.value<QObject*>());
QString s = QString("width:%1 height:%2 click:%3 %4").arg(it->width()).arg(it->height()).arg(x).arg(y);
//设置qml text对象的text属性
(qobject_cast<QQuickItem *>(text.value<QObject*>()))->setProperty("text",s);
}
};
#endif // CPPCLASS_H
main.qml 在鼠标点击、窗口宽度和高度改变时发送qml信号,由c++槽函数处理信号
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
Rectangle
{
property int value: 258
Text
{
id: text
anchors.centerIn: parent
text: "Hello World."
font.family: "微软雅黑"
font.pointSize: 25
}
function myQmlFunc(msg)
{
console.log("myQmlFunc " + msg)
return "myQmlFunc " + msg;
}
//qml信号及发送信号代码
//signal qmlSignal(string str)
signal qmlSignal(var obj,var text,int x,int y)
MouseArea
{
id: mouseArea
anchors.fill: parent
//鼠标点击
onClicked:
{
//QString类型参数
//parent.qmlSignal("qmlSignal:clicked x:" + mouse.x + " y:" + mouse.y)
//对象+int参数
parent.qmlSignal(parent,text,mouse.x,mouse.y)
}
}
//高度改变
onHeightChanged:
{
qmlSignal(this,text,mouseArea.mouseX,mouseArea.mouseY)
}
//宽度改变
onWidthChanged:
{
qmlSignal(this,text,mouseArea.mouseX,mouseArea.mouseY)
}
}
效果:
代码:https://github.com/yangyang0312/cpp/tree/master/Qt/qml/QmlCppObjInteract