Grid删除Item中的QML可见属性

时间:2015-02-25 16:06:44

标签: qt qt4 qml

我试图在Qt4.8上使用以下代码创建QML键盘。

Item {
    id: keyboard
    property string keys: "azertyuiopqsdfghjklmwxcvbn****^<"

    Rectangle {
        height: parent.height
        width: parent.width

        Grid {
            id: keyboardGrid

            rows: 4
            columns: 10
            spacing: 1

            Repeater {
                model: keys.length

                KeyboardButton {
                     visible: { (keys.charAt(index) == "*") ? false : true; }
                    btnKeyText: keys.charAt(index);                    
                }
            }
        }
    }
}

我已经把一些&#39; *&#39;在键中为了使一些不可见的按钮转到网格的下一行,但是当我将KeyboardButton设置为visible = false时,QML解释器只是忽略它。

请参阅屏幕截图以获取更多详细信息,第一个是使用此代码,第二个是当我对可见设置为false的行进行注释时。

Invisible * Button

Visible * Button

为什么看不见的隐藏组件?任何技巧?

1 个答案:

答案 0 :(得分:1)

正如BaCaRoZzo所说,元素opacity: 0visibility: 0元素未呈现(在Qt4.8中,在5中,而上级opacity : 0不会影响渲染),所以我找到了另一种做我想做的事。

我通过使用RepeaterRow创建我自己的网格来实现此目的:

Item {
    id: keyboard
    property variant keys: ["azertyuiop", "qsdfghjklm", "wxcvbn,;:!", "⇧* ↵←"]

    Repeater{
        id: lineRpt
        model: 4

        anchors.fill: parent

        Row {
            spacing: 1

            anchors.verticalCenter: parent.top
            anchors.verticalCenterOffset: 25+(index*52)

            anchors.left: parent.left

            property string currentLine: keys[index]

            Repeater {
                model: keys.length

                KeyboardButton {
                     visible: { (keys.charAt(index) == "*") ? false : true; }
                    btnKeyText: keys.charAt(index);                    
                }
            }
        }
    }
}

评论后修改:

你也可以将背景颜色设置为透明,在我的情况下,我也需要删除文本的de“*”。

相关问题