`Screen.pixelDensity` 在非视觉组件中等于 0 [英] `Screen.pixelDensity` equals 0 in non-visual components

查看:69
本文介绍了`Screen.pixelDensity` 在非视觉组件中等于 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 QML 中使用 Screen.pixelDensity 来计算我的视觉组件的大小.为了将元素的属性保留在一个地方(有几个),我创建了一个容器对象:

I want to use Screen.pixelDensity in QML to calculate the size of my visual components. In order to keep an element's properties in one place (there are a couple of them) I created a container object:

import QtQuick 2.0

Item
{
    readonly property double constantValue: 100 * Screen.pixelDensity
    property double first
    property double second: first + constantValue

    // and so on

    Component.onCompleted: console.log(Screen.pixelDensity) // (1)
}

对象初始化时,语句(1)报告Screen.pixelDensity == 0

When the object is initialized, the statement (1) reports that Screen.pixelDensity == 0

如果我在 main.qml 中放置相同的语句:

If I put the same statement in my main.qml:

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0

ApplicationWindow
{
    title: qsTr("title")
    width: 480
    height: 800

    // some visual stuff here

    Component.onCompleted: console.log("testing: "  + Screen.pixelDensity)
}

然后 Screen.pixelDensity 被报告为非零,正如预期的那样.

then Screen.pixelDensity is reported to be non-zero, as expected.

为什么属性在第一种情况下行为不当?

Why does the property misbehave in the first case?

推荐答案

Screen

在组件完成后,屏幕附加对象在 Item 或 Item 派生类型内有效.在这些项目中它指的是当前显示项目的屏幕.

The Screen attached object is valid inside Item or Item derived types, after component completion. Inside these items it refers to the screen that the item is currently being displayed on.

如果该项目未显示(这是我从您的问题中了解到的) 则无法访问该信息...因此显示为 0.

If the item is not displayed (that's what I understood from your question) then the information can't be reached... Thus it displays 0.

这篇关于`Screen.pixelDensity` 在非视觉组件中等于 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