2

In my project, I have two qml files viz. main.qml and DetailsPage.qml.

I am trying to change the text of a label of DetailsPage.qml from c++ using findChild() method.

This is the code in c++ file and DetailsPage.qml

Myfile.cpp code:

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 code:

// 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

        }

    }

}

Change I have made is not getting reflected in simulator...but visible on device log.

Is there any way to access control objects from multiple pages i.e pages other than main.qml?

Could you please look into it.

Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
  • If you set app->setScene(root); insteead of root1. The DetailsPage.qml is not displayed. So any change to it cannot be seen. Right? – Benoit Jun 12 '13 at 16:14
  • I'd assume that page is being displayed some other way, at a point not displayed in his sample code. setScene is not the only way to summon a QML to the screen. – TheSmurf Jun 18 '13 at 19:40
  • In that case, the code that summons it could be pasted. It'd help to help – Benoit Jun 26 '13 at 10:49

2 Answers2

0

You can use a navigation pane, add main.qml as page contents and details page as attachedObjects in the navigation pane like

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

in main page action add

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

i think this will solve your issue and in the main.cpp page change the content as follows

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

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

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

app->setScene(root);

and

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";}

}
pranavjayadev
  • 937
  • 7
  • 31
0

you can navigate in qml easily without using 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();
}

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90