【问题标题】:How to update QML ScrollView navigation when navigating child QML TextEdit with up and down keys使用向上和向下键导航子 QML TextEdit 时如何更新 QML ScrollView 导航
【发布时间】:2016-11-04 21:12:01
【问题描述】:

我在ScrollView 中有一个TextEdit。当用户按下向上或向下箭头键并将文本移动到ScrollView 边界之外时,如何实现逻辑以使ScrollView 移动?

//qml
ScrollView {
    id: palGenTextScrollView
    property int scrollBarWidth: 15
    anchors.fill: parent

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onWheel: {
            if (wheel.modifiers & Qt.ControlModifier){
                if (wheel.angleDelta.y > 0)
                {
                    mainTextEdit.font.pixelSize++
                }
                else
                {
                    mainTextEdit.font.pixelSize--
                }
            }
            else{
                wheel.accepted=false
            }
        }
    }
    TextEdit {
        id: mainTextEdit
        text: fileio.palFileText 
        font.family: "Courier"
        wrapMode: TextEdit.Wrap
        selectByMouse: true
        //when going out of upward bounds: palGenTextScrollView.flickableItem.contentY--
        //when going out of downward bounds: palGenTextScrollView.flickableItem.contentY++
    }
}

【问题讨论】:

    标签: qt qml qtquick2 qtquickcontrols


    【解决方案1】:

    您可以使用TextArea,它完全适合您。如果您想自己烘焙,请查看详细说明中 TextEdit 的文档中的 flickable 示例。

    请注意,TextEdit 不实现滚动、跟随光标或其他特定于外观的行为。例如,添加跟随光标的轻弹滚动:

        Flickable {
            id: flick
    
            width: 300; height: 200;
            contentWidth: edit.paintedWidth
            contentHeight: edit.paintedHeight
            clip: true
    
            function ensureVisible(r)
            {
                if (contentX >= r.x)
                    contentX = r.x;
                else if (contentX+width <= r.x+r.width)
                    contentX = r.x+r.width-width;
                if (contentY >= r.y)
                    contentY = r.y;
                else if (contentY+height <= r.y+r.height)
                    contentY = r.y+r.height-height;
            }
    
            TextEdit {
                id: edit
                width: flick.width
                height: flick.height
                focus: true
                wrapMode: TextEdit.Wrap
                onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2016-08-21
      相关资源
      最近更新 更多