本文介绍了C++ 和 QML 之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面展示了如何在 QML 中调用 C++ 函数.p>

我想要做的是通过 C++ 函数更改按钮上的图像(触发状态更改或完成).

我怎样才能做到这一点?

更新

我尝试了 Radon 的方法,但是当我插入这一行时立即:

 QObject *test = dynamic_cast(viewer.rootObject());

编译器这样抱怨:

 错误:不能 dynamic_cast '((QMLCppBinder*)this)->QMLCppBinder::viewer.QDeclarativeView::rootObject()'(类型为 'struct QGraphicsObject*')类型为 'class QObject*'(source 是指向不完整类型的指针)

如果相关,QMLCppBinder 是我尝试构建的一个类,用于封装从多个 QML 页面到 C++ 代码的连接.这似乎比人们预期的要棘手.

这是一个框架类,可以为此提供一些上下文:

 类 QMLCppBinder:公共 QObject{Q_OBJECT上市:QDeclarativeView 查看器;QMLCppBinder() {viewer.setSource(QUrl("qml/Connect/main.qml"));查看器.showFullScreen();//错误QObject *test = dynamic_cast(viewer.rootObject());}}
解决方案

如果你为图片设置了一个objectName,你可以很容易地从C++中访问它:

ma​​in.qml

导入QtQuick 1.0长方形 {身高:100;宽度:100图片 {对象名称:图像"}}

在 C++ 中:

//[...]QDeclarativeView 视图(QUrl("main.qml"));view.show();//获取根对象QObject *rootObject = dynamic_cast(view.rootObject());//按名称查找元素QObject *image = rootObject->findChild(QString("theImage"));if (image) {//找到元素image->setProperty("source", QString("path/to/image"));} 别的 {qDebug() <<'theImage' 未找到";}//[...]

QObject.findChild(), QObject.setProperty()

This page shows how to call C++ functions from within QML.

What I want to do is change the image on a Button via a C++ function (trigger a state-change or however it is done).

How can I achieve this?

UPDATE

I tried the approach by Radon, but immediately when I insert this line:

    QObject *test = dynamic_cast<QObject *>(viewer.rootObject());

Compiler complains like this:

    error: cannot dynamic_cast '((QMLCppBinder*)this)->QMLCppBinder::viewer.QDeclarativeView::rootObject()' (of type 'struct QGraphicsObject*') to type 'class QObject*' (source is a pointer to incomplete type)

In case it is relevant, QMLCppBinder is a class that I try to build to encapsulate the connections from several QML pages to C++ code. Which seems to be trickier than one might expect.

Here is a skeleton class to give some context for this:

    class QMLCppBinder : public QObject
    {
        Q_OBJECT
    public:
        QDeclarativeView viewer;

        QMLCppBinder() {
            viewer.setSource(QUrl("qml/Connect/main.qml"));
            viewer.showFullScreen();
            // ERROR
            QObject *test = dynamic_cast<QObject *>(viewer.rootObject());
        }
    }
解决方案

If you set an objectName for the image, you can access it from C++ quite easy:

main.qml

import QtQuick 1.0

Rectangle {
    height: 100; width: 100

    Image {
        objectName: "theImage"
    }
}

in C++:

// [...]

QDeclarativeView view(QUrl("main.qml"));
view.show();

// get root object
QObject *rootObject = dynamic_cast<QObject *>(view.rootObject());

// find element by name
QObject *image = rootObject->findChild<QObject *>(QString("theImage"));

if (image) { // element found
    image->setProperty("source", QString("path/to/image"));
} else {
    qDebug() << "'theImage' not found";
}

// [...]

QObject.findChild(), QObject.setProperty()

这篇关于C++ 和 QML 之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:13