Qt 无法全局访问QML变量/id
我有QtQuick 1.0Qt 无法全局访问QML变量/id,qt,qml,qt-quick,Qt,Qml,Qt Quick,我有QtQuick 1.0 我使用以下代码: Rectangle { Component { id: appDelegate MouseArea{ id:myMouseArea hoverEnabled: true onClicked:{ onClicked: load.source = page;
我使用以下代码:
Rectangle {
Component {
id: appDelegate
MouseArea{
id:myMouseArea
hoverEnabled: true
onClicked:{
onClicked: load.source = page;
}
}
Loader {
id: load
}
}
GridView {
id: view
// I am unable to access myMouseArea here.
highlight: myMouseArea.containsMouse ? appHighlight : !appHighlight
delegate: appDelegate
}
}
它给了我以下错误:
ReferenceError:找不到变量:myMouseArea
/usr/lib/i386 linux gnu/qt4/bin/qmlviewer已退出,代码为0
我不知道我提供的细节是否足够,如果我还缺少什么,请告诉我
我以这段代码为例:您无法访问myMouseArea,因为它是在委托上下文中创建的。您不能访问其他代理。但您可以在委托上下文中自由访问
视图
,以设置为附加属性索引
这是一个已更正的代码:
Rectangle {
width: 360
height: 360
Component { // It must be a component, if we want use it as delegate
id: appDelegate
// its not possible to have more than one element inside component
Rectangle
{
// need to set size of item, anchors wont work here
// could use view.cellWidth and view.cellHeight to keep it DRY
width: 96
height: 66
color: "green" // color only to see the place of MouseArea
MouseArea {
id:myMouseArea
anchors.fill: parent // this setup the size to whole rectangle
// it this item have the size 0,0 it will simple do not work
hoverEnabled: true
onEntered: {
// we know the mouse is inside this region
// setting this value will show the highlight rectangle
view.currentIndex = index;
}
onClicked:{
onClicked: load.source = page;
}
}
Loader {
// this is not needed but it's wise to not keep zero size
anchors.fill: parent
id: load
}
}
}
GridView {
id: view
// the size of GridView must be set,
// as otherwise no delegate will not show
anchors.fill: parent
anchors.margins: 5
cellWidth: 100
cellHeight: 70
// Rectangle will act as a border.
// Size and position is set by GridView
// to the size and position of currentItem.
// This is no a item, this makes a Component
// as highlight property needs one.
// You can create a Component like appDelegate.
highlight : Rectangle {
border.width: 2
border.color: "blue"
}
// some ListModel to setup the page variable inside delegate context
model: ListModel {
ListElement { page: "test1.qml"; }
ListElement { page: "test2.qml"; }
ListElement { page: "test3.qml"; }
}
delegate: appDelegate
}
}
非常感谢你。我们正在努力让这一切发生。它工作得很好!删除了我的答案,因为问题已修改,不再适用。