Results 1 to 4 of 4

Thread: How to manage Focus with States in my custom QML component?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    1

    Default Re: How to manage Focus with States in my custom QML component?

    Quote Originally Posted by anda_skoa View Post
    Mybe reverse the dependency, i.e. make the focus depend on the state, not the state depend on the focus?

    So events like click, double, escape, change the state and the state changes focus if necessary.

    Cheers,
    _
    Thank you for this advice, I adapted the example as you sugested and now it works!
    I post the code here for those who could be interested in a solution (as I said this is a simplified version of the real code):

    Qt Code:
    1. Item {
    2. id: root
    3. width: 175; height: 25;
    4.  
    5. states: [
    6. State {
    7. name: "noFocus"
    8.  
    9. PropertyChanges {
    10. target: innerComp; enabled: false
    11. }
    12. PropertyChanges {
    13. target: background; visible: false
    14. }
    15. PropertyChanges {
    16. target: manipulator; focus: true
    17. }
    18. },
    19.  
    20. State {
    21. name: "focused"
    22.  
    23. PropertyChanges {
    24. target: innerComp; enabled: false
    25. }
    26. PropertyChanges {
    27. target: background; visible: true
    28. }
    29. PropertyChanges {
    30. target: manipulator; focus: true
    31. }
    32. },
    33. State {
    34. name: "innerFocus"
    35.  
    36. PropertyChanges {
    37. target: innerComp; enabled: true
    38. }
    39. PropertyChanges {
    40. target: background; visible: true
    41. }
    42. PropertyChanges {
    43. target: manipulator; focus: true
    44. }
    45. }
    46. ]
    47. state: "noFocus"
    48.  
    49. //visual area of manipulation (drag, redimension, etc)
    50. MouseArea{
    51. id: manipulator
    52. anchors.fill: parent
    53.  
    54. onPressed: {
    55. root.state = "focused"
    56. forceActiveFocus(manipulator) //this prevents loosing focus in some especific situations
    57. }
    58. onDoubleClicked: {
    59. root.state = "innerFocus"
    60. forceActiveFocus(manipulator) //this prevents loosing focus in some especific situations
    61. }
    62. Keys.onEscapePressed: root.state = "noFocus"
    63.  
    64. }
    65. Rectangle {
    66. id: background
    67. anchors.fill: parent
    68. color: "lightsteelblue";
    69. }
    70. //Inner Component (TextField for example)
    71. InnerComp {
    72. id: innerComp
    73. anchors.fill: parent
    74.  
    75. Keys.onEscapePressed: root.state = "focused"
    76. }
    77. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to manage Focus with States in my custom QML component?

    In case you want to move the "forceActiveFocus" also into the state, then you can do so using http://doc.qt.io/qt-5/qml-qtquick-st...ngescript.html

    Cheers,
    _

Similar Threads

  1. GroupItems in two states
    By Andre008 in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2016, 12:40
  2. Replies: 0
    Last Post: 18th November 2014, 05:39
  3. Replies: 0
    Last Post: 1st February 2012, 18:34
  4. Replies: 1
    Last Post: 30th September 2011, 20:54
  5. Replies: 1
    Last Post: 3rd November 2009, 23:26

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.