2011-05-27 52 views
1

我有一個問題,在QML文件錨, 這個代碼不工作時,anchors.left並不適用於文本,該文本留在複選框:Qt的QML錨問題

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 } 
    } 
] 
} 

回答

3

我認爲,因爲您的Checkbox組件沒有指定錨點的尺寸,QML引擎不知道如何定位它。 試試這些選項。

  • 指定尺寸(高度,寬度)或CheckBox組件
  • 使用「列」或「行」項目錨,所以你不必微觀管理佈局做。
+0

我已經添加了寬度:30高度:30,它的工作。感謝 – NicoMinsk 2011-05-27 14:18:25

+2

如果你想進一步改進代碼,使用height:parent.height(和寬度)在圖像中(或其他方式)或者甚至使用圖像作爲您的複選框的根。矩形不是真的需要。 – blakharaz 2011-05-27 16:04:23