PDA

View Full Version : How to gain focus onto TextEdit in QML



ejoshva
28th May 2015, 07:49
how to set focus onto the textedit.
I checked the docs and tried with "focus : true" and forceActiveFocus() but in vain

The code is as below.
Notes.qml


import QtQuick 1.1

Rectangle {
id: notesrect
width: 250
height: 175
border.width: 2
border.color: "#B1B0A8"
color: "transparent"

property alias text: searchtext.text

Rectangle {
id: notesrect1
width: parent.width
anchors.top: parent.top
height: 40
color: "#F4F4F4"

Image {
id: img2
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 2
source: "images/hideicons.png"

MouseArea {
anchors.fill: parent
onClicked:
{
if(searchtext.text.length > 0)
{
rw.setannottext(searchtext.text)
browserwindow.storeannotations()
}
searchtext.text = ""
notesrect.visible = false

}
}
}

Text {
id:txt1
text: qsTr("Take Note")
font.pointSize: 9
font.family: "san-serif"
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottomMargin: 2
color: "black"
}

}

Rectangle {
id: notesrect2
width: parent.width
height: parent.height -40
anchors.top: notesrect1.bottom
color: "#FFFCD0"
clip: true

/* Flickable {
id: flickArea
anchors.fill: parent
contentWidth: searchtext.width; contentHeight: searchtext.height
flickableDirection: Flickable.VerticalFlick
clip: true */

TextEdit {
id: searchtext
anchors.fill: parent
focus: true
cursorVisible: true
color: "black"
clip:true
textFormat: TextEdit.AutoText
wrapMode: TextEdit.Wrap
Keys.onPressed: TextEdit.forceActiveFocus()

Keys.onReturnPressed:
{
if(searchtext.text.length > 0)
{
rw.setannottext(searchtext.text)
browserwindow.storeannotations()
}
}
}
}
}
in browserwindow.qml
I will set this Notes.qml to visible


function newnotes()
{
mypoint = graphicswebview.getscreenpos()
notesbox.x = mypoint.x - notesbox.width/2
notesbox.y = mypoint.y - 36
notesbox.visible = true
hlbox.visible = false
}

anda_skoa
28th May 2015, 08:07
You are trying to call forceActiveFocus() in an event handler that is only triggered if the component has keyboard focus.

Cheers,
_

ejoshva
28th May 2015, 08:10
oh ok.
I want the keyboard focus into the textedit meaning, once the textedit(notesbox) is made visible and I want to start typing into the textedit instead of manually bringing focus onto the textedit by mouse press.

anda_skoa
28th May 2015, 09:00
Hmm, I didn't see any item with id notesbox but if the visibility change is your intended trigger, then call searchtext.forceActiveFocus() in the onVisibleChanged handler of that item.

Cheers,
_

ejoshva
28th May 2015, 10:50
in ReaderView.qml



Notes {
id:notesbox
visible: false

}


since, TextEdit is the child of child in notesbox. I am not able to directly call forceActiveFocus().
Getting error as "TypeError: Result of expression 'notesrect.notesrect2' [undefined] is not an object.

in BrowserWindow.qml
calling as notesrect.notesrect2.searchtext.forceActiveFocus()

anda_skoa
28th May 2015, 11:01
But you can do this inside Notes.qml



import QtQuick 1.1

Rectangle {
id: notesrect

onVisibleChanged: if (visible) searchtext.forceActiveFocus()

// ....
}


Cheers,
_

ejoshva
28th May 2015, 11:56
Thanks anda_skoa
It worked :)