BlackBerry10级联:无法从c ++中的QML访问控制对象

时间:2013-06-11 13:14:33

标签: blackberry-10 blackberry-cascades

在我的项目中,我有两个qml文件即。 main.qmlDetailsPage.qml

我正在尝试使用DetailsPage.qml方法从c ++更改findChild()标签的文字。

这是c ++文件和DetailsPage.qml

中的代码

Myfile.cpp代码:

using namespace bb::cascades;

AbstractPane *root;

AbstractPane *root1;

 Navigater::Navigater

(bb::cascades::Application *app):QObject(app)

{

  QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

   QmlDocument *qml1 = QmlDocument::create("asset:///DetailsPage.qml").parent(this);

    qml->setContextProperty("_app", this);

    qml1->setContextProperty("_app", this);

    root = qml->createRootObject<AbstractPane>();

    root1 = qml1->createRootObject<AbstractPane>();

    app->setScene(root);

}

void Navigater::tr1()

{

Label *tryLabel1 = root1->findChild<Label*>("labelObj");

if(tryLabel1)

{

qDebug()<<"tttt "<<tryLabel1->text();               //initial text

tryLabel1->setText("Hello!!!!!!!") ;

qDebug()<<"yyyy "<<tryLabel1->text();  //changedText gets reflected on DeviceLog but not on UI   
}   
else

{

qDebug()<<"Not Found";}

}

DetailsPage.qml代码:

// Navigation pane project template

import

 bb.cascades 1.0

Page

 {

   // page with a picture detail

   id: pgDetail


actions: [

  ActionItem {

     title: qsTr("Break")

          onTriggered: {

                 _app.tr1();

                imgView.imageSource = "asset:  //images/picture1br.png"

            }

        }

    ]


paneProperties: NavigationPaneProperties {


backButton: ActionItem {


onTriggered: {                

                navigationPane.pop()

            }

        }

    }


onCreationCompleted: {

        _app.tr1();

    }

   Container {


background: Color.Black


Label {


objectName: "labelObj"    // control to be found


text: "Page 2"


horizontalAlignment: HorizontalAlignment.Center               


textStyle {    


base: SystemDefaults.TextStyles.TitleText


color: Color.Yellow

                      }

        }


ImageView {


id: imgView


imageSource: "asset:///images/picture1.png"    


horizontalAlignment: HorizontalAlignment.Center

        }          

Label {


text: qsTr("Picture description")    


horizontalAlignment: HorizontalAlignment.Center

        }

    }

}

我所做的更改并未反映在模拟器中......但在设备日志中可见。

有没有办法从多个页面访问控制对象,即main.qml以外的页面?

请你仔细研究一下。

2 个答案:

答案 0 :(得分:0)

您可以使用导航窗格,将main.qml作为页面内容和详细信息页面添加为导航窗格中的attachObjects,如

attachedObjects: [
                    ComponentDefinition {
                        id: secondPageDefinition
                        source: "DetailsPage.qml"
                    }

在主页面操作中添加

 onClicked: {
                    // show detail page when the button is clicked
                    var page = secondPageDefinition.createObject();
                     navigationPane.push(page);
                }

我认为这将解决您的问题,并在main.cpp页面中更改内容如下

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

qml->setContextProperty("_app", this);

root = qml->createRootObject<AbstractPane>();

app->setScene(root);

void Navigater::tr1()

{

Label *tryLabel1 = root ->findChild<Label*>("labelObj");

if(tryLabel1)

{

qDebug()<<"tttt "<<tryLabel1->text();               /////////////intial text

tryLabel1->setText("Hello!!!!!!!") ;

qDebug()<<"yyyy "<<tryLabel1->text();  ////////////changedText gets reflected on DeviceLog but not on UI

}

else

{

qDebug()<<"Not Found";}

}

答案 1 :(得分:0)

您可以轻松导航qml而无需使用c ++


1.page1.qml

Page {
    id: rootPage
    Container {
        background: Color.LightGray

        layout: DockLayout {

        }
        Label {
            text: "First page"
            horizontalAlignment: HorizontalAlignment.Center
            verticalAlignment: VerticalAlignment.Center
        }
    }

actions: [
        ActionItem {
            title: "Next page"
            ActionBar.placement: ActionBarPlacement.OnBar
            onTriggered: {
                var page = pageDefinition.createObject();
                navigationPane.push(page);

            }

            attachedObjects: ComponentDefinition {
                id: pageDefinition
                source: "PageTwo.qml"
            }
        }
    ]
}
onPopTransitionEnded: {
    page.destroy();
}