Qt on Android: how to adapt to different screen sizes, qtandroid

Source: Internet
Author: User

Qt on Android: how to adapt to different screen sizes, qtandroid

How can I use Android apps developed by Qt to adapt to various screen sizes of Android smartphones?

Speaking of the screen size, there are mobile phone screens from 2.8 to 8.9, which is more painful for programmers. The Android project has taken this issue into consideration. resource files and comparison labels all have ldpi, mdpi, hdpi, and xhdpi versions. The Android framework automatically selects corresponding icons Based on the screen size, in this way, the application looks similar on screens of different sizes.

What about the Qt application? In fact, this mechanism is not very useful for Android (only App icons can be used for free), and everything has to be done by yourself. What should we do?

First, we need to understand DPI, and then the font size.

DPI and font size

DPI, dot per inch, that is, the number of points contained in each inch. Another concept is PPI, that is, the number of records per inch. In general, DPI is enough for professionals to handle ultra-high DPI scenarios. The use of PPI may be more accurate. In Qt, There is only DPI, so let's just talk about it.

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

On some screens, the horizontal DPI is different from the vertical DPI, that is, the pixel is not a square, which is more complicated ......

When writing an application, we should ideally set the size of the interface elements based on DPI + screen resolution.

QScreen class

In Qt, The QScreen class can obtain DPI-related information.

The group of attributes physicalDotsPerInch/physicalDotsPerInchX/physicalDotsPerInchY in QScreen indicates the physical DPI. LogicalDotsPerInch/logicalDotsPerInchX/logicalDotsPerInchY indicates logical DPI. Qt uses it to calculate the font size. We can use it to convert the pointSize of the font to pixelSize. Let's talk about it.

LogicalDotsPerInch is a simple average value of X and Y, which is enough in most cases. Of course, if you have the ultimate pursuit, ask logicalDotsPerInchX and logicalDotsPerInchY.

QFont class

QFont indicates the font. The font size can be expressed in two ways: pixelSize and pointSize. That is, the pixel size and the dot matrix size. If you use the pixel size to indicate the font size, the font size will not be affected by the DPI. on the computer, you can adjust the DPI of the monitor to observe the interface changes. However, this is not suitable for mobile scenarios that adapt to diversified screen sizes. When developing Android devices, we should use the pointSize of the font, which is also the default Processing Method of Qt applications.

No. The default value is pointSize!

Neither! And look down.

Controls in Qt

In Qt, QLabel, QPushButton, QListWidget, QTabelWidget, and other controls that can be used on Android devices can be used to display text. You can find a bunch of mobile phones with different sizes and use the QFont setPointSize () method to adjust the font dot matrix size and weigh the effect, you can decide how to set the font size of your application.

But there are still non-text scenarios. For example, if you are an image button, what should you do?

My answer is: Calculate the pixel size based on the dot matrix size of the font, and then use this to adjust the size of non-text controls. In this way, non-text elements such as text elements and images on the interface can be matched.

Calculation formula from pointSize to pixelSize: pixelSize = DPI * pointSize/72.

How to Use DPI for Qt applications

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()) / 72;    /*    f.setPointSize(25);    a.setFont(f);    int newPixelSize = (f.pointSize() * screen->logicalDotsPerInch()) / 72;    */    ...}

In fact, it is very simple. If you set the font of QApplication, the font size of all the interface elements of your application will change. If you want to set the font of a Widget separately, you can call the setFont () method for it.

The code above also demonstrates the conversion from pointSize to pixelSize. Once you get the appropriate pixelSize, you can set the size of non-text interface elements based on it.

Qt obtains the screen resolution

We have mentioned that it is best to adjust the interface elements together with the resolution and DPI. DPI usage has been briefly introduced, with resolution left.

The size attribute of the QScreen class can return the pixel size of the screen. availableSize can return the available size of the application. The difference between the two is that availableSize removes the size occupied by the window manager (on the computer is the taskbar, on the Android mobile phone is the status bar and other areas ).

The following is a simple sample code:

