从其他 qml 文件更改元素的属性 [英] changing property of element from other qml file
问题描述
我知道有很多类似的主题,我尝试从他们那里得到答案,但仍然没有结果.
I know that there is tons of topic similar like this, I try to implement answer from them and I still have no results.
我从 qt creator 那里拿了一些示例项目来玩这个.我改变了 qml 文件的可见性(将每个文件视为其他屏幕).吃完第三个屏幕后,我想让第二个屏幕不可见.这是我想要更改属性的代码:
I take some sample project from qt creator to play with this. I play with changing visibility of qml files ( treat every file as other screen). After lunching 3rd screen I want to make the second one invisible. Here Is the code where I want change property in it:
MyLuncherList.qml
MyLuncherList.qml
import QtQuick 2.0
Rectangle {
Item
{
id:ei
visible:false
clip: true
property url itemUrl
onItemUrlChanged:
{
visible = (itemUrl== '' ? false : true);
}
anchors.fill: parent
anchors.bottomMargin: 40
Rectangle
{
id:bg
anchors.fill: parent
color: "white"
}
MouseArea
{
anchors.fill: parent
enabled: ei.visible
//takes mouse events
}
Loader
{
focus:true
source: ei.itemUrl
anchors.fill: parent
}
}
}
这是我要执行操作的代码
查看2.qml
and here is the code where I want to make a action
View2.qml
import QtQuick 2.0
Rectangle {
width: 100
height: 62
Text
{
text: "second screen"
}
MyLuncherList
{
id:luncherList
}
Rectangle
{
x: 50
y: 30
width: 120
height: 60
color: "red"
MouseArea
{
anchors.fill: parent
id: mouseAreaWhichHides
onClicked:
{
luncherList.ei.itemUrl = '';
}
}
}
}
我得到了错误:qrc:///View2.qml:29: TypeError: Type error
这行的哪一点 luncherList.ei.itemUrl = '';
类型错误说我与类型不匹配,但我什至不确定,如果我以正确的方式执行此访问过程,所以我问如何更改
and I got the error: qrc:///View2.qml:29: TypeError: Type error
which point on this line luncherList.ei.itemUrl = '';
Type error says that I make some mismatch with Type, but I’m not even sure, if I do this access process in properly way, so I’m asking how to change property of
ei.itemUrl
来自
View2.qml
在工作中.
推荐答案
ei 元素不能直接在其他 QML 文件中使用.您可以使用别名来执行此操作.
The ei element won't be available directly in other QML file. You can use an alias to do it.
property alias callUrl: ei.itemUrl
并从其他 QML 文件中调用它
and call it from other QML file
luncherList.callUrl='file:///home/user/file.jpg'
这篇关于从其他 qml 文件更改元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!