Qt 属性中的默认值

Qt 属性中的默认值,qt,qml,qtquick2,Qt,Qml,Qtquick2,我创建了一个自定义元素数据字段。 当我把它放在另一个qml文件的Qt设计器中时,我的道具值为零。如何为自定义属性设置非零值? 我希望:比率、像素大小等默认为非零 //DataField.qml import QtQuick 2.2 import QtQuick.Controls 1.2 import QtQuick.Controls.Styles 1.3 import QtQuick.Window 2.1 Item { id: field width: 100 heig

我创建了一个自定义元素数据字段。 当我把它放在另一个qml文件的Qt设计器中时,我的道具值为零。如何为自定义属性设置非零值? 我希望:比率、像素大小等默认为非零

//DataField.qml
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.3
import QtQuick.Window 2.1

Item {
    id: field
    width: 100
    height: 100
    property bool readOnly: true
    readonly property real ratio: Screen.height / 1200
    property real size: 20
    property real cellRatio: 1.25
    readonly property real pixelSize: field.size * field.ratio
    readonly property real cellHeight: field.size * field.cellRatio


    property real bottom
    property real top
    property int decimals
    property string top_text: "Up"
    property string left_text: "Left"
    property string right_text: "Right"
    property string value_text: "23"


    Label {
        id: top_lbl
        x: -69
        y: -31
        height: Props.cellHeight
        horizontalAlignment: Text.AlignLeft
        font.pixelSize: field.pixelSize
        verticalAlignment: Text.AlignVCenter
        text: field.top_text

    }

    TextField {
        id: textField1
        y: 31
        width: 90
        height: field.cellHeight
        text: field.value_text
        anchors.verticalCenter: left_lbl.verticalCenter
        horizontalAlignment: TextInput.AlignRight
        validator: DoubleValidator {
            decimals: field.decimals
            notation: DoubleValidator.StandardNotation
            top: field.top
            bottom: field.bottom
        }
        font.pixelSize: field.pixelSize
        placeholderText: "0.0"
        anchors.leftMargin: 5
        anchors.left: left_lbl.right
    }

    Label {
        id: right_lbl
        width: 44
        height: field.cellHeight
        text: field.right_text
        anchors.verticalCenter: textField1.verticalCenter
        horizontalAlignment: Text.AlignLeft
        font.pixelSize: field.pixelSize
        anchors.leftMargin: 5
        anchors.margins: 10
        verticalAlignment: Text.AlignVCenter
        anchors.left: textField1.right
    }

    Label {
        id: left_lbl
        width: 57
        height: field.cellHeight
        text: field.left_text
        anchors.top: top_lbl.bottom
        anchors.topMargin: 0
        anchors.left: top_lbl.left
        anchors.leftMargin: 0
        horizontalAlignment: Text.AlignRight
        font.pixelSize: field.pixelSize
        verticalAlignment: Text.AlignVCenter
    }

}

请注意,文档说明-组件完成后,屏幕附加对象在项目或项目派生类型内有效。此外-请注意,屏幕类型在Component.onCompleted处无效,因为此时项目或窗口尚未显示在屏幕上。这可能是问题吗?未初始化的数字属性值为零,例如小数,其他属性可以设置为零,因为Screen.height为零,@skypjack的参数请参阅以了解详细信息。顺便说一句,你可以在没有id的情况下参考内部礼仪,即field.ratio可以是ratio。@BaCaRoZzo我应该把评论放在回答中,这样他就可以结束问题了吗?@skypjack你可以等待OP回来并给出反馈,或者只是回答他的问题。这完全取决于您。不知道为什么,但现在Screen.height在Qt Designer中正常工作=