Detecting device type (phone/tablet/other) using Qt

880 views
Skip to first unread message

Matti Dahlbom

unread,
Dec 1, 2012, 4:18:04 AM12/1/12
to andro...@googlegroups.com

Hi, say I wanted to detect whether my Qt-Necessitas app is running on a
mobile phone or a tablet (or other kind of a device), would there be some
nice trick around for doing this?

I know Android APIs do not provide a straightforward way of querying this
(as device profiles arent separated) but if someone has written a
nice piece of code for detecting them, I'd be interested to know :)

- Matti


noname

unread,
Dec 1, 2012, 4:26:45 AM12/1/12
to andro...@googlegroups.com
the only way i know of is detecting the screen size. in case you need
that information to adjust your layouts, that should be sufficient. we
have three classes of displays (different size ranges) and for each one
we load different layouts (ui files).

kind regards

Matti Dahlbom

unread,
Dec 1, 2012, 4:33:03 AM12/1/12
to andro...@googlegroups.com


On Sat, 1 Dec 2012, noname wrote:

> the only way i know of is detecting the screen size. in case you need that
> information to adjust your layouts, that should be sufficient. we have three
> classes of displays (different size ranges) and for each one we load
> different layouts (ui files).

Aye, for Android APIs I know approaches such as this:

http://stackoverflow.com/questions/5832368/tablet-or-phone-android/9590506#9590506

but the real question here is, can I get the same result
(somewhat reliably) using Qt so I dont have to write Java and use JNI ..

- M

noname

unread,
Dec 1, 2012, 4:44:14 AM12/1/12
to andro...@googlegroups.com
well i don't think the "large" and "xlarge" definitions are available in
Qt, so you might need to redefine them yourself. but that shouldn't be
too hard. you could say "<6 inch: phone", ">6 inch: tablet" or something.
other than that, the code in your link seems to do just what i said:
check the screen size. or am i wrong about that?
to do so in Qt, what we do is the following:

QRect screenGeometry = QApplication::desktop()->availableGeometry();
int dpiY = app->desktop()->physicalDpiY();
int dpiX = app->desktop()->physicalDpiX();
double displayWidthInch = screenGeometry.width() / dpiX;
double displayHeightInch = screenGeometry.height() / dpiY;
double displayDiagonalInch = sqrt(displayWidthInch*displayWidthInch +
displayHeightInch*displayHeightInch); // screen diagonal size in inches

now you have everything you need to determine the size (and also
orientation) of the screen.
to dynamically handle orientation changes, you can use the
QApplication::desktop().workAreaResized(int) signal.

hope that's what you asked for. sorry if not.


king regards

Matti Dahlbom

unread,
Dec 1, 2012, 5:00:57 AM12/1/12
to andro...@googlegroups.com


On Sat, 1 Dec 2012, noname wrote:

> hope that's what you asked for. sorry if not.

No, actually this should work sweetly. Thank you. Calculating the physical
screen size via DPI is a nice solution.

While knowing the type of the device (instead of the device
features/capabilities) isnt that important normally, my app attempts to
dig up as much hardware info about the device as possible and also to
categorize the devices, so I need some kind of an approximate solution
such as this. In the future the boundaries between tablets and phones will
blur more anyway and this will be even more foobar.

Thanks, have a nice weekend ;)

- Matti

noname

unread,
Dec 1, 2012, 5:11:19 AM12/1/12
to andro...@googlegroups.com
glad to help.
one more note though, if you want the size of the whole display, you
should probably use screenGeometry() instead of availableGeometry()
(which should exclude the "task bar" at the top, i think), see the docs
for more info.

kind regards
Reply all
Reply to author
Forward
0 new messages