代码之家  ›  专栏  ›  技术社区  ›  ymoreau

如何在没有固定大小的布局中删除文本项?

  •  0
  • ymoreau  · 技术社区  · 7 年前

    根据文件 Text.elide

    只有设置了显式宽度,文本才会省略。

    但是,如果我希望我的文本项占用版面中的所有可用空间并删除不适合的内容,该怎么办?

    例如,具有固定宽度项目(图标、按钮…)和某些文本的布局:

    RowLayout
    {
        id: _layout
        anchors.fill: parent
    
        Rectangle {
            color: "lightblue"
            height: 20
            width: 20
        }
        Text {
            text: "long words long words long words long words long words long words long words long words long words long words "
            elide: Text.ElideRight
        }
        Rectangle {
            color: "lightblue"
            height: 20
            width: 20
        }
    }
    

    如果窗口/容器太小,此代码将被截断,并在文本后隐藏项目(也不会显示 ... 就像一个省略的文本)。

    1 回复  |  直到 7 年前
        1
  •  2
  •   derM - not here for BOT dreams    7 年前

    必须指定布局应如何限制宽度,例如:

    Text {
        text: "long words long words long words long words long words long words long words long words long words long words "
        elide: Text.ElideRight
    
        Layout.fillWidth: true
    }
    

    见: http://doc.qt.io/qt-5/qml-qtquick-layouts-layout.html#fillWidth-attached-prop