qt - QML 在应用程序启动时筛选不正确的属性

标签 qt screen qml screen-size

我在开始时需要正确的屏幕尺寸,但如果我执行以下代码:

import QtQuick 2.0
import QtQuick.Window 2.0

Rectangle {

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log( Screen.height, Screen.width, Screen.desktopAvailableHeight, Screen.desktopAvailableWidth);
        }
    }

    Component.onCompleted: {
        console.log( Screen.height, Screen.width, Screen.desktopAvailableHeight, Screen.desktopAvailableWidth);
    }
}

我一开始有零
并在鼠标单击时校正屏幕尺寸
0 0 0 0
1200 1920 1133 1920

怎么能看出这是什么?
以及如何在应用程序开始时获得正确的屏幕尺寸?

最佳答案

目前我正在解决类似的问题。
来自 Screen documentation :

Note that the Screen type is not valid at Component.onCompleted, because the Item or Window has not been displayed on a screen by this time



所以你将无法使用 onCompleted()从屏幕项目中获取屏幕尺寸。
但是,您可以从应用程序的 C++ 端获取此信息并传递给 QML 上下文:
QScreen* screen = QGuiApplication::primaryScreen();
QSize screenSize = screen->size();
QtQuick2ApplicationViewer viewer;
QQmlContext* qmlContext = viewer.rootContext();
qmlContext->setContextProperty("screenSize", screenSize);

关于qt - QML 在应用程序启动时筛选不正确的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22878452/

相关文章:

c++ - 将静态 Qt 库链接到 C 程序

c++ - 如何从另一个函数执行一个函数?

c++ - 将图片从在线保存到本地存储qt 5.1

android - Android中的布局拼图开发

javascript - 使用 qml 读取多个 Xml 属性

qt - 如何在 QML 旋转框中使用 float

Qt qmake : removing entries from a variable

iphone - 针对两种 iPhone 屏幕尺寸的编程

python - Kivy ScreenManager 破坏了 BoxLayout

qt - QML ListView 不会在鼠标单击时响应