Results 1 to 2 of 2

Thread: QML TextInput never loses focus

  1. #1
    Join Date
    Aug 2017
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QML TextInput never loses focus

    Hello everyone. I'm writing my own qml object for textinput. What I'have done so far is:

    Qt Code:
    1. //Input.qml file
    2. import QtQuick 2.8
    3. import QtGraphicalEffects 1.0
    4. Item {
    5. id:root
    6.  
    7. property color shadowColor:"#b7b7b7"
    8. property color backgroundColor:"#eaeaea"
    9. property int shadowRadius:4
    10. property alias text:yazı.text
    11. property bool shadowless:false
    12. property alias radius:arkaplan.radius
    13. property bool icon : simge.source
    14.  
    15. Rectangle{
    16. id:arkaplan
    17. anchors.fill: parent
    18. radius:2
    19. color:backgroundColor
    20. border.color: backgroundColor
    21. layer.enabled: true
    22. layer.effect: DropShadow{
    23. radius: if(!shadowless) radius=shadowRadius; else radius=0;
    24. samples: 9
    25. transparentBorder: true
    26. color: shadowColor
    27. verticalOffset: 0
    28. horizontalOffset: 0
    29. }
    30. }
    31.  
    32. TextInput{
    33. anchors.fill: parent
    34. id:text
    35. verticalAlignment: Qt.AlignVCenter
    36. padding: 8
    37. font.bold: true
    38. onFocusChanged: console.log("focus changed to: "+focus)
    39.  
    40. }
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 

    but, when I once focus the TextInput, no matter what I do (clicking outside the element, hitting esc key) I can't lose the focus. If I put two TextInput, then I can lose one's focus by clicking the other.
    I have just found a weird way to lose the focus:
    Qt Code:
    1. import QtQuick 2.8
    2. import QtQuick.Window 2.2
    3. import "qrc:/UI"
    4. Window {
    5. visible: true
    6. width: 640
    7. height: 480
    8. title: qsTr("Hello World")
    9. FocusScope{
    10. id:fakeFocus
    11. }
    12.  
    13. MouseArea{
    14. anchors.fill: parent
    15. onPressed: fakeFocus.forceActiveFocus()
    16.  
    17. }
    18. Input{
    19. width:128
    20. height: 32
    21. x:64
    22. y:23
    23. radius: 0
    24. }
    25. Input{
    26. width:128
    27. height: 32
    28. x:64
    29. y:64
    30. radius: 0
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    this way, I can lose the focus by clicking outside. But it seemed so dirty way of doing this. Is there any other method?
    Thanks,

  2. The following user says thank you to Non for this useful post:

    zeFree (18th September 2018)

  3. #2
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QML TextInput never loses focus

    I also wanted to achieve the same thing and had to do pretty much the same thing.
    I added this mouse-area as the bottom-most item in the Window.
    Qt Code:
    1. Window {
    2. id: rootWindow
    3.  
    4. // set window's proerties, etc.
    5.  
    6. MouseArea {
    7. anchors.fill: parent
    8.  
    9. onClicked: {
    10. console.log("base mouse area pressed")
    11. focus = true
    12. }
    13. }
    14.  
    15. // everything else goes here onward
    16. }
    To copy to clipboard, switch view to plain text mode 
    This does pretty much the same thing, just that we don't have to create an extra FocusScope this way.

Similar Threads

  1. Replies: 1
    Last Post: 7th May 2015, 21:58
  2. Replies: 1
    Last Post: 31st July 2014, 09:55
  3. Replies: 3
    Last Post: 29th July 2014, 19:18
  4. Closing QCompleter when QLineEdit loses focus
    By mathieuS in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2011, 19:14
  5. QWinwidget loses focus
    By salix in forum Qt Programming
    Replies: 0
    Last Post: 16th June 2009, 22:54

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.