KeyboardHelper.qml 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. // Helper component to ensure virtual keyboard works properly
  4. Item {
  5. id: keyboardHelper
  6. // Force show keyboard for a specific TextField
  7. function showKeyboardFor(textField) {
  8. textField.forceActiveFocus()
  9. Qt.inputMethod.show()
  10. }
  11. // Hide keyboard
  12. function hideKeyboard() {
  13. Qt.inputMethod.hide()
  14. }
  15. // Enhanced TextField with proper keyboard support
  16. component EnhancedTextField: TextField {
  17. activeFocusOnPress: true
  18. selectByMouse: true
  19. // Default input hints
  20. inputMethodHints: Qt.ImhNone
  21. MouseArea {
  22. anchors.fill: parent
  23. onPressed: {
  24. parent.forceActiveFocus()
  25. Qt.inputMethod.show()
  26. mouse.accepted = false
  27. }
  28. }
  29. onActiveFocusChanged: {
  30. if (activeFocus) {
  31. Qt.inputMethod.show()
  32. }
  33. }
  34. Keys.onReturnPressed: {
  35. Qt.inputMethod.hide()
  36. focus = false
  37. }
  38. Keys.onEscapePressed: {
  39. Qt.inputMethod.hide()
  40. focus = false
  41. }
  42. }
  43. // Numeric-only TextField
  44. component NumericTextField: EnhancedTextField {
  45. inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText
  46. validator: IntValidator { bottom: 0; top: 9999 }
  47. // Only allow numeric input
  48. onTextChanged: {
  49. var numeric = text.replace(/[^0-9]/g, '')
  50. if (text !== numeric) {
  51. text = numeric
  52. }
  53. }
  54. }
  55. }