我在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 }
}
]
}
答案 0 :(得分:3)
我认为,因为在为Checkbox组件指定的锚点上没有维度,QML引擎不知道如何定位它。 试试这些选项。