Qt QML 锚点问题

发布于 2024-11-09 19:01:41 字数 1528 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-11-16 19:01:41

我认为因为没有为您的 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.

  • Specify Dimensions(height,Width) or anchors to the Checkbox component
  • Use a 'Column' or 'Row' Item so you don't have to micro manage layouts.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文