2014-02-19 3 views
1

У меня есть QtQuick 1,0
Я использую следующий код:Невозможно получить доступ к QML переменной/идентификатор глобально

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: Can't find variable: myMouseArea /usr/lib/i386-linux-gnu/qt4/bin/qmlviewer exited with code 0

я не делаю знайте, если подробности, которые я вам предоставил, достаточны, пожалуйста, дайте мне знать, если что-либо еще мне не хватает.

Я использую этот код в качестве примера:
http://docs.knobbits.org/qt4/declarative-modelviews-gridview-qml-gridview-example-gridview-example-qml.html

+0

Удален моим ответ, как вопрос был изменен, и он больше не применяется. – Matthew

ответ

2

Вы не можете получить доступ к myMouseArea, потому что он создан в контексте делегирования. Вы не можете получить доступ к делегату, кроме currentItem. Но вы можете свободно обращаться к view в контексте делегата, чтобы установить currentIndex в прикрепленное свойство index.

Это является исправленным кодом:

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 
    } 
} 
+0

Большое спасибо. работал над тем, чтобы это произошло. Его работа замечательно! – Jino

Смежные вопросы