#ifdef ANDROID    QSize iconSize(32, 32);    ...    QScreen *screen = qApp->primaryScreen();    QFont f = qApp->font();    int pixelSize = (f.pointSize() * screen->logicalDotsPerInch()) / 72;    QSize screenSize = screen->size();    if(screenSize.width() > 960 || screenSize.height() > 960)    {        iconSize *= ((qreal)pixelSize) / 20;    }#endif

We know how to set the Qt on Android app to adapt to a variety of screen sizes, but what is the effect of your app after it runs depends on how you use the content described above ......

Other articles in this series:

  • Qt on Android: full process of Hello World
  • Introduction to Qt 5.2 for Android development in Windows
  • Analysis of the deployment process of Qt for Android
  • Qt on Android: Output Qt debugging information to logcat.
  • Qt on Android: Qt 5.3.0 released. Improvements for Android
  • Qt on Android Episode 1)
  • Qt on Android Episode 2)
  • Qt on Android Episode 3)
  • Qt on Android Episode 4)
  • Compiling a pure C project using Qt for Android
  • Compiling Android C language executable programs using Qt for Android in Windows
  • Qt on Android: Android SDK Installation
  • Qt on Android: http download and Json Parsing
  • Set the app name for Qt on Android to Chinese
  • Qt on Android: Enables full screen display of Qt Widgets and Qt Quick apps




How does the android layout adapt to different screen sizes?

You can calculate the space occupied by your popupwindow in real time. For example, if your initial parameter is 300pxX250px, you 'd better instantly convert dp to px.
/**
* Convert the dip or dp value to the px value to ensure that the size remains the same
*
* @ Param dipValue
* @ Param scale
* (Density in DisplayMetrics class)
* @ Return
*/
Public static int dip2px (float dipValue, float scale ){
Return (int) (dipValue * scale + 0.5f );
}
Public static DisplayMetrics getMetrics (Activity activity ){
DisplayMetrics metrics = new DisplayMetrics ();
Display display = activity. getWindowManager (). getdefadisplay Display ();
Display. getMetrics (metrics );
Return metrics;
}

New PopupWindow (findViewById (R. id. mainlayout), dip2px (300, getMetrics (youractivity), and dip2px (250, getMetrics (youractivity )));

In addition, this method will also lose effect if it is displayed on the tablet. If you want to display it in a high resolution, you have to pass a higher value for the same display effect. The following is a method to determine whether it is a tablet.
Private boolean isPad (){
WindowManager wm = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
Display display = wm. getDefaultDisplay ();
// Screen width
Float screenWidth = display. getWidth ();
// Screen height
Float screenHeight = display. getHeight ();
DisplayMetrics dm = new DisplayMetrics ();
Display. getMetrics (dm );
Double x = Math. pow (dm. widthPixels/dm. xdpi, 2 );
Double y = Math. pow (dm. heightPixels/dm. ydpi, 2 );
// Screen size
Double screenInches = Math. sqrt (x + y );
// Pad if the size is greater than 6
If (screenInches & gt; = 6.0 ){
Return true;
}
Return false;
}... Remaining full text>

How does the Android program adapt to the orientation and size of the screen?

Different Android targets have different sizes. the application interface needs to adjust the size of interface elements for different sizes. In addition, the screen can be switched between the landscape screen and the portrait screen, and the interface also needs to be adjusted. By default, when the screen is switched, The onCreate () method of the activity will be called again, so you can use the following code to read the screen direction: public void onCreate () {if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE) {Log. I ("info", "landscape");} else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT) {Log. I ("info", "portrait") ;}} if you are in androidmanifest. add android: configChanges = "orientation | keyboardHidden | navigation in xml. When the screen is flipped, the Activity will not repeatedly call onCreate (), onPause (), and onResume (). instead, call onConfigurationChanged (Configuration newConfig) int screenWidth, screenHeight; WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); screenWidth = display. getWidth (); screenHeight = display. getHeight (); someone also mentioned another method: DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm );

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.