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

时间:2012-09-18 18:00:54

标签: qt qml blackberry-10

我正在开发一个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 ++代码中的label1,所以我有以下代码:

#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 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);
}

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