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 Contributor
HumptyDumptyEIZ
Posts: 9
Registered: ‎11-28-2013
My Device: Z10
Accepted Solution

File not found error when trying to make a QDeclarativeComponent of a QML in C++

I am trying to insert controls in a qml document from C++ . 

 

Following is my C++ code: 

 

//SelectRoomView.hpp

#ifndef SELECTROOMVIEW_HPP_
#define SELECTROOMVIEW_HPP_

#include <QtCore>
#include <QtCore/QObject>
#include<QDeclarativeEngine>
#include<QDeclarativeComponent>
#include <bb/cascades/Container>
#include <bb/cascades/Label>
#include <bb/cascades/GestureHandler>
#include <bb/cascades/TapHandler>
#include <bb/cascades/ScrollView>

#include "ServicedView.hpp"

using namespace bb::cascades;

class SelectRoomView : public ServicedView {

Q_OBJECT

Q_PROPERTY(QString propId READ propId WRITE setPropId NOTIFY propIdChanged)
public:
	SelectRoomView(QObject* parent = 0);
	virtual ~SelectRoomView();

	void setPropId(QString propId);
	QString propId();
	

	Q_SIGNALS:
		void propIdChanged();
		

protected:
		virtual void populateView(QVariantMap resultSet);
		virtual void populateViewAmenities(QVariantMap resultSet);
		QString _propId;
		
};

#endif /* SELECTROOMVIEW_HPP_ */







//SelectRoomView.cpp

#include "SelectRoomView.hpp"

#include <QDebug>

using namespace bb::cascades;

SelectRoomView::SelectRoomView(QObject *parent) : ServicedView(parent) {
}

SelectRoomView::~SelectRoomView() {
	// TODO Auto-generated destructor stub
}

QString SelectRoomView::propId() {
	return _propId;
}

void SelectRoomView::setPropId(QString propId) {
	_propId = propId;
	emit propIdChanged();
	ServicedView::paramConnection("property/raterule", "get", _propId);
}

void SelectRoomView::populateView(QVariantMap resultSet) {

	QDeclarativeEngine *engine = new QDeclarativeEngine();
	QDeclarativeComponent component(engine, QUrl::fromLocalFile("asset:///SelectARoom.qml"));

	QObject *object = component.create();
	qDebug() << "Component error: " << component.errors();


	ScrollView *sView = object->findChild<bb::cascades::ScrollView*>("scrollView");
	Container *listContainer = new Container();

	sView->setContent(listContainer);
	qDebug() << "sView: " <<sView;

	QVariant result, roomEntry;
	foreach(result, resultSet.value("rules").toList()) {
		QVariantMap rule, room;

		rule["ruleName"] = result.toMap().value("ruleName");

		Container *ruleContainer = new Container();
		Container *roomContainer = new Container();
		Label *ruleLabel = new Label();
		Label *roomName = new Label();

		ruleLabel->setText(rule["ruleName"].toString());

		ruleContainer->add(ruleLabel);



		foreach(roomEntry, result.toMap().value("rooms").toList()) {
			room["name"] = roomEntry.toMap().value("name");
			
			roomName->setText(room["name"].toString());

			roomContainer->add(roomName);
			ruleContainer->add(roomContainer);
		}


		listContainer->add(ruleContainer);
		

	}

}

void SelectRoomView::populateViewAmenities(QVariantMap resultSet) {}

 

 

And here is my QML code:

 

//SelectARoom.qml

import bb.cascades 1.0
import Data.SelectRoomView 1.0
import "commons"


	Page {
    	    id: pageSelectARoom
    	    property string propId     //This property is brought forward from the previous qml file

            attachedObjects: [
    	        SelectRoomView {
            	    id: selRoom 
                }
    	    ]

            onPropIdChanged: {
           	selRoom.propId = propId;
            }

            Container {
	        layout: StackLayout {
	            orientation: LayoutOrientation.TopToBottom
	        }
	       
                background: Color.create("#FFFFFF")

                 ScrollView {   
                     horizontalAlignment: HorizontalAlignment.Fill
                     objectName: "scrollView"       
                 }
           }
       } 

 

