QML - 无法在 "On Completed" 中获取宽度、高度等
QML - Not able to get width,height etc in "On Completed"
我需要在 Component.OnCompleted
处理程序中获取矩形的宽度和高度,但如果我打印相同的内容,我会得到一些未知值,以下是代码:
[EDIT-1] - 添加了更多代码。
import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Window 2.3
ApplicationWindow {
id: appWindow
visible: true
width: 600
height: 400
title: qsTr("test")
flags: Qt.Window | Qt.FramelessWindowHint
Rectangle{
id:rectParent
width:parent.width * 0.75
height: parent.height * 0.70
Rectangle{
id:rectChild
width:parent.width * 0.75
height: parent.height * 0.70
Component.onCompleted: {
console.log("Width=",width) //prints "0" .
}
}
}
}
如何在 onCompleted
中获取宽度、高度?
parent
项,直接嵌套在window中的不是window,而是window的contentItem
.
试试这个:
Rectangle{
id:rect
width: appWindow.width * 0.75
height: appWindow.height * 0.70
}
这同样适用于你的 "full code":
Rectangle{
id:rectParent
width:appWindow.width * 0.75
height: appWindow.height * 0.70
Rectangle{
id:rectChild
width:parent.width * 0.75
height: parent.height * 0.70
Component.onCompleted: {
console.log("Width=",width) //prints "Width= 337.5"
}
}
}
您可以在第二个矩形中使用 parent
,因为它的父矩形将是第一个矩形,但是由于第一个矩形嵌套在 window 中,因此需要引用 window 而不是其父级以获得 属性 大小。
好的,我会尝试重新开始:
你的问题是误解,隐藏的东西在parent
.
背后
import QtQuick 2.6
import QtQuick.Controls 2.0
ApplicationWindow {
id: appWindow
width: 600
height: 400
visible: true
Rectangle {
id: someRect
width: parent.width * 0.7
heigth: parent.height * 0.7
}
}
这里假设 someRect
的 parent
是 appWindow
,因此,parent.width = appWindow.width = 600
。 这是错误的
someRect
的父项不能是 appWindow
,因为 appWindow
不是 Item
类型。其实someRect.parent === appWindow.contentItem
,所以width: parent.width => width: appWindow.contentItem.width
.
问题是,contentItem
的宽度在创建时是0
,只有在创建后才会重新占用appWindow.width
。
这意味着,someRect.width
也是 0
,直到 appWindow.contentItem
的宽度已调整为 600
- 这不会发生,直到 Component.onCompleted
被执行。
解决方案是,缩短对 appWindow.contentItem
宽度的依赖,因为最终值从一开始就可用,在 属性 appWindow.width
.
再看一个例子:
import QtQuick 2.6
import QtQuick.Controls 2.0
ApplicationWindow {
id: appWindow
width: 600
height: 400
visible: true
Rectangle {
id: someRect
width: parent.width * 0.7 // depends on appWindow.contentItem.width -> initally 0, changing soon after
heigth: appWindow.height * 0.7 // depends on appWindow.height -> initially 400.
Component.onCompleted: console.log('someRect:', width, height) // prints: "someRect: 0 280"
Rectangle {
id: someOtherRect
width: parent.width * 0.7 // depends on someRect.width which is initally 0 as it depends on appWindow.contentItem.width
height: parent.height * 0.7 // depends on someRect.height which is initally 400 * 0.7
Component.onCompleted: console.log('someOtherRect:', width, height) // prints "someOtherRect: 0, 196"
}
}
}
在这里,高度将从一开始就设置好,而宽度只会在 appWindow.contentItem
调整大小时才改变。所以还是跟着方法比较好,我用的是height
.
There are many QML Components, where the parent might not be, what it seems. For custom Components that are, e.g. all Components that use default property alias
to push "children" into nested Item
s.
我需要在 Component.OnCompleted
处理程序中获取矩形的宽度和高度,但如果我打印相同的内容,我会得到一些未知值,以下是代码:
[EDIT-1] - 添加了更多代码。
import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Window 2.3
ApplicationWindow {
id: appWindow
visible: true
width: 600
height: 400
title: qsTr("test")
flags: Qt.Window | Qt.FramelessWindowHint
Rectangle{
id:rectParent
width:parent.width * 0.75
height: parent.height * 0.70
Rectangle{
id:rectChild
width:parent.width * 0.75
height: parent.height * 0.70
Component.onCompleted: {
console.log("Width=",width) //prints "0" .
}
}
}
}
如何在 onCompleted
中获取宽度、高度?
parent
项,直接嵌套在window中的不是window,而是window的contentItem
.
试试这个:
Rectangle{
id:rect
width: appWindow.width * 0.75
height: appWindow.height * 0.70
}
这同样适用于你的 "full code":
Rectangle{
id:rectParent
width:appWindow.width * 0.75
height: appWindow.height * 0.70
Rectangle{
id:rectChild
width:parent.width * 0.75
height: parent.height * 0.70
Component.onCompleted: {
console.log("Width=",width) //prints "Width= 337.5"
}
}
}
您可以在第二个矩形中使用 parent
,因为它的父矩形将是第一个矩形,但是由于第一个矩形嵌套在 window 中,因此需要引用 window 而不是其父级以获得 属性 大小。
好的,我会尝试重新开始:
你的问题是误解,隐藏的东西在parent
.
import QtQuick 2.6
import QtQuick.Controls 2.0
ApplicationWindow {
id: appWindow
width: 600
height: 400
visible: true
Rectangle {
id: someRect
width: parent.width * 0.7
heigth: parent.height * 0.7
}
}
这里假设 someRect
的 parent
是 appWindow
,因此,parent.width = appWindow.width = 600
。 这是错误的
someRect
的父项不能是 appWindow
,因为 appWindow
不是 Item
类型。其实someRect.parent === appWindow.contentItem
,所以width: parent.width => width: appWindow.contentItem.width
.
问题是,contentItem
的宽度在创建时是0
,只有在创建后才会重新占用appWindow.width
。
这意味着,someRect.width
也是 0
,直到 appWindow.contentItem
的宽度已调整为 600
- 这不会发生,直到 Component.onCompleted
被执行。
解决方案是,缩短对 appWindow.contentItem
宽度的依赖,因为最终值从一开始就可用,在 属性 appWindow.width
.
再看一个例子:
import QtQuick 2.6
import QtQuick.Controls 2.0
ApplicationWindow {
id: appWindow
width: 600
height: 400
visible: true
Rectangle {
id: someRect
width: parent.width * 0.7 // depends on appWindow.contentItem.width -> initally 0, changing soon after
heigth: appWindow.height * 0.7 // depends on appWindow.height -> initially 400.
Component.onCompleted: console.log('someRect:', width, height) // prints: "someRect: 0 280"
Rectangle {
id: someOtherRect
width: parent.width * 0.7 // depends on someRect.width which is initally 0 as it depends on appWindow.contentItem.width
height: parent.height * 0.7 // depends on someRect.height which is initally 400 * 0.7
Component.onCompleted: console.log('someOtherRect:', width, height) // prints "someOtherRect: 0, 196"
}
}
}
在这里,高度将从一开始就设置好,而宽度只会在 appWindow.contentItem
调整大小时才改变。所以还是跟着方法比较好,我用的是height
.
There are many QML Components, where the parent might not be, what it seems. For custom Components that are, e.g. all Components that use
default property alias
to push "children" into nestedItem
s.