Results 1 to 5 of 5

Thread: Switch the focus between 2 widgets

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    620
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Switch the focus between 2 widgets

    Hi to all,
    In my application I have mainly 2 widgets ( QWidgets , 2 waveform viewers ) and I would switch the focus between these 2 widgets with the shift key.
    For example I have
    wave_1 and wave_2. When the application starts the wave_1 has focus.
    If I press shift wave_2 must have the focus. Pressing shift again wave_1 will have the focus and so on.
    Pressing shift the focus must pass from wave_1 and wave_2, no other widgets.


    How can I do it?

    Best Regards
    Franco Amato

  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: Switch the focus between 2 widgets

    in wave_1 hold a pointer to wave_2 and vice versa. Then use QWidget::keyPressEvent() and there callwave_X->setFocus().

    Or use signal and slots instead of the pointers.

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    620
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Switch the focus between 2 widgets

    Quote Originally Posted by Lykurg View Post
    in wave_1 hold a pointer to wave_2 and vice versa. Then use QWidget::keyPressEvent() and there callwave_X->setFocus().

    Or use signal and slots instead of the pointers.
    Hi, I have a CentralWidget containing the 2 widget.
    I don't have to call setFocus from such container widget?

    I wrote this code

    Qt Code:
    1. void CentralWidget::keyPressEvent( QKeyEvent*pe )
    2. {
    3. switch( pe->key() )
    4. {
    5. case Qt::Key_Tab:
    6. {
    7. // switch the focus
    8. if( m_pWave2->hasFocus() )
    9. {
    10. m_pWave1->setFocus( Qt::TabFocusReason );
    11. }
    12. else if ( m_pWave1->hasFocus() )
    13. {
    14. m_pWave2->setFocus(Qt::TabFocusReason);
    15. }
    16.  
    17. break;
    18. }
    19.  
    20. default:
    21. QWidget::keyPressEvent(pe);
    22. break;
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    this can be ok?
    Last edited by franco.amato; 14th December 2009 at 22:39.
    Franco Amato

  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: Switch the focus between 2 widgets

    Quote Originally Posted by franco.amato View Post
    Hi, I have a CentralWidget containing the 2 widget.
    I don't have to call setFocus from such container widget?
    if you have a main widget, then it's easier.
    I wrote this code

    ...

    this can be ok?
    That looks nice. Should work. Just try it.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    620
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Switch the focus between 2 widgets

    Quote Originally Posted by Lykurg View Post
    if you have a main widget, then it's easier..
    Hi I think I did a mistake.
    If I press "tab key" from the main widget it mean that the main widget has the focus to receive keyboard event right?
    Is not I want. I neet the focus only between wave_1 and wave_2

    so I don't know how to "solve" that problem
    Franco Amato

Similar Threads

  1. 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
  2. Replies: 0
    Last Post: 13th February 2009, 09:26
  3. Upper limit on number of widgets?
    By jdiewald in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2008, 00:00
  4. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 07:54
  5. Tab/Enter focus problem
    By b1 in forum Qt Programming
    Replies: 4
    Last Post: 24th October 2006, 00:34

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.