qt 分辨率问题 安卓_Qt for Android dp转换

本文介绍了在Qt for Android中处理分辨率问题,特别是dp(与密度无关的像素)单位的转换。提供了计算dp到实际像素的公式,并展示了一个QML模板,用于在界面布局中自动根据屏幕密度进行适配。此外,还提到了点Size(pointSize)到像素Size(pixelSize)的转换公式。
摘要由CSDN通过智能技术生成

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 {

text:"A"

font.pixelSize: units.dp(50)

}

}

\endqml

Here is a short example too:

\qml

import QtQuick 2.0

import "."

Rectangle {

width: units.dp(100)

height: units.dp(80)

Label {

text:"A"

font.pixelSize: units.dp(50)

}

}

\endqml

*/

Object {

id: units

/*!

\internal

This holds the pixel density used for converting millimeters into pixels. This is the exact

value from \l Screen:pixelDensity, but that property only works from within a \l Window type,

so this is hardcoded here and we update it from within \l ApplicationWindow

*/

property real __pixelDensity: 4.5 // pixels/mm

function cm(number) {

return number * __pixelDensity * 10

}

/*!

Converts millimeters into pixels. Used primarily by \l units::dp, but there might be other

uses for it as well.

*/

function mm(number) {

return number * __pixelDensity

}

function inch(number) {

return number * __pixelDensity * 10 * 2.54

}

// 相对以视窗的宽度,视窗宽度是100vm

function vw(i, width) {

return number * (width / 100)

}

// 相对以视窗的高度,视窗高度是100vh

function vh(number, height) {

return number * (height / 100)

}

function vmin(number, width, height) {

return number * (Math.min(width, height) / 100)

}

function vmax(number, width, height){

return number * (Math.max(width, height) / 100)

}

function dp(number) {

var px = Math.round(number * (__pixelDensity * 25.4 / 160));

if(Qt.platform.os === "windows" || Qt.platform.os === "mac")

return px * 2;

else

return px;

}

}// qmldir

module Material

singleton units 0.1 Units.qml// Object.qml

import QtQuick 2.0

QtObject {

id: object

//default property alias data: object.__data

//property list __data

default property alias children: object.__childrenFix

property list __childrenFix: [QtObject {}]

}

关于pointSize

从 pointSize 到 pixelSize 的计算公式: pixelSize = DPI * pointSize/72 。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值