Hi,
This is my first widget implementation and I have to tweak a few things to accompany touchscreen input. I installed an event filter on an IP form. When the groupbox OR the textEdit field is touched a number pad will show and the selected text is bolded. The number pad will simulate keypress entry events to the lineedit widget. It is working. Can you please review my implementation and provide suggestions?

1) An easier way to find which target is selected. I want this event filter to be generic, so I don't want it to know about the ui.form names. (i.e ui.PortText) Each lineEdit is inside a groupbox.
2) Is there something I am missing? Is this filter to exhaustive?
Thank you.

Qt Code:
  1. bool NetworkConfigurationForm::eventFilter(QObject *target, QEvent *event)
  2. {
  3.  
  4. // if the mouse/ts area is pressed on the group box or edit line then show numpad
  5. // start the lineEdit at the beginning, make sure there is no selected text
  6. QLineEdit *textEdit = qobject_cast<QLineEdit *>(target);
  7. QGroupBox *groupBox = qobject_cast<QGroupBox *>(target);
  8.  
  9. if (event->type() == QEvent::MouseButtonPress)
  10. {
  11. // should only be QlineEdit or GroupBox
  12. // but if more filters are added, this needs to be here
  13. if (textEdit || groupBox)
  14. {
  15. // if groupbox widget was touched ignore if same widget, otherwise
  16. // clear font on old widget, set bold on new widget
  17. if(groupBox)
  18. {
  19. QFont font;
  20. QWidget *parent;
  21.  
  22. qDebug() << groupBox << "GROUPBOX";
  23.  
  24. // if mousepress on the same widget, ignore it
  25. // Otherwise clear font on old item, set on new item.
  26. if(currentTextSelection)
  27. {
  28. if(currentTextSelection->parentWidget() == groupBox)
  29. return true;
  30. parent = currentTextSelection->parentWidget();
  31. font = parent->font();
  32. font.setBold(false);
  33. parent->setFont(font);
  34. }
  35. font = groupBox->font();
  36. font.setBold(true);
  37. groupBox->setFont(font);
  38. if (ui.ServerIPText->parentWidget() == groupBox)
  39. currentTextSelection = ui.ServerIPText;
  40. else if (ui.TerminalIPText->parentWidget() == groupBox)
  41. currentTextSelection = ui.TerminalIPText;
  42. else if (ui.SubnetText->parentWidget() == groupBox)
  43. currentTextSelection = ui.SubnetText;
  44. else if (ui.GatewayText->parentWidget() == groupBox)
  45. currentTextSelection = ui.GatewayText;
  46. else if (ui.PortText->parentWidget() == groupBox)
  47. currentTextSelection = ui.PortText;
  48. }
  49.  
  50. // if textEdit widget was touched ignore if same widget, otherwise
  51. // clear font on old widget, set bold on new widget
  52. if (textEdit)
  53. {
  54. QFont font;
  55. QWidget *parent;
  56. qDebug() << textEdit << "textEdit";
  57. if(currentTextSelection)
  58. {
  59. if (textEdit == currentTextSelection)
  60. return true;
  61. parent = currentTextSelection->parentWidget();
  62. font = parent->font();
  63. font.setBold(false);
  64. parent->setFont(font);
  65. }
  66. parent = textEdit->parentWidget();
  67. font = parent->font();
  68. font.setBold(true);
  69. parent->setFont(font);
  70. currentTextSelection = textEdit; // switch to new widget
  71. }
  72. // Start at the beginning of the text
  73. ui.ServerIPText->home(true);
  74. ui.TerminalIPText->home(true);
  75. ui.SubnetText->home(true);
  76. ui.GatewayText->home(true);
  77. // make sure no text has been selected by mistake
  78. ui.ServerIPText->deselect();
  79. ui.TerminalIPText->deselect();
  80. ui.SubnetText->deselect();
  81. ui.GatewayText->deselect();
  82.  
  83. if(currentTextSelection)
  84. {
  85.  
  86. if(currentTextSelection != ui.ServerIPText && currentTextSelection != ui.PortText)
  87. padEntry->move(400,0);
  88. else
  89. padEntry->move(0,0);
  90. padEntry->show();
  91. }
  92.  
  93. return true;
  94. }
  95. }
  96.  
  97. // this prevents a mouse moving event(dragging) which selects the text
  98. else if (event->type() == QEvent::MouseMove)
  99. return true;
  100.  
  101.  
  102. return false;
  103. }
  104.  
  105. // slot for key entered - from number pad widget //
  106. void NetworkConfigurationForm::keyEntered(QKeyEvent *event)
  107. {
  108. QCoreApplication::sendEvent(currentTextSelection,event);
  109. if(event->key() == Qt::Key_Enter)
  110. {
  111. currentTextSelection->clearFocus();
  112. currentTextSelection = 0;
  113. padEntry->hide();
  114. }
  115. }
To copy to clipboard, switch view to plain text mode