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: 139
Registered: ‎05-24-2011
My Device: Z30 running 10.2 and Playbook 4GLTE running OS2.1
Accepted Solution

listItemComponent , GroupDataModel and a C++ object

i have a class that returns information about the a set of items.

 
it has 3 Q_PROPERTY's that return a GroupDataModel of QObject* pointers.
 
the QObject returned in the GroupDataModel is simple.  Constructor takes a QString and a bool, properties return them.  For example:
 
foo bar = new foo("item1", true);

QString myText = bar.text;  //myText = "item1"
bool myBool = bar.active;    // myBool = true


 

 
The Q_PROPERTY's are attached to ListView's in QML.
 
the listItemComponent looks like this:
 
listItemComponents: [
                              ListItemComponent {
                                  type: "item"
                                  Container {
                                      layout: StackLayout { orientation : LayoutOrientation.LeftToRight }
                                      ImageView {
                                       preferredWidth: 40
                                       scalingMethod: ScalingMethod.AspectFit
                                       verticalAlignment: VerticalAlignment.Bottom
                                          imageSource: {
                                              if (ListItemData.active) { imageSource = "asset:///images/on.png" ; }
                                              else { imageSource = "asset:///images/off.png" ; }
                                          }
                                      }
                                      Label {
                                          text: ListItemData.text
                                      }
                                      Label {
                                           text: "(" + ListItemData.active + ")"   
                                      }
                                  }
                              }
                          ]

 

 
what's weird is that the Label that displays ListItemData.active (a bool) is always correct.  if the item in question is active (active=true), then the Label displays "true".  likewise if it's disabled (active = false, and the Label displays "false").
 
However the image source is all messed up.  It seems like my if/else - which will display one imageSource if ListItemData.active = true, and a different image if ListItemData.active = false) isn't working, but i can't figure out why.
 
for instance, i might see something like this:
 
-when the app first loads, the display is correct.  the default is to display everything with .active=true and show the "on.png" in the ListView.
-when i unfilter the list to show all items (whether active=true or active=false), then 
--the ImageView might show the on.png, or might show the off.png regardless of the value of ListItemData.active.
--the Label always shows the correct value.
 
Why doesn't my if/else statement work correctly, and why does the ImageView not seem to care above the value of ListItemData.active?
 
Thanks!
 
oddboy
 
bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Developer
Posts: 229
Registered: ‎01-12-2013
My Device: z30

Re: listItemComponent , GroupDataModel and a C++ object

You have imageSource 2 times. One outside the if, one inside the if.

 

imageSource: {
                                              if (ListItemData.active) { imageSource = "asset:///images/on.png" ; }
                                              else { imageSource = "asset:///images/off.png" ; }
                                          }

Try the next line

 

imageSource: "asset:///images/" + (ListItemData.active?"on":"off") + ".png"

 It's a bit shorter and it works for me.

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 275
Registered: ‎02-05-2010
My Device: Blackberry Z10

Re: listItemComponent , GroupDataModel and a C++ object

I think you can't use if else statement. Try the code below instead :

imageSource: ListItemData.active ? "asset:///images/on.png" : "asset:///images/off.png"

Twitter : @LaurentKP
My apps
Developer
Posts: 229
Registered: ‎01-12-2013
My Device: z30

Re: listItemComponent , GroupDataModel and a C++ object

I allready replied to that with the same solution :smileywink:.

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 275
Registered: ‎02-05-2010
My Device: Blackberry Z10

Re: listItemComponent , GroupDataModel and a C++ object

Oops sorry you're right !
Twitter : @LaurentKP
My apps