Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 27
Registered: ‎09-18-2013
My Device: Z10
My Carrier: +
Accepted Solution

Can't call element in qml

Hi everyone.

 

this is my code

 

.h

 

signals:
void complete(const QVariant &info);

 

.m

 

QmlDocument *qml = QmlDocument::create("asset:///backup.qml").parent(this);
	// Create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();

	//connect to qml's function
	connect(this, SIGNAL(complete(QVariant)), root, SLOT(showStaffID(QVariant)));

 

.qml 

 

import bb.cascades 1.0


Page {
    Container {
        objectName: "rootContainer"
        id: rootContainer
        layout: DockLayout {

        }
        ...................
        Container {

            layout: StackLayout {

            }
            Container {
                preferredWidth: maxWidth
                layout: DockLayout {
                }

                ...............
            }
            Container {
                leftPadding: 5
                rightPadding: 5
                topMargin: 50
                horizontalAlignment: HorizontalAlignment.Center
                layout: StackLayout {
                    orientation: LayoutOrientation.TopToBottom
                }
                Label {
                    id: introductionLabel
                    objectName: "introductionLabel"
                    textStyle {
                        base: SystemDefaults.TextStyles.TitleText
                        color: Color.Red
                        fontWeight: FontWeight.Bold
                    }
                    horizontalAlignment: HorizontalAlignment.Center
                    text: qsTr("sdfsdfsds")

                    verticalAlignment: VerticalAlignment.Center
                    multiline: true
                }
                

            }
        }

        
    }

    function showStaffID(info) {
        console.log(info);
       
        introductionLabel.text = "update"
        
    }
}

 My problems is:

In

function showStaffID(info) 

I can see the log of

console.log(info);

But i can't change the text of

introductionLabel

 

What's wrong with me?

 

Thanks

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Can't call element in qml

Try changing the text in onCreationCompleted:

Page {
  onCreationCompleted: {
    introductionLabel.text = "update"
  }

If it won't work, then I suspect that you've created a second instance of the Page and modifying that second instance and not the one being displayed.

 


Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 27
Registered: ‎09-18-2013
My Device: Z10
My Carrier: +

Re: Can't call element in qml

It's ok

 

no problems

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Can't call element in qml

[ Edited ]

Please post the entire .m (.cpp?) file plus the code where the pane is added to the scene.

Try adding console.log("some marker") to onCreationCompleted to verify that it's not created twice. Otherwise, the code looks correct.

 

QmlDocument *qml = QmlDocument::create("asset:///backup.qml").parent(this);
	// Create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();

	//connect to qml's function
	connect(this, SIGNAL(complete(QVariant)), root, SLOT(showStaffID(QVariant)));

 

This code is executed only once on class initialization, right? It creates a new instance of the pane.

 

Could it be that the pane is instantiated in QML and then in C++ code?


Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 27
Registered: ‎09-18-2013
My Device: Z10
My Carrier: +

Re: Can't call element in qml

[ Edited ]

Sorry about my typo (.m is iOS's file).

 

        console.log("some marker");

 it's created twice.

 

This is my cpp code

 

ApplicationUI.cpp


QmlDocument *qml = QmlDocument::create("asset:///home.qml").parent(this); qml->setContextProperty("_applicationui", this); // Create root object for the UI AbstractPane *root = qml->createRootObject<AbstractPane>(); // Set created root object as the application scene Application::instance()->setScene(root);

 and 

"asset:///home.qml")

is my main layout

 

I go from home.qml to backup.qml by this code

var backup = backupPage.createObject();
                            navigationPane.push(backup);

attachedObjects: [

            ComponentDefinition {
                id: backupPage
                source: "asset:///backup.qml"
            }
        ]

 in backup.qml, i have one button

 

                  ImageButton {
                        id: saveButton
                        objectName: "saveButton"

                        onClicked: {
                            progressIndicator.visible = true;
                            _applicationui.backup();

                        }
                    }

 this is my backup function

 

void ApplicationUI::backup(){
...............

QmlDocument *qml = QmlDocument::create("asset:///backup.qml").parent(this);
	// Create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();
	//connect to qml's function
	connect(this, SIGNAL(complete(QVariant)), root, SLOT(showStaffID(QVariant)));

}

 

i moved

QmlDocument *qml = QmlDocument::create("asset:///backup.qml").parent(this);
	// Create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();
	//connect to qml's function
	connect(this, SIGNAL(complete(QVariant)), root, SLOT(showStaffID(QVariant)));

 to contructor of ApplicationUI, but nothing change

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Can't call element in qml

So, it's created and pushed to navigationPane in QML:

var backup = backupPage.createObject();

 In C++ it's created again (but left unused):

QmlDocument *qml = QmlDocument::create("asset:///backup.qml").parent(this);
	// Create root object for the UI
	AbstractPane *root = qml->createRootObject<AbstractPane>();

 You'll need to find and use an existing page instead. For example, by using findChild:

 

Page *page = Application::instance()->scene()->findChild<Page *>("objectNameOfPage");

 

I'm also confused on howStaffID slot gets called, because you're instantiating backup.qml, but this slot seems to be defined in other file (home.qml).

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
New Developer
Posts: 27
Registered: ‎09-18-2013
My Device: Z10
My Carrier: +

Re: Can't call element in qml

Page *page = Application::instance()->scene()->findChild<Page *>("objectNameOfPage");

 save my life