Results 1 to 6 of 6

Thread: Question about focus

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Question about focus

    Good morning to all, this is my scenario:
    I wrote an application based on QMainWindow containing a centralwidget.
    The centralwidget contains 2 classes inherited from QWidget lets say ww1 and ww2.
    Now I would, pressing tab, move the focus between ww1 and ww2.
    I tried to override the focusInEvent - focusOutEvent methods of every QWidget
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. //code
    4. protected:
    5. virtual void focusInEvent( QFocusEvent* event );
    6. virtual void focusOutEvent( QFocusEvent* event );
    7. //more code
    8. };
    To copy to clipboard, switch view to plain text mode 
    Now I would, from the QMainWindow, to execute some code depending on the QWidget having the focus. Now I emit a signal in the focusIn/Out methods so:

    Qt Code:
    1. /*********************************************/
    2. /* focusInEvent
    3. /*********************************************/
    4. void MyWidget::focusInEvent( QFocusEvent *event )
    5. {
    6. Q_UNUSED(event);
    7. emit sig_focusIn(); // widget had the focus
    8. }
    9.  
    10. /*********************************************/
    11. /* focusOutEvent
    12. /*********************************************/
    13. void MyWidget::focusOutEvent( QFocusEvent *event )
    14. {
    15. Q_UNUSED(event);
    16. emit sig_focusOut(); // focus is gone
    17. }
    To copy to clipboard, switch view to plain text mode 
    The signals are connected to a slot that's part of mainwindow class. My code works but I'm not sure if is the best way to achieve what I would.
    For example I read about a virtual bool eventFilter(QObject *obj, QEvent *evt); that can filter ( as it says ) the event sent to other widgets. For example intercepting the focus event. The problem is how can I know it the focus is for ww1 or ww2?
    I hope to get a reply.
    Best Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Question about focus

    Qt Code:
    1. bool MyMainWindow::eventFilter(QObject *obj, QEvent *evt)
    2. {
    3. QWidget * w = dynamic_cast<QWidget *>(obj);
    4. if(w == ww1)
    5. return false; // widget 1 event
    6. else if(w == ww2)
    7. return false; // widget 2 event
    8. else
    9. return QMainWindow::eventFilter(obj, event);
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Santosh Reddy for this useful post:

    franco.amato (14th July 2011)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Question about focus

    Quote Originally Posted by Santosh Reddy View Post
    Qt Code:
    1. bool MyMainWindow::eventFilter(QObject *obj, QEvent *evt)
    2. {
    3. QWidget * w = dynamic_cast<QWidget *>(obj);
    4. if(w == ww1)
    5. return false; // widget 1 event
    6. else if(w == ww2)
    7. return false; // widget 2 event
    8. else
    9. return QMainWindow::eventFilter(obj, event);
    10. }
    To copy to clipboard, switch view to plain text mode 
    Thank you very much.
    Just a couple of things:
    Why did you return false in case of ww1 and ww2?
    I have to install the eventfilter in my MyWidget class right?

    Thanx again
    Franco Amato

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Question about focus

    Why did you return false in case of ww1 and ww2?
    you can replace it with your functions, and return false, this will ensure that event is sent to the actual widget ww1 / ww2
    I have to install the eventfilter in my MyWidget class right?
    event filters are installed on objects not on class, you need to install event filter in ww1 and ww2 not on MyWidget

  6. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: Question about focus

    Hi thanx,
    in which case I have to return true?
    This is what I wrote:

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *evt)
    2. {
    3. WaveWidget *w = dynamic_cast<WaveWidget *>(obj);
    4. if(w == ww1)
    5. {
    6. switch (evt->type())
    7. {
    8. case QEvent::FocusIn:
    9. qDebug("Wave 1 received focus");
    10. return false;
    11. case QEvent::FocusOut:
    12. qDebug("Wave 1 focus lost");
    13. return false;
    14. default:
    15. return true; // In this case I have to return true?
    16. }
    17. }
    18. else if(w == ww2)
    19. {
    20. switch (evt->type())
    21. {
    22. case QEvent::FocusIn:
    23. qDebug("Wave 2 received focus");
    24. return false;
    25. case QEvent::FocusOut:
    26. qDebug("Wave 2 focus lost");
    27. return false;
    28. default:
    29. return true; // In this case I have to return true?
    30. }
    31. }
    32. else
    33. return QMainWindow::eventFilter(obj, evt);
    34. }
    To copy to clipboard, switch view to plain text mode 

    My doubt is in when I have to return true?
    Regards
    Franco Amato

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Question about focus

    if you want filter the event / eat the event / stop the event / consume the event, then return true, by default return QMainWindow::eventFilter(obj, evt);

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

    franco.amato (15th July 2011)

Similar Threads

  1. Question about focus
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 22nd January 2010, 18:46
  2. widget focus question
    By eva2002 in forum Newbie
    Replies: 1
    Last Post: 12th January 2010, 01:04
  3. Window focus question
    By waynew in forum Newbie
    Replies: 3
    Last Post: 1st November 2009, 00:11
  4. Window focus issues (How to force focus to a window?)
    By montylee in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2009, 02:00
  5. Focus issues / Setting multiple focus
    By ComputerPhreak in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2007, 07:09

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
  •  
Qt is a trademark of The Qt Company.