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
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various
Accepted Solution

How to get a size of bb:cascades:Control object?

It is sometimes required to retrieve the size of a control (a button, a label ... etc). 

 

I have struggled find a property or method that would allow me to read the current control size.

 

Is it possible at all?

 

Thanks.

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to get a size of bb:cascades:Control object?

Please read about the LayoutUpdateHandler which can do this for you: https://developer.blackberry.com/cascades/reference/bb__cascades__layoutupdatehandler.html

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: How to get a size of bb:cascades:Control object?

Thanks, but this is a handler for changes in the layout (position, size ...). If the size and position do not change  - how does one get that information?

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to get a size of bb:cascades:Control object?

The size and position change on startup, as the UI is laid out by the Cascades rendering engine. That means you can't get that information except by listening for these changes... unless you are in direct control of the size, in which case you'd already know it.

For example, if you use minHeight/preferredHeight/maxHeight to force something to be 100 pixels tall, you'd already have that information and/or could read it off one of those properties.

If you don't force the size to something, you need to wait for the size to be assigned, and then listen for the event with that handler.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: How to get a size of bb:cascades:Control object?

That is understood, but the problem arises with a situation like this

 

bb::cascades::ImageView* anImageView = new bb::cascades::ImageView();

anImageView->setImageSource(QUrl("asset///someimage.png"));

 

Loading an image like this will set the ImageView size to the size of the image by default. At this point I would like to know the size of "anImage" control. I understand that I could have connected the layout events to the object before I assigned the image source, but then I have to manage and keep that information somewhere. What if I have 10, 20, 100 of these little images. I would have to create a wrapper object or a new one inheriting from the ImageView just so I can connect the events and store the information somewhere so I can use it on demand. 

 

That information must be available somewhere. I can't believe that there is no access to it. If there isn't - it's really a missing piece.

 

QImage has information about the image size. Casscade's Image (accessible through the ImageView as well) doesn't. 

 

 

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: How to get a size of bb:cascades:Control object?

How general is your goal here? The title and earlier posts said "Control" but you're talking specifically about images now. For the general case, I don't believe you have any alternative because of the architecture of Cascades.

Even with images, what about 9-slice images, or tiled ones? Assuming you can eliminate all those things (other Controls, and non-simple images) then I don't know what your other options are if the basic class involved doesn't report sizes. I take it you've already considered ImageData (https://developer.blackberry.com/cascades/reference/bb__imagedata.html) and found it wanting too?

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: How to get a size of bb:cascades:Control object?

Yes - I am aware of the ImageData - thanks. The problem is when one wants to use the AbsolluteLayout and layout other controls based on the sizes and positions of other controls.

I used the ImageView control because that was the easiest to demonstrate the issue with.

 

translationX() property is also being changed by the layout of the controls, yet it is accessible on demand. I do not understand why there is no property called "currentSize" for example. 

 

We'll find some workaround, but this is definitely a shortcoming - particularly when one uses the AbsoluteLayout.

 

 

Please use plain text.
Developer
djogon
Posts: 93
Registered: ‎10-29-2012
My Device: Z10
My Carrier: Various

Re: How to get a size of bb:cascades:Control object?

Since we need for a particular subset of custom made controls, we decided to use the LayoutUpdateHandler after all.

If anyone has needs for similar functionality - here how we did it.

 

Our control(s) have a private QRectF currentBounds; member.

 

In the control's constructor we added the following

 

LayoutUpdateHandler::create(this).onLayoutFrameChanged(this, SLOT(onLayoutFrameChanged(QRectF)));

 

 

Then implemented a private slot onLayoutFrameChanged(QRectF& bounds) and update the private currentBounds member.

 

It's not pretty and, most importantly, should not be really needed in our opinion, but it works and we can always access the "currentBounds" property to get the actual bounds.

 

Please use plain text.