Results 1 to 11 of 11

Thread: ActiveFocus with TextEdit

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default ActiveFocus with TextEdit

    Hi All,

    I have an application which uses QML Treeview. The items of the treeview needs to be editable, hence I have configured a delegate which toggles between text and textedit when user sets a flag.

    Here's the code
    Qt Code:
    1. ApplicationWindow {
    2. id:window
    3. visible: true
    4. width: 640
    5. height: 480
    6. title: qsTr("File System")
    7.  
    8. property var clickedIndex: -1
    9.  
    10. ListModel{
    11. id:listModel
    12. ListElement{
    13. colorName:"red"
    14. }
    15. ListElement{
    16. colorName:"yellow"
    17. }
    18. ListElement{
    19. colorName:"pink"
    20. }
    21. }
    22.  
    23. ListView{
    24. id:listview
    25. height: parent.height
    26. width: 50
    27. model: listModel
    28. anchors.left: parent.left
    29.  
    30. delegate: Component{
    31. Rectangle{
    32. color: colorName
    33. height: 50
    34. width: 50
    35. MouseArea{
    36. anchors.fill: parent
    37. onClicked: {
    38. console.log("Clicked " + colorName)
    39. forceActiveFocus()
    40. }
    41. }
    42. }
    43. }
    44. }
    45.  
    46. TreeView {
    47. id: view
    48. objectName: "view"
    49. anchors.margins: 2 * 12
    50. anchors.left:listview.right
    51. anchors.top:listview.top
    52. anchors.right:parent.right
    53. anchors.bottom:parent.bottom
    54.  
    55. model: treemodel
    56. selectionMode:SelectionMode.ExtendedSelection
    57. headerVisible: true
    58.  
    59. TableViewColumn {
    60. title: "Name"
    61. role: "Title"
    62. width: 300
    63. delegate: Component{
    64. Item {
    65. Text {
    66. id: test
    67. text: model.Title
    68. color: model.IsSelected ? "green" : "blue"
    69. width: parent.width
    70. visible: !model.EditEnabled
    71. }
    72. TextEdit{
    73. id: textField
    74. width: parent.width
    75. visible: model.EditEnabled
    76.  
    77. onVisibleChanged: {
    78. if(visible)
    79. {
    80. textField.text = model.Title
    81. forceActiveFocus()
    82. }
    83.  
    84. onActiveFocusChanged: {
    85. if(!activeFocus)
    86. {
    87. model.EditEnabled = activeFocus
    88. }
    89. }
    90.  
    91. onTextChanged: {
    92. model.Title = textField.text
    93. }
    94. }
    95. }
    96. }
    97. }
    98. }
    99.  
    100. TableViewColumn {
    101. title: rect
    102. role:"IsSelected"
    103. width: 25
    104.  
    105. delegate: Component{
    106. Rectangle{
    107. id: rect
    108. anchors.right: test.right
    109. color: model.IsSelected ? "red" : "teal"
    110. MouseArea{
    111. anchors.fill: parent
    112. onClicked: {
    113. model.EditEnabled = true;
    114. }
    115. }
    116. }
    117. }
    118. }
    119. }
    120. }
    To copy to clipboard, switch view to plain text mode 

    However, I want the textEdit to loose focus when i click any other item displayed in the screen.

    Please provide me some pointers

  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: ActiveFocus with TextEdit

    One thing you could try is to bind the "enabled" property to the "visible" property, so when it becomes non-visible it would also be disabled.

    Another option would be to switch the delegate depending on whether the item is editable or not.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveFocus with TextEdit

    Thanks for the inputs anda_skoa

    The problem I'm facing is that I have so many items in the QML page.
    The treeview has editable item delegate, when I click on any other item i want the activefocus of the textedit to be lost.

    Could you show me how to bind the enabled to visible? I keep getting the binding loop warning for visible property

    Is it possible to do so without setting activeFocus to the other clicked item.

  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: ActiveFocus with TextEdit

    Qt Code:
    1. enabled: visible
    To copy to clipboard, switch view to plain text mode 

    If that doesn't work, use two components and switch between them.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveFocus with TextEdit

    Thanks for the suggestion anda_skoa.

    I understand that the solution suggested would work for items in the treeview.

    However, the application has a treeview and listview of items.

    The treeview has an editable delegate. So when I'm editing an item on the treeview and click an item in the listview, I would like the item which is in edit mode to loose focus.

    The way expressed in the example is setting activeFocus to the listview item. Is there another way to do so without activeFocus?
    Last edited by volcano; 21st June 2016 at 17:36.

  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: ActiveFocus with TextEdit

    So you don't want to deactivate editing only remove the focus?
    Because in your original code removing the focus would also deactivate editing.

    If you want to keep the editable delegate but remove its focus, then I don't understand what the problem is that you are asking for help with.
    Does setting the focus to another component not work?

    Cheers,
    _

  7. #7
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveFocus with TextEdit

    The treeview has an editable delegate. So when I'm editing an item on the treeview and click an item in the listview, I would like the item which is in edit mode to loose focus.
    I meant I would like to change the edit mode to non-edit mode, if a user clicks on any other item on the qml page.

    Let me explain the scenario, I have buttons, a treeview and listview in the same screen. When I enable the item to be edited and start editing the item in the treeview by setting activeFocus, so that the keyboard key strokes are accepted by the textedit.

    Now if I click on some other button on the screen or any item in the listview without setting the activeFocus on the item I just clicked, the activeFocus of the item in the treeview isn't lost, this activeFocus loss is used to set the text and change the edit mode to non-edit mode.

    So currently i'm setting activeFocus explicitly on the item I clicked(i.e. button or item in the listview) other than the treeview item to disable the activeFocus on the treeview item.

    Is there a better way to shift activeFocus from the treeview item rather than configuring activeFocus on any other item which is clicked?

  8. #8
    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: ActiveFocus with TextEdit

    I think I understand better now.

    - when click a button or a ListView entry, you want to stop editing mode on the tree view
    - you currently do that by tracking activeFocus in the tree view delegate

    What about this
    - add a method to your tree model that resets all "edit enabled" values
    - call that method when you want to stop editing
    - ignore anything focus related in QML

    I.e. instead of calling forceActiveFocus() in the list view delegate's mouse area you would then do something like
    Qt Code:
    1. treemodel.stopEditing()
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #9
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveFocus with TextEdit

    Thanks anda_skoa for the suggestions..

    But then for every new item added I have to ensure that the method is called.

    Can you suggest if there exist another way to do so?

  10. #10
    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: ActiveFocus with TextEdit

    Difficult.

    What you could try is a MouseArea that fills the whole window, sits below the tree view but above everything else.
    When it is clicked, it calls the reset method.
    It would also need to set propagateComposedEvents otherwise you can't click on anything below it.

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    volcano (28th June 2016)

  12. #11
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveFocus with TextEdit

    Thanks anda_skoa.

    Will try out your suggestion.

Similar Threads

  1. Qml TextEdit
    By goli in forum Newbie
    Replies: 4
    Last Post: 28th April 2011, 14:37
  2. Table in TextEdit
    By Splatify in forum Newbie
    Replies: 1
    Last Post: 8th March 2011, 18:57
  3. textedit
    By limeir in forum Qt Programming
    Replies: 1
    Last Post: 13th July 2010, 10:27
  4. [B]tableWidget row,col to textEdit[B]
    By briang in forum Newbie
    Replies: 1
    Last Post: 6th November 2009, 04:22
  5. Printing from a textEdit
    By Nefastious in forum Newbie
    Replies: 2
    Last Post: 21st October 2009, 05:29

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.