Results 1 to 7 of 7

Thread: How to gain focus onto TextEdit in QML

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default How to gain focus onto TextEdit in QML

    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
    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id: notesrect
    5. width: 250
    6. height: 175
    7. border.width: 2
    8. border.color: "#B1B0A8"
    9. color: "transparent"
    10.  
    11. property alias text: searchtext.text
    12.  
    13. Rectangle {
    14. id: notesrect1
    15. width: parent.width
    16. anchors.top: parent.top
    17. height: 40
    18. color: "#F4F4F4"
    19.  
    20. Image {
    21. id: img2
    22. anchors.top: parent.top
    23. anchors.horizontalCenter: parent.horizontalCenter
    24. anchors.topMargin: 2
    25. source: "images/hideicons.png"
    26.  
    27. MouseArea {
    28. anchors.fill: parent
    29. onClicked:
    30. {
    31. if(searchtext.text.length > 0)
    32. {
    33. rw.setannottext(searchtext.text)
    34. browserwindow.storeannotations()
    35. }
    36. searchtext.text = ""
    37. notesrect.visible = false
    38.  
    39. }
    40. }
    41. }
    42.  
    43. Text {
    44. id:txt1
    45. text: qsTr("Take Note")
    46. font.pointSize: 9
    47. font.family: "san-serif"
    48. anchors.bottom: parent.bottom
    49. anchors.horizontalCenter: parent.horizontalCenter
    50. anchors.bottomMargin: 2
    51. color: "black"
    52. }
    53.  
    54. }
    55.  
    56. Rectangle {
    57. id: notesrect2
    58. width: parent.width
    59. height: parent.height -40
    60. anchors.top: notesrect1.bottom
    61. color: "#FFFCD0"
    62. clip: true
    63.  
    64. /* Flickable {
    65.   id: flickArea
    66.   anchors.fill: parent
    67.   contentWidth: searchtext.width; contentHeight: searchtext.height
    68.   flickableDirection: Flickable.VerticalFlick
    69.   clip: true */
    70.  
    71. TextEdit {
    72. id: searchtext
    73. anchors.fill: parent
    74. focus: true
    75. cursorVisible: true
    76. color: "black"
    77. clip:true
    78. textFormat: TextEdit.AutoText
    79. wrapMode: TextEdit.Wrap
    80. Keys.onPressed: TextEdit.forceActiveFocus()
    81.  
    82. Keys.onReturnPressed:
    83. {
    84. if(searchtext.text.length > 0)
    85. {
    86. rw.setannottext(searchtext.text)
    87. browserwindow.storeannotations()
    88. }
    89. }
    90. }
    91. }
    92. }
    93. in browserwindow.qml
    94. I will set this Notes.qml to visible
    95. [code]
    96. function newnotes()
    97. {
    98. mypoint = graphicswebview.getscreenpos()
    99. notesbox.x = mypoint.x - notesbox.width/2
    100. notesbox.y = mypoint.y - 36
    101. notesbox.visible = true
    102. hlbox.visible = false
    103. }
    To copy to clipboard, switch view to plain text mode 
    [/code]

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to gain focus onto TextEdit in QML

    You are trying to call forceActiveFocus() in an event handler that is only triggered if the component has keyboard focus.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How to gain focus onto TextEdit in QML

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to gain focus onto TextEdit in QML

    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,
    _

  5. #5
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How to gain focus onto TextEdit in QML

    in ReaderView.qml
    Qt Code:
    1. Notes {
    2. id:notesbox
    3. visible: false
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    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()

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to gain focus onto TextEdit in QML

    But you can do this inside Notes.qml

    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle {
    4. id: notesrect
    5.  
    6. onVisibleChanged: if (visible) searchtext.forceActiveFocus()
    7.  
    8. // ....
    9. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How to gain focus onto TextEdit in QML

    Thanks anda_skoa
    It worked

Similar Threads

  1. Replies: 3
    Last Post: 18th July 2013, 05:12
  2. QMessageBox on Linux fails to gain focus
    By napajejenunedk0 in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2011, 14:11
  3. Qml TextEdit
    By goli in forum Newbie
    Replies: 4
    Last Post: 28th April 2011, 15:37
  4. Replies: 15
    Last Post: 26th July 2007, 22:04
  5. Focus issues / Setting multiple focus
    By ComputerPhreak in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2007, 07:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.