2011-09-22 6 views
0

MouseArea 요소가 포함 된 QT QML에 Item을 만들었습니다. 여기 MouseArea의 그리드 Qt QML

import QtQuick 1.0 
Rectangle { 
id: base 
width: 240 
height: 320 
x:0; y:0 
color: "#323138" 
/////////////////////// MAIN FOCUSSCOPE ////////////////////// 
FocusScope { 
    id: mainfocus 
    height: base.height; width: base.width 
    focus: true 
    /////////////////////// MAIN GRID /////////////////////////// 
    GridView { 
     id: maingrid 
     width: base.width-10; height: base.height-titlebar.height-10 
     x: 5; y: titlebar.height+5; 
     cellHeight: maingrid.height/3; cellWidth: maingrid.width/3-1 
     Component { 
      id: myicon 
      Rectangle { 
       id: wrapper 
       height: maingrid.cellHeight-10; width: maingrid.cellWidth-10 
       radius: 8; smooth: true 
       color: GridView.isCurrentItem ? "#c0d0c0" : "transparent" 
       focus: true 
       MouseArea { 
        id: clickable 
        anchors.fill: wrapper 
        hoverEnabled: true 
        //onClicked: func() 
       } 
       Image { 
        id: iconpic 
        source: "./ui6.svg" 
        anchors.centerIn: wrapper 
       } 
       Text { 
        id: iconname 
        color: wrapper.GridView.isCurrentItem ? "black" : "#c8dbc8" 
        anchors.top: iconpic.bottom; anchors.horizontalCenter: iconpic.horizontalCenter 
        text: name 
       } 
      } 
     } 
     model: 4 
     delegate: myicon 
     focus: true 
    } 
} 
//////////////////////// TITLEBAR /////////////////////// 
Rectangle { 
    id: titlebar 
    x:base.x 
    y:base.y 
    height: 25; width: base.width 
    color : "#356f47" 
    Text { 
     color: "#fdfdfd" 
     anchors.centerIn: titlebar 
     text: "title" 
    } 
} 
} 

내가 그것을 나에게 내가 다른 기능에 사용할 수있는 만든 맞춤 제작 한 클릭 수 항목의 그리드를 제공하도록 이러한 항목의 격자를 만들고 싶어, 코드입니다.

GridView 요소를 사용하여 만든 사용자 정의 항목의 번호를 템플릿으로 사용하는 격자를 만들 수있었습니다.

문제는이 항목 중 하나를 클릭하면 내 항목에 하나의 MouseArea 요소가 있기 때문에 단일 기능을 실행한다는 것입니다. 항목에 대한 클릭을 감지 할 수는 있지만 클릭 한 항목을 고유하게 결정할 수는 없습니다. 이것을 어떻게 성취합니까?

물론 잘못된 것일 수 있으므로 다른 제안도 환영합니다. 감사합니다

답변

2

GridView 항목이 인스턴스를 만들 때 인덱스 변수를 상속합니다. 고유 항목을 식별합니다.

MouseArea { 
    id: clickable 
    anchors.fill: wrapper 
    hoverEnabled: true 
    onClicked: { 
     maingrid.currentIndex=index; 

     switch (index) 
     { 
     case 0: 
      //Function or method to call when clicked on first item 
      break; 
     case 1: 
      //Function or method to call when clicked on second item 
      break; 
     default: 
      //Function or method to call when clicked on another item 
      break; 
     } 
    } 
} 

도움이되기를 바랍니다.

관련 문제