qt - 平均共享 QML 行中的水平空间

我需要在我的行中的所有“按钮”之间平均共享水平空间。 我将此代码与 Repeater 一起使用。

Component {
    id: buttonComponent
    Rectangle {
        height: buttonRow.height
        width: buttonRow.width / buttonsRepeater.count
        color:  "#FFDDDD"
        Text {
            anchors.centerIn: parent
            text: model.text
        }
    }
}

Rectangle {
    color: "#DDDDDD"
    id: buttonBar
    height: 30
    anchors {
        bottom: parent.bottom
        left: parent.left
        right: parent.right
    }

    Row {
        id: buttonRow
        anchors.fill: parent
        Repeater {
            id: buttonsRepeater
            model: buttonsModel
            delegate: buttonComponent
        }
    }
}

现在,我想计算 Row 的理想宽度,以便我所有的按钮文本都能正确显示。 我怎样才能得到这个理想的宽度?

最佳答案

如果你不想使用 QtQuick.Layouts 因为它们还没有真正准备好,你可以使用这个:

Rectangle {
    id: buttonBar;
    color: "#DDDDDD";
    height: 30;
    width: (buttonColumn.width + 20 + buttonRow.spacing) * buttonsRepeater.count;
    anchors {
        bottom: parent.bottom;
        left: parent.left;
    }

    Column {
        id: buttonColumn;
        visible: false;

        Repeater {
            model: buttonsModel;
            delegate: Text {
                text: model.text;
            }
        }
    }
    Row {
        id: buttonRow;
        anchors.fill: parent;

        property real itemWidth : ((width + spacing) / buttonsRepeater.count) - spacing;

        Repeater {
            id: buttonsRepeater;
            model: buttonsModel;
            delegate: Component {
                id: buttonDelegate;

                Rectangle {
                    height: parent.height;
                    width: parent.itemWidth;
                    color: "#FFDDDD";
                    border.width: 1;

                    Text {
                        anchors.centerIn: parent;
                        text: model.text;
                    }
                }
            }
        }
    }
}

我只是使用了一个隐藏的 Column 来轻松计算文本元素的最大宽度,并在条形宽度中添加了一点填充以避免无间距文本。

https://stackoverflow.com/questions/16584097/

相关文章:

scala - 没有用于 future 的守护线程的执行上下文

crystal-reports - 具有内置最终用户报告设计器的报告系统

xcode - 如何在 Xcode 和其他文件中同时复制 .h 和 .m 文件?

autoconf - Automake 要求 "Autoconf 2.65 or better"但我

events - Kendo multiselect,当一个项目被移除时触发一个事件

python - recarray 中的 numpy datetime64

.net - 无法在 PowerShell 中创建 .Net 对象

chef-infra - 是否有相当于 Berkshelf 的产品,但用于 Puppet 模块?

vba - 如何将 Microsoft Word 中当前选定段落的段落编号获取到 AppleScri

.net - 可区分联合中的函数约束泛型参数的类型