QML:更改文本字段中的光标颜色

时间:2019-11-05 21:33:51

标签: qt qml qt5 qtquick2 qtquickcontrols2

如何更改QML TextField元素中的光标颜色和宽度?假设我们有以下一个:

import QtQuick 2.12
import QtQuick.Controls 2.12

TextField {
    id: control
    placeholderText: qsTr("Enter description")

    background: Rectangle {
        implicitWidth: 200
        implicitHeight: 40
        color: control.enabled ? "transparent" : "#353637"
        border.color: control.enabled ? "#21be2b" : "transparent"
    }
}

如何使光标变为绿色,蓝色或其他颜色?谢谢!

1 个答案:

答案 0 :(得分:2)

由于cursorDelegate继承自TextField,因此您必须通过TextInput将Rectangle设置为想要的颜色作为光标。

import QtQuick 2.12
import QtQuick.Controls 2.12

TextField {
    id: control
    placeholderText: qsTr("Enter description")
    cursorDelegate: Rectangle {
        visible: control.cursorVisible
        color: "salmon"
        width: control.cursorRectangle.width
    }
    background: Rectangle {
        implicitWidth: 200
        implicitHeight: 40
        color: control.enabled ? "transparent" : "#353637"
        border.color: control.enabled ? "#21be2b" : "transparent"
    }
}