2017-01-26 13 views
3

私は自分のアプリケーションでフォーカスを適切に設定する方法を理解しようとしています。QMLフォーカスはどのように伝達されますか?

MyItem.qmlというコンポーネントがあります。これは、子がフォーカスを取得したときに背景を変更したいのです。私はMyItem.qmlから派生したMyDerivedItem.qmlも持っています.MyItem.qmlは、子がフォーカスを取得した場合に基本クラスの背景を変更する必要があります。

ドキュメントを正しく理解していれば、コンポーネントがフォーカスを取得すると、階層内のすべての親のフォーカスプロパティがtrue(またはFocusScopeコンポーネントに達するまで)に設定されます。

これが当てはまる場合、MyItem.qmlまたはMyDerivedItem.qmlのいずれかのTextFieldを押すと、myItem.focusプロパティがtrueに変更され、背景色が変更されます。

私は何をしたいのかという小さな例を作ろうとしましたが、期待通りに動作しません。

//main.qml 
import QtQuick.Controls 2.0 

ApplicationWindow { 
    height: 768 
    width: 1024 
    visible: true 

    MyDerivedItem { 
     anchors.top: parent.top 
     anchors.left: parent.left 
     anchors.bottom: parent.bottom 
     width: parent.width/2 
    } 
    MyDerivedItem { 
     anchors.top: parent.top 
     anchors.right: parent.right 
     anchors.bottom: parent.bottom 
     width: parent.width/2 
    } 
} 

//MyItem.qml 
import QtQuick 2.7 
import QtQuick.Controls 2.0 

Rectangle { 
    id: myItem 

    default property alias data: column.data 

    color: focus ? "red" : "green" 

    Column { 
     id: column 

     TextField { 
      placeholderText: "Input Text Here" 
     } 
    } 
} 

//MyDerivedItem.qml 
import QtQuick 2.7 
import QtQuick.Controls 2.0 

MyItem { 
    id: myDerivedItem 

    TextField { 
     placeholderText: "Derived Input Text Here" 
    } 

    TextField { 
     placeholderText: "Derived Input Text Here" 
    } 

    TextField { 
     placeholderText: "Derived Input Text Here" 
    } 

    TextField { 
     placeholderText: "Derived Input Text Here" 
    } 

    //... 
} 

答えて

1

私の問題に対する解決策は、マイナーな変更でした。 をMyItem.qmlに追加すると、次のようになります。

//MyItem.qml 
import QtQuick 2.7 
import QtQuick.Controls 2.0 

FocusScope { 
    id: focusScope 

    default property alias data: column.data 

    Rectangle { 
     id: myItem 

     anchors.fill: parent 
     color: focusScope.focus ? "red" : "green" 

     Column { 
      id: column 
      anchors.fill: parent 

      TextField { 
       placeholderText: "Input Text Here" 
      } 
     } 
    } 
} 
1

hereです。その伝播には、Qt - > QQuickWindow - >フォーカスを置いている項目です。 オブジェクトツリーのトラバースはありませんが、フォーカスは直接発生します。

このルールには例外が1つあります。これは、シーンに向かってフォーカスされたItem、または上位階層のFocusScopeとして機能するFocusScopeです。 基本的に、オブジェクトツリーに加えて、第2のフォーカスツリーがあり、各ノードはFocusScopeで、他のすべてはItemsが葉です。
FocusScope -Nodeには、フォーカスを持つ子が1人いる可能性があります。
オブジェクトツリー内のItemの子は、フォーカスツリー内のオブジェクトの親に兄弟である可能性があります。

関連する問題