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
Developer
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE
My Carrier: Simobil
Accepted Solution

QML and C++ integration

I have a problem with QML and C++ integration.

 

I have second qml(beside main.qml) attached in main:

attachedObjects: [             
             ComponentDefinition {
                    id: dataPage
                    source: "dataPage.qml"
                }
            ]

 

When item is triggered in listview this page is pushed in navigationPane:

onTriggered: {
	     var selectedItem = dataModel.data(indexPath); 
             var page = dataPage.createObject();
              page.name = selectedItem.title;
              navigationPane.push(page);
                            }

 

 

ApplicatonUI::ApplicationUI(bb::cascades::Application *app)
:QObject(app)
.
.
.
dataPageqml = QmlDocument::create("asset:///dataPage.qml").parent(qml); dataPageqml->setContextProperty("pageC", this); .
.
.
}

void ApplicationUI::processChoice(){ Page *page = dataPageqml->createRootObject<Page>(); page->setProperty("label", "New text"); }

 

In dataPage.qml I call onCreationCompleted to call processChoice() function:

Page {
    id: page
    onCreationCompleted: {
        pageC.processChoice();
    }
    property string name
    Container {
        WebView{
            horizontalAlignment: HorizontalAlignment.Fill
            minHeight: 700.0
            maxHeight: 700.0

        }
        Label{
            id: lb
            objectName: "label"
            text: page.name
        }
    }
}

 

In this case I want to change text in label, but nothing happens.

What am I doing wrong ?

 

Ty for answers.

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: QML and C++ integration

It's cleaner if your keep the UI in the qml and backend login in your cpp. Say,

 

1) hpp/cpp 

hpp
	Q_PROPERTY( QString pageName READ pageName NOTIFY pageNameChanged)
	
private:
   QString m_pageName;
   QString pageName(void) const;
   
   
cpp
  void ApplicationUI::pageNamevoid) const 
  {
     return m_pageName;
  }
  
  void ApplicationUI::processChoice() 
  {
     m_pageName = "New text";
	 emit pageNameChanged();
  }
  
2) In dataPage.qml
   Page {
    id: page
    onCreationCompleted: {
        pageC.processChoice();
    }
    Container {
        WebView{
            horizontalAlignment: HorizontalAlignment.Fill
            minHeight: 700.0
            maxHeight: 700.0

        }
        Label{
            id: lb
            objectName: "label"
            text: pageC.pageName
        }
    }
}
  
   

 

Developer
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE
My Carrier: Simobil

Re: QML and C++ integration

[ Edited ]

I see what you mean, but still there is a problem of connecting that second qml with main program.

 

ApplicationUI::ApplicationUI(bb::cascades::Application *app)
: QObject(app)
{
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    qml = QmlDocument::create("asset:///main.qml").parent(this);

    dataPageqml = QmlDocument::create("asset:///dataPage.qml").parent(qml);
    dataPageqml->setContextProperty("pageC", this);

    m_pageName = "start text";

    // create root object for the UI
    root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene
    app->setScene(root);
    placesLv  = root->findChild<ListView*>("places");
    model = root->findChild<XmlDataModel*>("model");

}

 

void ApplicationUI::processChoice(){
	m_pageName = "New text";
	emit pageNameChanged(m_pageName);
}

 

Page {
    id: page
    onCreationCompleted: {
        pageC.processChoice();
    }
    Container {
        WebView{
            horizontalAlignment: HorizontalAlignment.Fill
            minHeight: 700.0
            maxHeight: 700.0

        }
        Label{
            id: lb
            objectName: "label"
            text: pageC.pageName
        }
    }
}

 

 

This should write "New text" in that label, but i get a blank screen.

Simillar to what I had before.  Something is wrong with that qml connection.

 

Developer
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE
My Carrier: Simobil

Re: QML and C++ integration

Now I've also tried to link it with main.qml and also it doesn't work.
I connect it like this:

qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("mainC", this);

and then call:
onTriggered: {
mainC.processChoice();
}
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: QML and C++ integration

Change 

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

To

QmlDocument::defaultDeclarativeEngine()->rootContext()->setContextProperty("MainC", this);

 

Highlighted
Developer
Posts: 79
Registered: ‎02-04-2013
My Device: Z10 LE
My Carrier: Simobil

Re: QML and C++ integration

Yep, that works for root.

Another thing that bothers me is ListView- FindChild.... which does not find it I assume, beacuse when I connect it to a signal app crashes.

ListView *placesLv  = root->findChild<ListView*>("places");
		model = root->findChild<XmlDataModel*>("model");
		bool open = QObject::connect(placesLv, SIGNAL(triggered()), this,
		                                                   SLOT(processDataPage()));
		            Q_ASSERT(open);
		            Q_UNUSED(open);

But I found another way around so I can manage without that for now. (Strange though  I used it like this in several apps).

 

Another thing is that ListView does not colorize to green. Like it wouldn't be recognized, but it is included in file. So I don't get any suggestions, which functions it has.