Results 1 to 2 of 2

Thread: Event Filtering on Widgets

  1. #1
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Event Filtering on Widgets

    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 

  2. #2
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Event Filtering on Widgets

    Would using QObject::findChildren() be helpful?

Similar Threads

  1. Upper limit on number of widgets?
    By jdiewald in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2008, 23:00
  2. Replies: 2
    Last Post: 16th May 2008, 14:39
  3. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39
  4. how to call parent widget's event ?
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 12:39
  5. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28

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.