首页 文章

如果行中的某个项目不可见,如何将其展开?

提问于
浏览
0

根据文件

如果行中的某个项目不可见,或者它的宽度或高度为0,则该项目将不会布局,并且在该行中将不可见 . 此外,由于Row自动将其子项水平放置,因此行内的子项不应使用left,right,anchors.horizontalCenter,fill或centerIn anchors设置其x位置或水平锚定自身 . 如果需要执行这些操作,请考虑在不使用行的情况下定位项目 .

我无法找到最佳解决方案 . 在这方面有人能帮助我吗?看下面的代码:

Rectangle{
    width: parent.width
    height: parent.height
    Component{
        id:itemDelegate
        Item {
            id: itemId
            width: listView.width; height: listView.height * 0.20
            Row{
                spacing : listView.height * 0.20
                Repeater{
                    model:5
                    Column{
                        Button{
                            text: index
                            //opacity: (index % 2) === 0 ? 1.0:0.0
                            visible: (index % 2) === 0 ? true:false
                        }
                        Button{
                            text: index
                            //opacity: (index % 3) === 0 ? 1.0:0.0
                            visible: (index % 3) === 0 ? true:false
                        }
                    }
                }
            }
        }
    }

    ListView{
        id:listView
        anchors.fill: parent
        anchors {
            left: parent.left; top: parent.top;
            right: parent.right
            margins: parent.width * 0.08
        }
        delegate: itemDelegate
        model:aaModel
        clip:true
    }
}

在输出中,第一个索引列将是不可见的 . 因此它不会被布局,第二个索引列将占用该空间 . 但我想要那里的空白 . 但是,我可以在第三个索引列(第一个空格后跟一个按钮)中以空白空间为代价使用不透明度 . 但是如果按钮在侧面不可见,如果Column中的所有按钮都不可见,那么我希望这样的行为可以在列中提升按钮,空白区域应该出现在Column的位置 . 所以这里的行内的列不会帮助我 . 做这个的最好方式是什么?

1 回答

  • 0

    Item 中包含按钮

    Column {
        Button {
            text: 'Blue'
        }
        Item {
            width: greenButton.implicitWidth
            height: greenButton.implicitHeight
            Button {
                id: greenButton
                visible: false
                text: 'Green'
            }
        }
        Button {
            text: 'red'
        }
    }
    

    为了更容易,为它创建一个自定义组件(例如MyButton.qml):

    import QtQuick 2.0
    import QtQuick.Controls 2.0
    
    Item {
        id: root
        property alias button: myButton
        implicitHeight: button.height
        implicitWidth: button.width
        Button {
            id: myButton
        }
    }
    

    像这样使用它:

    Column {
        MyButton {
            button {
                text: 'green'
                onClicked: button.visible = false // Won't shrink the column
            }
        }
        MyButton {
            button {
                text: 'red'
                visible: false
            }
        }
        MyButton {
            button {
                text: 'orange'
                onClicked: visible = false // Will shrink the column
            }
        }
    }
    

相关问题