Reputation: 2502
I am developing QT cross flat form application for Ubuntu and Windows. My Question is simple but as bit new to QT, i am not able to figure it out. I have bellow code in QML. I am using QT TextField
in my qml. When QML is loaded for first time i want id_username
TextField should show cursor by default at initial position (0) which is not happening. When i click same TextField using mouse, cursor is visible at initial position. I am doing focus: true
which is not helpful. Next, i added cursorVisible: true
with that cursor is visible when qml is loaded, but i am not able to type anything(Input anything) till i click on the TextField
.
TextField
{
id: id_username
objectName: "id_username"
placeholderText: qsTr("username")
color: "#000000"
font.pixelSize: 20
cursorPosition: 0
selectByMouse: true
anchors.horizontalCenter: parent.horizontalCenter
focus: true
background: Rectangle
{
anchors.horizontalCenter: parent.horizontalCenter
implicitWidth: 200
border.color: id_username.focus ? "#000000": "#36404e"
border.width: id_username.focus ? 2: 1
radius: id_instruction.height / 4
}
}
Please let me know what i am doing wrong. Is there any onLoad function from where can call forceActiveFocus()
Upvotes: 2
Views: 2613
Reputation: 57
try this in TextField
:
onVisibleChanged: if(visible) id_username.forceActiveFocus()
Reference link - http://www.qtcentre.org/threads/62655-How-to-gain-focus-onto-TextEdit-in-QML
Upvotes: 1