Qt on Android: How to adapt to different screen sizes

Source: Internet
Author: User

How do I adapt to the various screen sizes of Android smartphones using Qt-developed Android apps?

When it comes to screen sizes, from 2.8-inch to 8.9-inch phone screens, it's a little more painful for program apes. The Android project itself has considered this issue, resource files, ldpi/mdpi/hdpi/xhdpi, and so on, the Android framework automatically selects the appropriate icon based on the screen size, so that the application looks the same on different screen sizes.

What about the Qt application? It's not really a mechanism to use Android (only App Icon can hitch a ride), and everything has to be done on its own. How do we deal with that?

The first thing to understand is the DPI, then the font size.

DPI and Font size

DPI, dot per inch, which is the number of dots per inch. Another concept is the PPI, which is the number of pixels per inch. In general, we use DPI is enough, for professionals to handle ultra-high DPI scenarios, the use of PPI may be more accurate. In Qt, only DPI, so let's just go for it.

The larger the value, the larger the pixel density, and the smaller screen size can have a large resolution. For example, some Android phones, 3.7-inch screen can provide 960x540 resolution, while some phones, 5-inch screen provides 800x480 resolution. These two different screen sizes and resolutions of the phone, the 5-inch screen will look grainy, while 3.7 inches looks very delicate. This is the difference in pixel density.

Some screens, the horizontal dpi and the vertical dpi are different, that is, the pixel is not a square, which is more complicated ...

When we write applications, it is desirable to set the size of the interface elements according to the DPI + screen resolution .

QScreen class

In Qt, the QScreen class can get DPI-related information.

QScreen Physicaldotsperinch/physicaldotsperinchx/physicaldotsperinchy This set of properties represents the physical DPI. Logicaldotsperinch/logicaldotsperinchx/logicaldotsperinchy This set of properties represents a logical DPI, and Qt uses it to calculate the font size, which we can use to convert the pointsize of the font to Pixe Lsize. Let's just say it.

Logicaldotsperinch is a simple average of X and Y, and in most cases it is enough, of course, if you have the ultimate pursuit, ask the way in Logicaldotsperinchx and Logicaldotsperinchy.

Qfont class

Qfont represents a font, and the size of the font is expressed in two ways: Pixelsize and pointsize. That is, pixel size and lattice size. If you use the pixel size to represent the font, the font will not be affected by the DPI, and on the computer you can adjust the dpi of the display to see how the interface changes. However, this does not apply to the requirement to fit a variety of screen sizes in a mobile scene. When developing for Android devices, we should use the pointsize of the fonts, which is the default way for Qt apps to be processed.

Nonsense is not, the default is Pointsize, but also a wordy!

No, not too! and look down.

Controls in Qt

There are qlabel/qpushbutton/qlistwidget/qtabelwidget in Qt and so on you can use the controls on Android devices, which can be used to display text. You find a bunch of different size screen phones, use Qfont's Setpointsize () method to adjust the size of the font lattice, weigh the effect, you can determine how the font size of your app is set.

But there are non-text scenes, such as you are the picture button, what to do?

My answer is: Calculate the pixel size based on the bitmap size of the font, and then take this to adjust the size of the non-text control. Such non-textual elements as text elements and pictures on the interface can be matched.

Calculation formula from pointsize to pixelsize: pixelsize = DPI * pointsize/72.

How Qt Apps use DPI

Look at a code snippet:

int main (int argc, char **argv) {    qapplication A (argc, argv);    ...    QScreen *screen = A.primaryscreen ();    Qfont f = A.font ();    int pixelsize = (f.pointsize () * Screen->logicaldotsperinch ())/;    /* F.setpointsize (+)    ; A.setfont (f);    int newpixelsize = (f.pointsize () * Screen->logicaldotsperinch ())/;    */    ...}

In fact, as long as the Qapplication font is set, the font size of all the interface elements you apply will change. If you want to set the font for a Widget individually, you can call the SetFont () method against it.

The above code also shows the conversion from pointsize to Pixelsize, and once you get the appropriate pixelsize, you can set the dimensions of non-text interface elements based on it.

Qt Get screen resolution

Before we say it's best to combine resolution and DPI to tweak interface elements together. The use of DPI has been briefly introduced, the resolution is left.

The Size property of the QScreen class returns the pixel dimensions of the screen, and availablesize returns the dimensions that the app can use. The difference between the two is that availablesize removes the size that the window manager occupies (on the computer, the taskbar, and the status bar on the Android phone).

The following is a simple example code:

#ifdef ANDROID    qsize iconsize (+);    ...    QScreen *screen = Qapp->primaryscreen ();    Qfont f = Qapp->font ();    int pixelsize = (f.pointsize () * Screen->logicaldotsperinch ())/;    Qsize screensize = Screen->size ();    if (Screensize.width () > 960 | | screensize.height () > 960)    {        iconsize *= ((qreal) pixelsize)/;    } #endif

We know how to set up a Qt on Android app to fit a wide variety of screen sizes, but what's the effect of running your app, depends on how you use the content described above ...

Other articles in this series:

  • Qt on Android: text-to-detail Hello world whole process
  • Introduction to the development of Qt 5.2 for Android under Windows
  • Qt for Android deployment process Analysis
  • QT on Android: output qt Debug information to Logcat
  • Qt on ANDROID:QT 5.3.0 released for Android improvement instructions
  • Qt on Android episode 1 (translation)
  • Qt on Android episode 2 (translation)
  • Qt on Android episode 3 (translation)
  • Qt on Android episode 4 (translation)
  • Qt for Android compiled pure C project
  • Windows under Qt for Android compiled Android C language executable program
  • Qt on Android:android SDK installation
  • Qt on android:http download and JSON parsing
  • Qt on Android Settings app is named Chinese
  • QT on Android: let Qt Widgets and QT Quick apply full screen display



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.