Qt QML-无法在“中获取宽度、高度等”;“关于已完成的”;
我需要在Qt QML-无法在“中获取宽度、高度等”;“关于已完成的”;,qt,qml,qtquick2,Qt,Qml,Qtquick2,我需要在组件中获取矩形的宽度和高度。未完成的处理程序,但如果打印相同的矩形,则会得到一些未知值,代码如下: [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("t
组件中获取矩形的宽度和高度。未完成的
处理程序,但如果打印相同的矩形,则会得到一些未知值,代码如下:
[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
中获得宽度和高度?直接嵌套在窗口中的项目的父项不是窗口,而是窗口的contentItem
请尝试以下方法:
Rectangle{
id:rect
width: appWindow.width * 0.75
height: appWindow.height * 0.70
}
这同样适用于您的“完整代码”:
您可以在第二个矩形中使用parent
,因为它的父项将是第一个矩形,但由于第一个矩形嵌套在窗口中,因此它需要引用窗口而不是父项来确定属性大小。直接嵌套在窗口中的项目的父项
不是窗口,但是窗口的contentItem
请尝试以下方法:
Rectangle{
id:rect
width: appWindow.width * 0.75
height: appWindow.height * 0.70
}
这同样适用于您的“完整代码”:
您可以在第二个矩形中使用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
}
}
这里您假设sometrect
的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
这意味着sometrect.width
也将0
,直到appWindow.contentItem
的宽度调整为600
——直到执行组件.onCompleted
时才会发生
解决方案是,在属性appWindow.width
中,将依赖项的宽度缩短为appWindow.contentItem
,因为最终值从一开始就可用
让我们看另一个例子:
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
调整大小后才会改变。因此,最好按照我使用的高度
的方法进行操作
有许多QML组件,父组件可能不在其中,看起来是什么。用于自定义组件,例如使用默认属性别名
将“子项”推入嵌套项
s的所有组件
好的,我将尝试重新开始:
你的问题是一种误解,隐藏在家长
背后的是什么
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
}
}
这里您假设sometrect
的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
这意味着sometrect.width
也将0
,直到appWindow.contentItem
的宽度调整为600
——直到执行组件.onCompleted
时才会发生
解决方案是,在属性appWindow.width
中,将依赖项的宽度缩短为appWindow.contentItem
,因为最终值从一开始就可用
让我们看另一个例子:
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
调整大小后才会改变。因此,最好按照我使用的高度
的方法进行操作
有许多QML组件,父组件可能不在其中,看起来是什么。用于自定义组件,例如使用默认属性别名
将“子项”推入嵌套项
s的所有组件
没有变化。我得到了0。不可能,它应该是这样工作的。尝试构建-运行qmake,您的更改可能没有得到反映。它为我打印qml:Width=450
。很抱歉,我在添加问题时遗漏了一些代码。我已编辑,请检查。如果父对象本身是矩形,并且我正在尝试在oncompleted
中获取宽度,则我遇到了问题。无论如何,如果父窗口是应用程序窗口,则上述操作有效。你能为这个用例提出建议吗?我不理解你的问题。很简单,窗口的子项会自动嵌套在内容项中,因此,如果要将项目调整为窗口大小,请使用窗口id,而不是父项
。如果父项是窗口以外的另一项,则可以使用parent
。此外,如果您选择使用id而不是父项,则始终可以使用id。没有更改。我得到了0。不可能,它应该是这样工作的。尝试构建-运行qmake,您的更改可能没有得到反映。它为我打印qml:Width=450
。很抱歉,我在添加问题时遗漏了一些代码。我已编辑,请检查。如果父对象本身是矩形,并且我正在尝试在oncompleted
中获取宽度,则我遇到了问题。无论如何,如果父窗口是应用程序窗口,则上述操作有效。你能推荐这种用法吗