1

QML ファイルのアンカーに問題があります。このコードは機能していません。anchors.left がテキストに適用されず、テキストがチェックボックスにとどまります。

Checkbox{
    objectName: "chkRemenber"
    id: chkRemenber
}
Text {
    id: labRemenber
    text: "REMENBER"
    anchors.left: chkRemenber.right
}

ただし、独自のコンポーネントを使用せずに画像を使用すると、機能します。テキストは chkRemenber2 の左側にあります。

Image {
    id: chkRemenber2
    width: 30
    height: 30
    source: "../checkbox_on.png"
    fillMode: Image.PreserveAspectFit;
    MouseArea {
        anchors.fill: parent
        onClicked: toggle()
    }
}
Text {
    id: labRemenber2
    text: "REMENBER"
     anchors.left: chkRemenber2.right
}

これは私のチェックボックスのコードです:

import QtQuick 1.0

Rectangle {
id: container
property bool pressed: false
property string src: "../checkbox_off.png"

function toggle (){
    if (container.state == "on")
        container.state = "off";
    else
        container.state = "on";
    console.log("CLICK ! " + container.state);
}

Image {
    id: checkBoxImg
    width: 30
    height: 30
    source: src
    fillMode: Image.PreserveAspectFit;

    MouseArea {
        anchors.fill: parent
        onClicked: toggle()
    }
}
states: [
    State {
        name: "on"
        PropertyChanges { target: checkBoxImg; source: "../checkbox_on.png" }
        PropertyChanges { target: container; pressed: true }

    },
    State {
        name: "off"
        PropertyChanges { target: checkBoxImg; source: "../checkbox_off.png" }
        PropertyChanges { target: container; pressed: false }
    }
]
}
4

1 に答える 1

3

Checkbox コンポーネントに指定されたアンカーにディメンションがないため、QML エンジンはそれを配置する方法を認識していないと思います。これらのオプションを試してください。

  • 寸法 (高さ、幅) またはチェックボックス コンポーネントへのアンカーを指定します。
  • 「列」または「行」アイテムを使用すると、レイアウトを細かく管理する必要がなくなります。
于 2011-05-27T14:02:59.627 に答える