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
dkonigs
Posts: 241
Registered: ‎07-25-2008
My Device: Bold 9900

Correct way to get display width/height in QML?

I'm seeing an embarassingly large number of QML samples that all hard-code "768" and/or "1280" for container sizing, to make things correctly fill the screen.  As we all know, this is a VERY BAD practice to actually follow in UI markup.  Were this BB Java, I'd be using "Display.getWidth()" and "Display.getHeight()" in the code for most of those cases.

 

So what's the best way to specify, in a QML file (that hopefully won't break the QML preview window), the width or height of the screen?

 

(Or do we have to hard code it within some properties, that we reset from the C++ that loads the QML?)

Please use plain text.
Developer
dtater
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Correct way to get display width/height in QML?

This is actually a very good question.  I have been trying to keep my layouts as dynamic as possible, but some things must be hard coded :smileysad:

 

I think that the initial BB hardware will be the same resolution as the dev alpha, so you'll be OK for launch, but soon thereafter the hardware will start to heature different screen resolutions.

 

Im posting so i can subscribe to the thread :smileyhappy:

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

Re: Correct way to get display width/height in QML?

dtater (and others), for what it's worth, you don't need to comment on a thread to subscribe to it. Just find the "Topic Options" dropdown next to the Reply and Tweet Post buttons above, and there you'll see a Subscribe item in the dropdown menu.

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.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Correct way to get display width/height in QML?

I assume you've seen http://supportforums.blackberry.com/t5/Cascades-Development/Display-Width-and-Height/m-p/1806377/hig... as you allude to that.

 

Did you find a way to do this directly from QML, or an easy way to streamline it?

If so, please reply here so all can see.

 

If not, perhaps you want to create a feature request.  Follow instructions in http://supportforums.blackberry.com/t5/Java-Development/Developer-Issue-Tracker/td-p/271768

 

Stuart

Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: Correct way to get display width/height in QML?

Please vote for https://www.blackberry.com/jira/browse/BBTEN-172

 

From C++ you can now more easily find the device size:

 

#include <bb/device/Display>
...

            bb::device::smileyvery-happy:isplay display;
            qDebug() << "Display size: " << display.pixelSize();

 

Stuart

Please use plain text.
Developer
dtater
Posts: 158
Registered: ‎01-11-2010
My Device: 9700 | 9900 | PlayBook | Dev Alpha
My Carrier: Many :)

Re: Correct way to get display width/height in QML?

Voted!

- - - - - - - - - -
Please use plain text.
Developer
dkonigs
Posts: 241
Registered: ‎07-25-2008
My Device: Bold 9900

Re: Correct way to get display width/height in QML?

One concern that may not be completely obvious at first glance is that BB10 supports multiple displays for the device, so a solution to this would need to somehow be specific to "the display that the current page is being shown on".  But really, not just that, also the displayable area on that display (which can be affected by the presence of a tab/navigation/action bar and the visibility of the virtual keyboard).

Please use plain text.
New Contributor
TheRain
Posts: 9
Registered: ‎09-26-2012
My Device: Dev Alpha
My Carrier: Verizon

Re: Correct way to get display width/height in QML?

Hi guys, appologies if this has been answered elsewhere, or if there is another better solution for this.  The solution I'm using is to get DisplayInfo via C++, grab the screen dimensions from that, and pass them in to my QML document.   Here's how to do that:

 

C++ Code:

 

#include <bb/device/DisplayInfo>
using namespace bb::device;

 

QmlDocument *qml = QmlDocument::create("asset:///main.qml");
qml->setParent(this);
	
DisplayInfo display;
int width = display.pixelSize().width();
int height = display.pixelSize().height();

QDeclarativePropertyMap* displayProperties = new QDeclarativePropertyMap;
displayProperties->insert("width", QVariant(width));
displayProperties->insert("height", QVariant(height));
	
qml->setContextProperty("DisplayInfo", displayProperties);

 Now a DisplayInfo object is accessible from QML like so:

 

ImageView{
    imageSource: "asset:///images/my_awesome_full_screen_image.png"
    preferredHeight: DisplayInfo.height
    preferredWidth: DisplayInfo.width
    scalingMethod: ScalingMethod.AspectFill
}

 

Please use plain text.
New Contributor
chrisjBitheads
Posts: 7
Registered: ‎11-22-2010
My Device: Not Specified

Re: Correct way to get display width/height in QML?

I am pretty sure at BBJAM last week that they said launch devices will go out with a width of 720 px, not 768 px... So the alpha devices are not a perfect match... Here's a post on the blog that links to another site that discusses this:

http://devblog.blackberry.com/2012/09/the-story-behind-blackberry-10-screen-resolutions/

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

Re: Correct way to get display width/height in QML?

chrisjBitheads, that's not quite correct, based on everything we've heard except maybe whoever said what you heard at BB Jam Americas.

The very first devices launched will match the Dev Alphas, so they will be 1280 x 768. All subsequent devices will be only 720, either 1280x720 or 720x720 (at least for the foreseeable future).

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.