【问题标题】:Show the beginning of a long string in a QML TextInput instead of the end在 QML TextInput 中显示长字符串的开头而不是结尾
【发布时间】:2019-04-23 15:23:09
【问题描述】:

我有一个 TextInput QML (Qt 5.12.2) 控件,它包含在一个 Rectangle 项(用于样式)中,我用字符串预先填充了该控件。有时该字符串可能比 TextInput 的最大宽度长。发生这种情况时,文本输入中的文本会显示字符串的结尾,如下所示:

但是,我希望 TextInput 中的文本从字符串的开头显示,如下所示:

这是一个重现此的示例:

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    Rectangle {
        border.width: 1
        border.color: 'black'
        height: childrenRect.height
        width: 600
        clip: true
        TextInput {
            width: parent.width
            text: "1. The quick bown dog jumps over the lazy dog. 2. The quick bown dog jumps over the lazy dog. 3. The quick bown dog jumps over the lazy dog. 4. The quick bown dog jumps over the lazy dog."
        }
    }
}

我尝试将autoScroll 设置为 false,这确实使它以我想要的方式显示,但也禁用了用户滚动文本的能力,如果他们想阅读文本的结尾。我还有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    你可以ensureVisible方法:

    TextInput {
        id: input
        width: parent.width
        text: "1. The quick bown dog jumps over the lazy dog. 2. The quick bown dog jumps over the lazy dog. 3. The quick bown dog jumps over the lazy dog. 4. The quick bown dog jumps over the lazy dog."
        Component.onCompleted: input.ensureVisible(0)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多