无法在C++代码中访问QML对象。

5

我正在开发一个QML应用程序(黑莓10),并且有一个像这样的QML文件:

import bb.cascades 1.0    
Page {
        content: Container {
            id: containerID
            Button {
                id: button1
                text: "text"
                onClicked: {
                }
            }
            Label {
                id: label1
                text: "text"
            }
        }
    }

现在我想在我的C++代码中访问,所以我有以下代码:

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    //-- setContextProperty expose C++ object in QML as an variable
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable
    //qml->setContextProperty("app", this);

    AbstractPane *root = qml->createRootNode<AbstractPane>();

    QObject *labelTest = root->findChild<QObject*>("label1");
    if (labelTest)
        labelTest->setProperty("text", "yes!!");

    Application::setScene(root);
}

现在我运行了应用程序,但标签的文本没有改变。

出了什么问题?

2个回答

5
我已经自己找到了答案。 QML代码如下:
import bb.cascades 1.0

//-- create one page with a label and text

Page {
    property alias labelText: label.text      
    content: Container {            
        Label {
            id: label
            text: "Label"
        }  

        Button {
            objectName: "button"
            text: "Button"
        }                               
    }
}

并且这段是C++代码:

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    //-- setContextProperty expose C++ object in QML as an variable
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable
    //qml->setContextProperty("app", this);

    AbstractPane *root = qml->createRootNode<AbstractPane>();

    root->setProperty("labelText", "yes");

    QObject *newButton = root->findChild<QObject*>("button");
    if (newButton)
        newButton->setProperty("text", "New button text");

    Application::setScene(root);
}

0

另一种解决方案是...

QML:

import bb.cascades 1.0

//-- create one page with a label and text

Page {
    property alias labelText: label.text      
    content: Container {            
        Label {
            id: label
            text: "Label"
            objectName: "label1"
        }  

        Button {
            objectName: "button"
            text: "Button"
        }                               
    }
}

C++

#include "app.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    //-- setContextProperty expose C++ object in QML as an variable
    //-- uncomment next line to introduce 'this' object to QML name space as an 'app' variable
    //qml->setContextProperty("app", this);

    AbstractPane *root = qml->createRootNode<AbstractPane>();

    QObject *labelTest = root->findChild<QObject*>("label1");
    if (labelTest)
        labelTest->setText(QString("yes!!"));

    Application::setScene(root);
}

但是你提出的解决方案在很多方面都更好。


我想labelTest必须转换为Label,我错了吗?或者你可以直接声明它:Label *labelTest = root->findChild<Label*>("label1"); - Dielson Sales

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接