Qt QML 锚点问题
我在 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 }
}
]
}
i've a problem with anchors in QML file,
this code is not working, the anchors.left is not apply to the text, the text stay in the checkbox :
Checkbox{
objectName: "chkRemenber"
id: chkRemenber
}
Text {
id: labRemenber
text: "REMENBER"
anchors.left: chkRemenber.right
}
But if i don't use my own component,
but an image, it's working, the text is on the left of 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
}
this is the code of my checkbox :
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 }
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为因为没有为您的 Checkbox 组件指定的锚点尺寸,QML 引擎不知道如何定位它。
尝试这些选项。
I think that because there are no dimensions on anchors specified for your Checkbox component the QML Engine doesn't know how to position it.
Try these options.