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
Regular Visitor
Posts: 1
Registered: ‎02-20-2013
My Device: Z10
My Carrier: T-Mobile

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

Not sure if these property were q-invokable when you wrote this answer, but a quicker (and better way) would be to expose this class to qml instead.

 

C++:

#include<bb/device/DisplayInfo>

qmlRegisterType<bb::device::DisplayInfo>("bb.displayInfo", 1, 0, "DisplayInfo");

 

QML:

import bb.displayInfo 1.0

// then use it like that  -->   display.pixelSize.width

 

.PRO:

LIBS   += -lbbdevice

 

Regards,
Jean-François Desgagné | Software Engineer @ BlackBerry
New Contributor
Posts: 4
Registered: ‎06-09-2010
My Device: 9700
My Carrier: Rogers

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

[ Edited ]

Can someone please confirm that this example as is (by jfdesgagneworks.

I tried it and it fails

 

Is it possible that the qml syntax may not be correct.

I believe we have to set some context variable into the qml

New Contributor
Posts: 9
Registered: ‎09-26-2012
My Device: Dev Alpha
My Carrier: Verizon

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

[ Edited ]

Hi rttom,

 

I can confirm this does work.  However there is a missing piece - you need to create an instance of the class being exported to QML.   For instance, in a given QML page where you are using this:

 

Page {

    attachedObjects: [
        DisplayInfo {
            id: displayInfo
        }
    ]

}

 Then you can access display size in pixels in your QML code by displayInfo.pixelSize.width

Regular Visitor
Posts: 1
Registered: ‎04-02-2013
My Device: Blackberry z10
My Carrier: Wind

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

This works like a charm. Thank you!

Contributor
Posts: 13
Registered: ‎03-30-2013
My Device: BB10 Dev Alpha A
My Carrier: China Mobile

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

New Contributor
Posts: 4
Registered: ‎06-09-2010
My Device: 9700
My Carrier: Rogers

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

I saw this code. I was debating on whether or not to use this approach on every qml page. I was expecting some built- in API to supply this value. Like someone said, in the absense of some built-in api, you will see lots of code that hard codes the width and height to some numberic value scattered generously throughout the code.

 

Setting a context value from C++ as someone suggested seemed so hackey especially in larger projects witl many QML assets.

 

In the end, we went with the reference to the DisplayInfo class. That still required more lines of code than I hoped. If it is only 6 lines in one file, that's ok, but we will have to duplicate 6 lines in 50+ files.

 

I know we could probably introduce some containment pattern to expose these values to contained objects, but we don't have time to refactor the code.

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

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

why so complicated ?

 

I always expose my application to QML

you do this one time at startup

 

then in my application c++ I have 3 useful methods like this

 

int displayWidth()

int displayHeigh()

bool isSquare

 

and now in all your 50+ QML you simply say

 

app.displayWidth() or app.displayHeight() and you're done

 

in some cases I also want to know if it's a keyboard device, where I don't have to support portrait/landscape, but have to use some layouting similar to landscape. so it's convinient and easy readable code to say

if(app.isSquare() ....

 

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
New Member
Posts: 1
Registered: ‎04-28-2013
My Device: Z10 STL100-3
My Carrier: Wind

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

 

Just leaving additional information I found useful after viewing this thread:

 

If you're trying to get layout information on the size of things that are being displayed -> height and width (object size), check out the LayoutUpdateHandler:

 

https://developer.blackberry.com/cascades/reference/bb__cascades__layoutupdatehandler.html

 

You should also read the page on visual nodes:

 

http://developer.blackberry.com/cascades/reference/bb__cascades__visualnode.html

 

There's also the Image Tracker which wasn't helpful in my situation:

 

http://developer.blackberry.com/cascades/reference/bb__cascades__imagetracker.html