【问题标题】:Simple TextArea Element in qmlqml 中的简单 TextArea 元素
【发布时间】:2013-03-11 15:07:26
【问题描述】:

我想在 QML 中创建简单的 TextArea 元素并尝试使用此代码。但是在 Textarea 中写入时,文本会超出边界。

你有简单的 TextArea 还是可以帮我改进这段代码:

FocusScope {
    id: focusScope
    width: 400; height: 50
property int fontSize: focusScope.height -30
property color textColor: "black"
property string placeHolder: "Type something..."
property string inputExpression: ".*"
property bool isUserInTheMiddleOfEntringText: false

Rectangle {
    width: parent.width
    height: parent.height
    border.color: 'steelblue'
    color: focus?'red':'#AAAAAA'
    border.width: 3
    radius: 0
    MouseArea {
        anchors.fill: parent
        onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
        }
    }
}

Text {
    id: typeSomething
    anchors.fill: parent; anchors.rightMargin: 8
    verticalAlignment: Text.AlignVCenter
    text: placeHolder
    color: "gray"
    font.italic: true
    font.pointSize: fontSize
    MouseArea {
        anchors.fill: parent
        onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
        }
    }

}

MouseArea {
    anchors.fill: parent
    onClicked: { focusScope.focus = true; textInput.openSoftwareInputPanel();
    }
}

TextEdit {
    id: textInput
    anchors { right: parent.right; rightMargin: 8; left: clear.left; leftMargin: 8; verticalCenter: parent.verticalCenter }
    focus: true
    selectByMouse: true
    font.pointSize: fontSize
    wrapMode: TextEdit.WordWrap
    color: textColor

}


Text {
    id: clear
    text: "\u2717" // 'x'//"\u2713"
    color: 'steelblue'
    font.pointSize: 25
    opacity: 0
    anchors { left: parent.left; leftMargin: 8; verticalCenter: parent.verticalCenter }
    MouseArea {
        anchors.fill: parent
        onClicked: { textInput.text = ''; focusScope.focus = true; textInput.openSoftwareInputPanel(); }
    }
}
states: State {
        name: "hasText"; when: textInput.text != ''
        PropertyChanges { target: typeSomething; opacity: 0 }
        PropertyChanges { target: clear; opacity: 1 }
    }
transitions: [
    Transition {
        from: ""; to: "hasText"
        NumberAnimation { exclude: typeSomething; properties: "opacity" }
    },
    Transition {
        from: "hasText"; to: ""
        NumberAnimation { properties: "opacity" }
    }
]
}

谢谢你的帮助

【问题讨论】:

  • 我试试这段代码,不行
  • :) 我认为现在是正确的
  • 你想要什么行为?文本上的一个很好的省略/剪辑?或者您的组件必须扩展以显示所有文本?

标签: qt textarea qml


【解决方案1】:

这段代码应该做你想做的:

import QtQuick 2.0

FocusScope {
    id: focusScope;
    width: 400;
    height: textInput.paintedHeight + (2 * textInput.anchors.topMargin);

    property alias  value                          : textInput.text;
    property alias  fontSize                       : textInput.font.pointSize;
    property alias  textColor                      : textInput.color;
    property alias  placeHolder                    : typeSomething.text;

    Rectangle {
        id: background;
        anchors.fill: parent;
        color: "#AAAAAA";
        radius: 5;
        antialiasing: true;
        border {
            width: 3;
            color: (focusScope.activeFocus ? "red" : "steelblue");
        }
    }
    TextEdit {
        id: textInput;
        focus: true
        selectByMouse: true
        font.pointSize: 20;
        wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere;
        color: "black";
        anchors {
            top: parent.top;
            topMargin: 10;
            left: parent.left;
            leftMargin: 10;
            right: parent.right;
            rightMargin: 10;
        }
    }
    Text {
        id: typeSomething;
        text: "Type something...";
        color: "gray";
        opacity: (value === "" ? 1.0 : 0.0);
        font {
            italic: true
            pointSize: fontSize;
        }
        anchors {
            left: parent.left;
            right: parent.right;
            leftMargin: 10;
            rightMargin: 10;
            verticalCenter: parent.verticalCenter;
        }

        Behavior on opacity { NumberAnimation { duration: 250; } }
    }
    MouseArea {
        visible: (!focusScope.activeFocus);
        anchors.fill: parent
        onClicked: { textInput.forceActiveFocus(); }
    }
    Text {
        id: clear;
        text: "\u2717" // 'x' //"\u2713"
        color: 'steelblue';
        font.pixelSize: 30;
        opacity: (value !== "" ? 1.0 : 0.0);
        anchors {
            right: parent.right;
            bottom: parent.bottom;
            margins: 5;
        }

        Behavior on opacity { NumberAnimation { duration: 250; } }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                value = "";
                focusScope.focus = false;
            }
        }
    }
}

我还修复了您代码中的许多问题,例如多个 MouseArea 用于相同的用途、绑定循环、错误的锚点/对齐等...

【讨论】:

  • 这是一个很好的答案,提供了很多关于 QML 工作原理的细节。谢谢!
  • 我有一个类似的问题和用例,但有点不同,这个问题发布在stackoverflow.com/questions/27088152/…,你们中的哪位可以帮我一些建议。
猜你喜欢
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2019-11-15
相关资源
最近更新 更多