Qt on Android: Create a scalable interface, qtandroid

Source: Internet
Author: User

Qt on Android: Create a scalable interface, qtandroid

To use Qt to develop Android applications, you also need to adapt to different mobile devices and adapt to a variety of screens and resolutions. This time we will discuss how to use the mechanism provided by Qt to create a scalable interface.

DPI

DPI must be explained.

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.

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.

DPI affects the interface: For images with the same resolution (in pixels), the larger the DPI, the smaller the screen.

Three types of scalable Elements

A Qt mobile app has three types of scalable UI elements:

 

  • Text
  • Image
  • Background

 

Let's take a look at it separately.

Text

For text, you only need to set the pointSize of the font (QFont) used by the specific text display and input control. Such methods as QLabel, QPushButton, and QLineEdit are applicable.

QFont has two expressions: pixelSize and pointSize. PointSize will adjust the font Based on the DPI of the device where the application is located, so that the effects on devices with different DPI levels are consistent.

In Qt, the font used by a Widget can be changed separately, or the global font can be provided through QApplication. In this way, widgets without specific settings will use the global font.

Image

As we have mentioned above, the larger the screen DPI for images with the same resolution, the smaller the screen DPI will be.

Qt can handle this situation. Let's take QPixmap as an example.

QPixmap has two methods:

 

  • Void setDevicePixelRatio (qreal scaleFactor)
  • Qreal QPixmap: devicePixelRatio () const

 

These two methods operate on a property called device pixel ratio, which specifies the conversion ratio between device-related pixels and device-independent pixels. We can adjust it to change the effect of an image on the mobile phone screen.

The QImage class also has these two methods. You can refer to the Qt help to see the details of the API.

How can I obtain the devicePixelRation of a device?

QScreen has a method to return this value: qreal QScreen: devicePixelRatio () const

QGuiApplication and QWindow classes also have methods of the same name.

We can also use the logicalDotsPerInch () method of QScreen to calculate by using a common DPI (such as 72). The following is the sample code:

 

[Cpp]View plaincopy
  1. Float SizeUtil: dpiFactor ()
  2. {
  3. QScreen * screen = qApp-> primaryScreen ();
  4. Return 72/screen-> logicalDotsPerInch ();
  5. }

 

The above method is used in the following example.

It should be noted that when using QPixmap and QImage, the built-in control of Qt determines the size of the Control Based on devicePixelRation. In our example, QLabel is used to display the image.

Background

The background is either a certain color or an image. When using an image as the background, the image is stretched. Android uses the 9 patch image to solve this problem. Qt also provides something similar: border-image.

In applications based on Qt Widgets, we can set border-image through qss to construct a scalable background.

 

In the help of Qt, the four lines cut an image into nine parts. during use, the four corners can be kept unchanged, and other parts can be stretched or tiled to adapt to the interface space size.

 

Well, there are so many basic backgrounds. Let's look at a simple example.

Scalable interface Example

Let's first look at the results and then look at the code.

Is the running effect on the PC:

 

Is the effect on the phone. At this time, devicePixelRatio is not set for the image.

 

The devicePixelRatio is not set, and the image looks much smaller. Comparing the devicePixelRatio with the text, we can see that the picture is out of proportion.

Is the effect of setting devicePixelRatio, it looks consistent.

Code Analysis

Created an application based on Qt Widgets, named scalabeUI, and created two files: sizeUtil. h and sizeUtil. cpp.

Two image resources are used in the project:

I added the image to qrc.

 

SizeUtil. h is as follows:

 

[Cpp]View plaincopy
  1. # Ifndef SIZEUTIL_H
  2. # Define SIZEUTIL_H
  3. # Include <QFont>
  4. # Include <QString>
  5. Class SizeUtil
  6. {
  7. Private:
  8. SizeUtil (){}
  9. SizeUtil (const SizeUtil &);
  10. SizeUtil & operator = (const SizeUtil &);
  11. Public:
  12. ~ SizeUtil (){}
  13. Static SizeUtil & instance ();
  14. Int defaultFontHeight ();
  15. Int widthWithDefaultFont (const QString & text );
  16. Int widthWithFont (const QString & text, int fontPointSize );
  17. Int fontHeight (int fontPointSize );
  18. Float dpiFactor ();
  19. };
  20. # Endif // SIZEUTIL_H


SizeUtil. cpp is as follows:

 

 

[Cpp]View plaincopy
  1. # Include "sizeUtil. h"
  2. # Include <QApplication>
  3. # Include <QFontMetrics>
  4. # Include <QScreen>
  5. SizeUtil & SizeUtil: instance ()
  6. {
  7. Static SizeUtil util;
  8. Return util;
  9. }
  10. Int SizeUtil: defaultFontHeight ()
  11. {
  12. Return qApp-> fontMetrics (). height ();
  13. }
  14. Int SizeUtil: widthWithDefaultFont (const QString & text)
  15. {
  16. Return qApp-> fontMetrics (). boundingRect (text). width ();
  17. }
  18. Int SizeUtil: widthWithFont (const QString & text, int fontPointSize)
  19. {
  20. QFont f = qApp-> font ();
  21. F. setPointSize (fontPointSize );
  22. QFontMetrics fm (f );
  23. Return fm. boundingRect (text). width ();
  24. }
  25. Int SizeUtil: fontHeight (int fontPointSize)
  26. {
  27. QFont f = qApp-> font ();
  28. F. setPointSize (fontPointSize );
  29. QFontMetrics fm (f );
  30. Return fm. height ();
  31. }
  32. Float SizeUtil: dpiFactor ()
  33. {
  34. QScreen * screen = qApp-> primaryScreen ();
  35. Return 72/screen-> logicalDotsPerInch ();
  36. }


The SizeUtil class is mainly used to calculate the pixel size of text.

 

The new project wizard generates Widgets. cpp and widgets. h. I modified the widgets. cpp and processed the text, images, and backgrounds. The Code is as follows:

 

[Cpp]View plaincopy
  1. # Include "widget. h"
  2. # Include <QVBoxLayout>
  3. # Include <QHBoxLayout>
  4. # Include <QLabel>
  5. # Include <QPushButton>
  6. # Include "sizeUtil. h"
  7. Widget: Widget (QWidget * parent)
  8. : QWidget (parent)
  9. {
  10. QVBoxLayout * layout = new QVBoxLayout (this );
  11. // Case 1. background
  12. QLabel * label = new QLabel ("Hello Scalable Label ");
  13. Layout-> addWidget (label, 1 );
  14. /* Top right bottom left */
  15. Label-> setStyleSheet (
  16. "QLabel {border-image: url (:/bkgnd.9.png) 38 6 6 16 ;"
  17. "Border-left-width: 16; border-top-width: 38 ;"
  18. "Border-right-width: 6; border-bottom-width: 6 }");
  19. // Case 2. image
  20. QLabel * head = new QLabel;
  21. QPixmap orig (":/head.png ");
  22. Orig. setDevicePixelRatio (SizeUtil: instance (). dpiFactor ());
  23. Head-> setPixmap (orig );
  24. Layout-> addWidget (head );
  25. // Case 3. text button
  26. QHBoxLayout * hlayout = new QHBoxLayout;
  27. Layout-> addLayout (hlayout );
  28. QPushButton * button = new QPushButton ("Text Button ");
  29. Hlayout-> addWidget (button );
  30. Hlayout-> addStretch (1 );
  31. }
  32. Widget ::~ Widget ()
  33. {
  34. }

 

This is all about the code. Although simple, the problem can be explained.

 

Blog Star Selection, click to vote for me, thank you. You can also vote once. You can vote every day.

The complete project code can be downloaded here: Click to download.

Other brilliant articles

 

Using Autocompletetext in android dialog for large-scale website architecture design-Solr mysql hash index android learning notes (32) grid view (GridView) and graphic switcher (ImageSwi... android learning notes (31) List component (ExpandableListView)

 

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.