我在开始时需要正确的屏幕尺寸,但如果我执行以下代码:
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 theItem
orWindow
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/