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
BlackBerry Development Advisor (Retired)
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

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

I'm not sure why your use of "asset:///" is not being translated to a file system path. The following alternative code works however:

 

QString workingDir = QDir::currentPath();
QDeclarativeComponent component(engine, QUrl("file://" + workingDir + "/app/native/assets/SelectARoom.qml"));

 

Considering your ListView options now.

--------------------------------------------------------------------------------------------
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.
BlackBerry Development Advisor (Retired)
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

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

Hi

 

Working from your example list:

                                                Rule Name

                                                          Room Name

                                                          Room Name

                                                          Room Name

                                                          Room Name

 

                                               Rule Name

                                                         Room Name

                                                         Room Name

                                                         Room Name

                                                         Room Name

 

I think the essence of the problem is that you need to flatten your hierarchy so that it can be used with GroupDataModel.

 

I managed to produce this from your JSON data (slightly tweaked the values but otherwise the same):

 

IMG_00000050.png

 

It looks like your example so I hope this is what you were looking for.

 

I produced it with the following code:

 

QML:

 

import bb.cascades 1.2
NavigationPane {
    id: nav
    Page {
        id: mainPage
        objectName: "mainPage"
        ListView {
            id: lv_rooms
            objectName: "lv_rooms"
            listItemComponents: [
                ListItemComponent {
                    type: "header"

                    Header {
                        title: ListItemData
                    }
                },

                ListItemComponent {
                    type: "item"
                    StandardListItem {
                        title: ListItemData.name
                    }
                }
            ]
        }
    }
}

 C++

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

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // fancy list code

    ListView *listView = root->findChild<ListView*>((const QString) "lv_rooms");

    if (listView) {
//        qDebug() << "XXXX found lv_rooms";
        GroupDataModel *model = new GroupDataModel(QStringList() << "ruleName" << "name");
        model->setGrouping(ItemGrouping::ByFullValue);
        JsonDataAccess jda;
        QVariant va_list = jda.load(QDir::currentPath() + "/app/native/assets/rooms.json");
        qDebug() << "XXXX JsonDataAccess error=" << jda.error().errorMessage();

        QVariantList rules_and_rooms_flattened_list;

        QVariantList rules = va_list.toMap()["rules"].toList();

        qDebug() << "XXXX rules list contains " << rules.size();

        for (QList<QVariant>::iterator it = rules.begin(); it != rules.end(); it++) {
            //Key-value pairs from list item put into a map
            QVariantMap rule_map = it->toMap();
            QVariant ruleName = rule_map["ruleName"];

            QVariantList rooms = rule_map["rooms"].toList();

            // iterate through the rooms attached to this rule
            for (QList<QVariant>::iterator it = rooms.begin(); it != rooms.end(); it++) {
                QVariantMap room_map = it->toMap();
                room_map["ruleName"] = ruleName;
                QVariant ruleName2 = room_map["ruleName"];
                QVariant roomName = room_map["name"];
                qDebug() << "XXXX room_map item: rule=" << ruleName2 << ",room=" << roomName;
                rules_and_rooms_flattened_list.append(QVariant(room_map));
            }

        }

        model->insertList(rules_and_rooms_flattened_list);
        qDebug() << "XXXX model contains " << model->size() << " items";
        listView->setDataModel(model);

    } else {
        qDebug() << "XXXX not found lv_rooms";
    }

    // Set created root object as the application scene
    app->setScene(root);

 

Does this help?

 

Regards

 

Martin

 

 

 

--------------------------------------------------------------------------------------------
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
My Carrier: vodafone

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

[ Edited ]

Thank you. Now the qml file is being found. I also implemented the GroupDataModel code both in qml and C++, albiet with  slight difference. I first tried your code like this:

 

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

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    // fancy list code

    ListView *listView = root->findChild<ListView*>((const QString) "lv_rooms");
   
    //The rest of the code for populating the GroupDataModel and setting the listView dataModel

    Application::instance()->setScene(root);
}

 When I run this code, the app crashes after coming onto the SelectARoom page.

 

Then I changed the C++ code to the following: 

 

QString workingDir = QDir::currentPath();
	QDeclarativeEngine *engine = new QDeclarativeEngine();
	QDeclarativeComponent component(engine, QUrl::fromLocalFile(workingDir + "/app/native/assets/SelectARoom.qml"));

	QObject *object = component.create();
   
        ListView *listView = object->findChild<bb::cascades::ListView*>((const QString) "lv_rooms");

//Rest of the code to populate the GroupDataModel and then set the listView dataModel

 When I run this code the app doesn't crash but nothing is displayed on the screen. I added the signal onDataModelChandged to the ListView and it shows that the data model has changed. I also added DataModelChangeType.Init to the onDataModelChanged signal (I came across it online) but to no effect. 

 

How do I overcome this problem? You have been extremely paitent with me and I thank you for this. I just need a little bit more of your help.

 

P.S. I'm using 10.0 NDK.

Please use plain text.
BlackBerry Development Advisor (Retired)
mwoolley
Posts: 571
Registered: ‎06-25-2010
My Device: Z10
My Carrier: Vodafone

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

I think we're making progress here. No patience required by the way... glad to help as best I can.

 

I think you need to revert to my code and then we should figure out why it doesn't work for you. Could you please provide details of any messages in the console or from slog2info when it crashes?

 

You're using a very old SDK though. Is it possible for you to upgrade to 10.2?

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