A proper way to determine Tablet / Mobile in Qt

232 Views Asked by At

To determine device is tablet or mobile, I use criteria: if device's screen diagonal is greater than 7 iches, the device is tablet. To get diagonal, I use:

QScreen *srn = qApp->screens().at(0);
QSizeF physicalSize = srn->physicalSize();

But when I use Nexus 4, which has 4.7 inches, physicalSize returns greater than 7 inches. I don't really know, whether the device implementers did error, or Qt framework. Anyway, another applications obtained from playmarket determines the matter correctly. QUESTON: What is the best way to determine, tablet or mobile the device is?

2

There are 2 best solutions below

0
On

I don't really know what you want to do but if you only want two different layout files for phone and tablet then you can do this: When you work with Fragments you usually have two different layoutfiles. One just contains a Framelayout, which is for the mobile devices and one that hold two Framelayouts for the tablet devies.

A good way to determine if the device is a phone or a tablet, is to create a refs.xml file in your values folder. Just put an item with a name in it and the layout you want to refer to. This file is for the mobile device:

<resources>
<item name="activity_masterdetail" type="layout">@layout/activity_fragment</item>
</resources>

Than you reate another values folder named values-sw600dp. In this folder you crate another refs.xml file. Its IMPORTANT to give the same item name for both refs files. Then you refer on your tablet layout.

<resources>
<item name="activity_masterdetail" type="layout">@layout/activity_twopane</item>
</resources>

Now you can get set the content view with your item name and android will just pick the right one if you have a tablet or a phone.

In your onCreate() method in your activity:

setContentView(R.layout.activity_masterdetail);

Now you must check if your Framecontainer of the tablet view is null:

if (findViewById(R.id.yourFragmentDetailContainerForTablets) == null) {
   doYourCode();
} else {
   doYourCode();
0
On

If you want to calculate display size in inches then you need to do it as below:

QScreen *scr = qApp->screens().at(0);
qDebug() << "My Screen size is : " << scr->physicalDotsPerInch();

Please find more info here