Results 1 to 6 of 6

Thread: Howto handle QLineEdit::focusInEvent() from container?

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Howto handle QLineEdit::focusInEvent() from container?

    I want to do something to an QLineEdit when the user clicks on it. It looks like I need to capture the QLineEdit::focusInEvent(). I do NOT want to have to spin my own, is there some way to capture the event in the container dialog?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Howto handle QLineEdit::focusInEvent() from container?

    Install an event filter on your dialog: QObject::installEventFilter(). There you can catch the focus event of your line edit.

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: Howto handle QLineEdit::focusInEvent() from container?

    Hi,
    I am a newbie to Qt programming. I was able to successfully install event filters on my objects when I coded the ui myself. I then decided to use Qt Designer to construct my UI but I am unable to figure out how event filters get installed on ui objects constructed using the designer. How do I installEventFilter using QtDesigner?

    Thanks,
    Priya

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Howto handle QLineEdit::focusInEvent() from container?

    e.g.:
    Qt Code:
    1. ui->NameOfYourWidget->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    right in the constructor.

  5. The following 2 users say thank you to Lykurg for this useful post:

    priyasuri (10th December 2010), wendelmaques (12th January 2011)

  6. #5
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Exclamation Re: Howto handle QLineEdit::focusInEvent() from container?

    Hi Lykurg,
    Thank you for the quick response. I did try installing the event filter using the method you suggested
    ui->NameOfYourWidget->installEventFilter(this); but when I run the application, the two QLineEdit boxes to which I have installed the event filter show up blank. Is there a way to debug this ?

    Thanks again for you time,
    Priya

    Hi Lykurg,
    I managed to debug it myself. For some reason when I set the return value to false in the eventFilter function, the objects show up. Wonder if I am missing something..
    Here is my eventFilter function...

    Qt Code:
    1. bool QGridEventHandler::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. bool bRetVal = false;
    4. if (event->type() == QEvent::FocusIn) {
    5. QLineEdit *grid = static_cast<QLineEdit *> (obj);
    6. if (grid->text().isEmpty()){
    7. if (bTurnChk) {
    8. grid->setText("X");
    9. bTurnChk = false;
    10. }
    11. else {
    12. grid->setText("O");
    13. bTurnChk = true;
    14. }
    15. }
    16. }
    17. return bRetVal;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Priya

    I guess I should try myself before asking I just realized other events get triggered before the launch of the application, which need to be processed in the default manner. This happens only when the custom eventFilter function returns false if the QLineEdit's event is not processed or when the QObject's eventFilter function is called from within..
    I have changed the code to the following and this works now...
    Qt Code:
    1. bool QGridEventHandler::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. bool bRetVal = true;
    4. if (event->type() == QEvent::FocusIn) {
    5. QLineEdit *grid = static_cast<QLineEdit *> (obj);
    6. if (grid->text().isEmpty()){
    7. if (bTurnChk) {
    8. grid->setText("X");
    9. bTurnChk = false;
    10. }
    11. else {
    12. grid->setText("O");
    13. bTurnChk = true;
    14. }
    15. }
    16.  
    17. }
    18. else {
    19. bRetVal = QObject::eventFilter(obj, event);
    20. }
    21. return bRetVal;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for your patience
    Last edited by priyasuri; 10th December 2010 at 20:08.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Howto handle QLineEdit::focusInEvent() from container?

    Well, I guess then your event filter is the problem. What do you want to achieve and how does your event filter look like?

  8. The following user says thank you to Lykurg for this useful post:

    priyasuri (10th December 2010)

Similar Threads

  1. Replies: 1
    Last Post: 30th July 2010, 07:23
  2. focusInEvent()
    By coderbob in forum Qt Programming
    Replies: 6
    Last Post: 24th December 2007, 21:31
  3. Quick ? about qSort(Container & container)
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2007, 11:20
  4. QLineEdit and focusInEvent
    By fuzzywuzzy01 in forum Qt Programming
    Replies: 5
    Last Post: 16th August 2007, 23:05
  5. Howto handle key-release with autorepeat key
    By gould75 in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2006, 07:52

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.