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
unbreakable
Posts: 74
Registered: ‎12-17-2009
My Device: Z30

Noob question regarding following sample apps

Hi,

 

I am trying to learn to create my own app by using the FilteredDataModel, which uses the VegetablesModel as a Custom Model and I am faced with a few questions. I have used mostly http://supportforums.blackberry.com/t5/Native-Development-Knowledge/Using-a-Custom-DataModel-to-Filt... though it differs slightly from the actual sample code I downloaded.

 

1) The sample app creates the list in main.qml, but I do not want to do so as I want to put it in another page customlist.qml (I also would like another list so I do not want it to interfer with other lists). So I am not sure if I should put the following code in the app?

 

The app sample creates an app.cpp that contains:

 

App::App(QObject *parent)
    : QObject(parent)
    , m_model(0)
{
    // Create the source data model
    VegetablesDataModel *vegetablesModel = new VegetablesDataModel(this);

    // Create the filtered data model and pass the source model
    m_model = new FilteredDataModel(vegetablesModel, this);

    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    qml->setContextProperty("_model", m_model);
    qml->setContextProperty("_app", this);

    AbstractPane* root = qml->createRootObject<AbstractPane>();
    Application::instance()->setScene(root);
}
//! [0]

//! [1]
void App::selectionChanged(const QVariantList &indexPath, bool selected)
{
    if (indexPath.size() != 1 || !selected)
        return; // Not interested

    // Selected a header item!
    const int selection = indexPath[0].toInt();

    // Toggle the expanded state of the selected header
    m_model->expandHeader(selection, !m_model->isHeaderExpanded(selection));
}
//! [1]

Re: Source DataModel

 

I do not want to use a custom list model but would rather use a XMLDataModel/SQL. But I am not sure how I can do this? 

 

 Re: [0]

Would it be correct to instead put:

 

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

	QmlDocument *qml = QmlDocument::create("asset://CustomList.qml").parent(this);
	qml->setContextProperty("myList", m_model);

 And should I still put them inside the app.cpp or create a CustomList.app?

 

Re: [1] App:: selectionChanged

I am afraid that this will apply to all 'selectionChanged'. How can I make sure it only applies to that specific Custom list?

 

-------------------------------------------------------------------------------------------

In filtereddatamodel.cpp

 

One of the functions of the filteredmodelclass contains the ::data

QVariant FilteredDataModel::data(const QVariantList& indexPath)
{
    if (indexPath.size() == 1) { // header item
        // Enrich the original data of the source model with additional data about expanded state
        QVariantMap data;
        data["data"] = m_sourceDataModel->data(indexPath);
        data["expanded"] = (indexPath[0] == m_expandedIndex);

        return data;
    } else {
        // Pass through the data from the source model
        return m_sourceDataModel->data(indexPath);
    }
}

 which I understand can then be called from main.qml's Listview as Listdatamodel.data, and Listdatamodel.expanded.

 

The thing is, I have a more complicated datamodel. How can I map the QVariantMap data to specific information on my XML/SQL (have not decided which one to use)?

 

e.g. I want to have: data["id"], data["title"], data["status"] to use in my ListView.

 

-----------------------------------------------------

QString FilteredDataModel::itemType

 

I would like to create 3 levels of list. Can I use:

 

{
switch (indexPath.size()) {
case1:
return QLatin1String("header");
break;
case 2:
return QLatin1String("subheader");
break;
default:
return QLatin1String("item");
break;
}
}

 And use them in my ListView like:

            listItemComponents: [
                ListItemComponent {
                    id: header
                    type: "header"
                    objectName: "header"
                },

                ListItemComponent {
                    id: subcategory
                    type: "subheader"
                    objectName: "subheader"
                },

                ListItemComponent {
                    id: item
                    type: "item"
                    objectName: "item"

                }
            ]

 

 

Thanks and apologies for the long post!!!

Developer
LoganSix
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook

Re: Noob question regarding following sample apps

Are you still going to use the main.qml as the starting page?

 

Then you don't need to do this part, you still can call _app from the other pages the same.

QmlDocument *qml = QmlDocument::create("asset://CustomList.qml").parent(this);
	qml->setContextProperty("myList", m_model);

From the documentation, you can create 2 levels of list, but I don't think you can create 3 levels.  

You don't need to create 2 lists, but just set your datamodel to sort on the first level ("header").

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment