px(像素):像素是指基本原色素及其灰度的基本编码。像素是构成数码影像的基本单元,通常以像素每英寸PPI(pixels per inch)为单位来表示影像分辨率的大小。
dp(与密度无关的像素):一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。
计算公式: 1dp * 像素密度 / 160 = 实际像素数
下面是一个模板用于把dp转换成实际的像素值(px)。// Units.qml
pragma Singleton
import QtQuick 2.0
/*!
\qmltype units
\brief Provides access to screen-independent units known as DPs (device-independent pixels).
This singleton provides methods for building a user interface that automatically scales based on
the screen density. Use the \l units::dp function wherever you need to specify a screen size,
and your app will automatically scale to any screen density.
Here is a short example:
\qml
import QtQuick 2.0
import Material 0.1
Rectangle {
width: units.dp(100)
height: units.dp(80)
Label {