When I run the app, SelectARoom.qml loads from the previous qml for a brief moment and then the application crashes and I get the following message in the console:

           QDeclarativeComponent: Component is not ready

           Component error:  (file:///accounts/1000/appdata/com.example.MyApp.testDev_Appcd302e70/asset://SelectARoom.qml: File not found)    

. I think, being a begginer in native development, I am doing some thing fundamentally wrong. Please help. Thanks.

Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

I'm not sure what the cause of your error is but before I investigate further, I wondered if you could clarify why you have chosen this approach? From what I see, you are dynamically building a page containing a series of Label components.

 

Have you considered using ListView instead?

 

https://developer.blackberry.com/native/documentation/cascades/ui/lists/list_view.html

 

Then your "previous" QML file would pass a key value to C++ where you would populate your ListView from an underlying DataModel and get a list of room data in your QML page. There are lots of examples of how to use ListView in our Github repos.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
New Contributor
HumptyDumptyEIZ
Posts: 9
Registered: ‎11-28-2013
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

[ Edited ]

The code submitted by me is exetremely simplified. I am not able to use ListView because my data model is three dimensional in nature; I'm parsing it from a json. It is of the following form:

 

QMap(("room", QVariant(QVariantMap, QMap(("id", QVariant(QString, "1") ) ( "name" ,  QVariant(QString, "Marble Arch Club Double(Complimentary Breakfast)") ) ( "newPrice" ,  QVariant(QString, "155.11") ) ( "oldPrice" ,  QVariant(QString, "185.22") ) ( "overview" ,  QVariant(QString, "Spoil yourself in one of our spacious Marble Arch Club Rooms at 28sqm,elegant designed") ) ( "thumbnail" ,  QVariant(QString, "http://oi42.tinypic.com/swajir.jpg") ) ( "vat" ,  QVariant(QString, "20") ) )  ) ) ( "ruleId", QVariant(QString, "1") ("ruleName" QVariant(QString, "Rule 1")) )

 If this can be implemented in ListView somehow then please help me out as it will make the task easier for me.

Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

Could you use ListView for 2 dimensions of your data and populate a DropDown with the values from the 3rd dimension? Then when the user selects a value from the drop down, you set the model content to the list associated with the selected value.

 

https://developer.blackberry.com/native/reference/cascades/bb__cascades__dropdown.html?f=drop

 

What do you think?

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
New Contributor
HumptyDumptyEIZ
Posts: 9
Registered: ‎11-28-2013
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

[ Edited ]

I need a view similar to this:

 

                                                Rule Name

                                                          Room Name

                                                          Room Name

                                                          Room Name

                                                          Room Name

 

                                               Rule Name

                                                         Room Name

                                                         Room Name

                                                         Room Name

                                                         Room Name

 

And the json that I'm getting is simillar to the following :

 

{
	"rules": [
		{
			"ruleID": "1",
			"ruleName": "Name 1",
                        "rooms": [
				{
					"id": "1",
					"ruleId": "1",
					"name": "Name 1",
                                },

                                {
					"id": "2",
					"ruleId": "1",
					"name": "Name 2",
                                },

                                {
					"id": "3",
					"ruleId": "1",
					"name": "Name 3",
                                },  
                        ]
                 },
           
                 {
			"ruleID": "2",
			"ruleName": "Name 2",
                        "rooms": [
				{
					"id": "1",
					"ruleId": "2",
					"name": "Name 1",
                                },

                                {
					"id": "2",
					"ruleId": "2",
					"name": "Name 2",
                                },

                                {
					"id": "3",
					"ruleId": "2",
					"name": "Name 3",
                                },  
                        ]
                 }
         ]
}

 

How should I parse this in a data model so that I can use ListView in qml? Thanks.

 

Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

Take a look at GroupDataModel. I think you should be able to store your data in this model and then link the model to ListView. 

 

https://developer.blackberry.com/native/reference/cascades/bb__cascades__groupdatamodel.html

 

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
New Contributor
HumptyDumptyEIZ
Posts: 9
Registered: ‎11-28-2013
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

I tried working with GroupDataModel but to no avail. I tried another approach: Nesting a Listview inside a LIstView. The following is the QML code: 

 

import bb.cascades 1.0
import Data.SelectRoomView 1.0
import "commons"


Page {
    id: pageSelectARoom
    
    property alias currentPage: pageSelectARoom
    property string hName
    property string propId
    property string aDate
    property string dDate
    property int night
    property string room
    property string adult
    property string child
    property int index
    attachedObjects: [
        SelectRoomView {
            id: selRoom 
        }
    ]
    
    onPropIdChanged: {
        selRoom.propId = propId;
    }
    
    Container {
        layout: StackLayout {
            orientation: LayoutOrientation.TopToBottom
        }
        
        background: Color.create("#FFFFFF")
        
        ScrollView{   
            horizontalAlignment: HorizontalAlignment.Fill
            objectName: "scrollView"                   
            
            ListView {
                id: ruleList
                dataModel: selRoom.modelRule
                
                listItemComponents: [	
                    ListItemComponent {
                        id: outerList
                        
                        Container{
                            Label {
                                id: outerLabel
                                text: ListItemData.ruleName
                                textStyle.fontSize: FontSize.XXSmall
                                textStyle.color: Color.Black
                            }
                            Label{
                                id: outerId
                                text: ListItemData.ruleId
                                textStyle.fontSize: FontSize.XXSmall
                                textStyle.color: Color.Black
                                visible: true
                            }
                            
                            ListView{
                                id: roomList
                                dataModel: selRoom.modelRoom
                                listItemComponents: [
                                    
                                    ListItemComponent {
                                        id: innerList
                                        
                                        
                                        Label{
                                            id: innerLabel
                                            text: ListItemData.name
                                            textStyle.fontSize: FontSize.XXSmall
                                            textStyle.color: Color.Black
                                        
                                        }
                                    }
                                ]
                            }
                        }
                    
                    }     
                ]
            
            }
        
        }
    
    }
}

 

Now, "ruleName" of the outer ListView is being displayed but no items of the inner ListView are being displayed. On checking the console, I get the following message: asset:///SelectARoom.qml:100: ReferenceError: Can't find variable: selRoom, i.e., 

 

ListView{
                                id: roomList
                                dataModel: selRoom.modelRoom   //selRoom can'tbe found

 What is wrong with this approach? I can make another object of SelectRoomView but then there will be two asynch calls to the web service which is a waste of resources and time. 

Also, could you figure out why SelectARoom.qml could not be found by the C++ class in my original code? Thanks.

Please use plain text.
New Contributor
HumptyDumptyEIZ
Posts: 9
Registered: ‎11-28-2013
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

So, I found out my mistake in the above qml code and have changed it. Now, the following is my qml code for the nested ListViews: 

 

ListView {
    id: ruleList
    layout: StackListLayout {
        headerMode: ListHeaderMode.None
    }
    
    dataModel: selRoom.modelRule
    
    listItemComponents: [	
        ListItemComponent {
            
                         	
            Container{
                id: root
                layout: DockLayout {
                
                }
                maxHeight: 300.0
                Label {
                    text: ListItemData.ruleName
                    textStyle.fontSize: FontSize.XXSmall
                    textStyle.color: Color.Black
                }
              
                
                ListView{
                    dataModel: root.ListItem.view.dataModel
                    rootIndexPath: root.ListItem.indexPath
                    maxHeight: 200.0
                    onCreationCompleted: {
                        console.debug("innerList rootIndexPath: " + root.ListItem.indexPath); 
                    }
                    listItemComponents: [
                        
                        ListItemComponent {
                            
                            Label{
                                text: ListItemData.name
                                textStyle.fontSize: FontSize.XXSmall
                                textStyle.color: Color.Black
                                maxHeight: 20.0
                                visible: true
                                onCreationCompleted: {
                                    console.debug("successful");                                                        
                                }
                                
                            }                           
                        }
                    ]
                }
            }
        }     
    ]
}

Now, the "ruleId" of the parent ListView is displayed but the list item component of the inner ListView is not. The inner ListView is getting created as I get the desired console output showing the rootIndexPath for the inner ListView. How to solve this problem? Thanks.

Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

I'll check but I don't think you're allowed to nest ListView components like this.

 

What was the problem with GroupDataModel?

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
Retired
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10

Re: File not found error when trying to make a QDeclarativeComponent of a QML in C++

[ Edited ]

I stand corrected. Looks like nesting ListView components is OK but the general consensus seems to be that GroupDataModel would be the first option to look at.

 

Trying to establish why your C++ could not find your QML component now btw.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.