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
5alid
Posts: 17
Registered: ‎12-30-2013
My Device: Q5
My Carrier: WIFI

create listview with object in C++

Hi all,

 

I need create listview with object in C++

 

My code:

 

void ApplicationUI::startNext()
{
    // If empty just add a new status message
    if (m_brQueue.isEmpty()) {
        addStatusMessage(QString("is empty"));
        return;
    }

       ..............
}

void ApplicationUI::addStatusMessage(const QString &message)
{
    m_statusMessage.append(message);
}

QString ApplicationUI::statusMessage() const
{
    return m_statusMessage.join("\n");
}

 

// I have add

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

// I have added the button to check and show in TextArea that good but I need show in listview

TextArea {  
          text: _app.statusMessage
          }

This is my list:

ListView {
                    id: check
                    objectName: "_app"
                    listItemComponents: [
                        ListItemComponent {
                            StandardListItem {
                                preferredHeight: 170
                                title: _app.statusMessage
                            }
                        }
                    ]
                }

 

When use TextArea is good and run but when use listview not work.

 

I think not can use like this:

 

title: _app.statusMessage

 

 

What's wrong with that?

 

 

 

Thanks

 

Please use plain text.
Developer
nik005
Posts: 34
Registered: ‎05-30-2014
My Device: BlackBerry Z10
My Carrier: Company

Re: create listview with object in C++

ListView {
                    id: check
                    objectName: "_app"
                    listItemComponents: [
                        ListItemComponent {
                            Label{
                               text: _app.statusMessage
                           }
                        }
                    ]
                }

 Try this...

Please use plain text.
Developer
simon_hain
Posts: 16,017
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: create listview with object in C++

maybe this helps:
http://supportforums.blackberry.com/t5/Native-Development-Knowledge/How-to-access-qml-properties-fro...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Developer
5alid
Posts: 17
Registered: ‎12-30-2013
My Device: Q5
My Carrier: WIFI

Re: create listview with object in C++

@nik005

 

Nothing has changed. ):

 

@simon_hain

 

You can not do that because it uses datamodel.

Also tried without datamodel nothing has changed.

 

 

Thank you for your comments.
Waiting for other solutions.

Please use plain text.
Developer
MagnumOpus
Posts: 127
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: create listview with object in C++

Simon is right, If  you want to access a object inside a ListItemComponent you have to expose that object to the ROOT context not the context of the QmlDocument. 

 

 

Regards.

If my answer has been useful to you, please, click the button
Please use plain text.
New Developer
5alid
Posts: 17
Registered: ‎12-30-2013
My Device: Q5
My Carrier: WIFI

Re: create listview with object in C++

@MagnumOpus

 

I have tried other solutions but did not work like:

 

onCreationCompleted: {
        Qt.app = _app;
    }

ListView {
                    id: check
                    objectName: "_app"
                    listItemComponents: [
                        ListItemComponent {
                            StandardListItem {
                                preferredHeight: 170
                                title: Qt.app.statusMessage
                            }
                        }
                    ]
                }

 


 

For solution Simon I have tried but not work me.

When use C++:

 

// add to root context in order to be available to ListItems
QDeclarativeEngine *engine = QmlDocument::defaultDeclarativeEngine();
QDeclarativeContext *rootContext = engine->rootContext();
rootContext->setContextProperty("_app", this);

 

Do not work what's wrong with that?

 


 

And how to use this:

ListItemComponent{
 id: myComponent
}

myComponent.ListItem.view //the ListView
myComponent.ListItem.view.dataModel //the dataModel property of the ListView
myComponent.ListItem.view.myFunction() //a function in the ListView

 

In my listview?

 

Sorry I do not know a lot in QML.

 

Please use plain text.
Developer
MagnumOpus
Posts: 127
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: create listview with object in C++

[ Edited ]

You needn't  use the objectName property unless you're planning to access the QML object through C++ code.
Simon's solution has to work, try removing the objectName property. As regards ListItem property, this property is created dinamically by ListItemView on each visual root node (i.e on each ListItemComponent's root content ), so If you want to access that property from an inner element you have to set an id for the visual root node and access the ListItem property through it, for example:

 

ListView {
  dataModel: myModel
  listItemComponent: [
    ListItemComponent {
      type: ""
      Container {
	id: visualRootItem
	Container {
	  Label {
	    //in the case of ListItem.data you have the alias ListItemData
           // that can be used on the entire visual node, so you don't need
	    // prefix ListItemData with the id of the visual root node
	    text: visualRootItem.ListItem.data.name
          }
         }
       }
     }
    ]
}

 

 

Regards.

If my answer has been useful to you, please, click the button
Please use plain text.
New Developer
5alid
Posts: 17
Registered: ‎12-30-2013
My Device: Q5
My Carrier: WIFI

Re: create listview with object in C++

@MagnumOpus Thanks.

 

Really sorry to the many questions.
Because I do not know a lot in QML.

I don't use the dataModel.

Let's put a simple example of what I want:

 

// In QML use button for check text in textarea or another thing...
// * note: I know can check text in QML but this sample * //

Button {
     onClicked: {
           _app.check();
                }
       }

Thats good when use textarea

TextArea {
      text: _app.statusMessage
         }

and when clicked check show text again.


But I need show this in listview and when click check again add item with message from C++.

I have tried use objectName with listview but not work.

 

Please use plain text.
Developer
MagnumOpus
Posts: 127
Registered: ‎01-03-2013
My Device: n/a
My Carrier: n/a

Re: create listview with object in C++

[ Edited ]

If you don't have a dataModel property on the QML ListView code, I guess you're setting the dataModel property through C++, aren't you?

Have you marked the check method as Q_INVOKABLE and the properties with Q_PROPERTY?

You have to do what Simon said if you want to be able to use the _app property inside the ListItemComponent.

 

ListView {
  dataModel: myModel
  listItemComponent: [
    ListItemComponent {
      type: ""
      Container {
	id: visualRootItem
	Container {
	  Label {
	    text: _app.statusMessage
          }
         }
       }
     }
    ]
    onTriggered: {
      //here you should retrieve the data through its index if you're planning to do
      // something with it.
      _app.check();
    }
}

 

Regards.

If my answer has been useful to you, please, click the button
Please use plain text.
New Developer
5alid
Posts: 17
Registered: ‎12-30-2013
My Device: Q5
My Carrier: WIFI

Re: create listview with object in C++

 

@MagnumOpus

 

Do I create a ListView with datamodel in C++?

 

Yes, I did

 

I think in your example did not understand what I want.

 

Let's say I have a button, when clicked, a message appears and when you click again show the same message, but this is good work in the text area.
What I want is that this message appears in the listview and when you click again add another item and show the same message.

Please use plain text.