Results 1 to 7 of 7

Thread: [SOLVED] Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

  1. #1
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default [SOLVED] Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    Hi, my application has a QTabWidget object and each tab consists of an editor. What I would like is that if my QTabWidget object gets the focus, then the editor of the current tab should automatically get the focus.

    I have been able to get this to work to an extent (e.g. when switching from one tab to another), but when another widget in my application has the focus and that I then click on the active tab of my QTabWidget object, the editor for that tab doesn't get the focus. I need to switch to another tab and back to get the focus which is clearly not what I want. On the other hand, if another widget in my application has the focus and that I then click on a non-active tab in my QTabWidget object, then the editor for that tab gets the focus, as expected since I take advantage of the QTabWidget::currentChanged signal to give the focus to the editor for the active tab.

    I have looked into overriding the QTabWidget::focusInEvent, but to no avail. I also thought I might get it to work by using QTabWidget::setFocusProxy, but still to no avail. Otherwise, yes, I do have given a strong focus policy to my QTabWidget object, since I want my QTabWidget object to accept focus by both tabbing and clicking (and tabbing works perfectly fine indeed).

    So... any idea of how I could achieve what I am after?...
    Last edited by agarny; 8th April 2011 at 11:07.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    I have looked into overriding the QTabWidget::focusInEvent, but to no avail.
    Can you show the code and tell what is the behavior observed?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    Quote Originally Posted by high_flyer View Post
    Can you show the code and tell what is the behavior observed?
    The code I have for focusInEvent is as follows:
    Qt Code:
    1. void TabWidget::focusInEvent(QFocusEvent *pEvent)
    2. {
    3. // Default handling of the event
    4.  
    5. QTabWidget::focusInEvent(pEvent);
    6.  
    7. // Activate the widget of the given tab index
    8.  
    9. QWidget *crtWidget = currentWidget();
    10.  
    11. if (crtWidget)
    12. crtWidget->setFocus();
    13. }
    To copy to clipboard, switch view to plain text mode 
    I know that to code (the activation of the tab's widget) to work fine, since 1) it's very trivial and 2) I use it to handle the QTabView::currentChanged signal. This being all said, the above code never gets executed at all. I put some qDebug traces and they just never show up. So, yes, I am a bit confused to say the least.


    Added after 18 minutes:


    Ok, after some more googling around, I have finally found a way to get what I want and need. I am now relying on the QApplication::focusChanged signal to check that my QTabWidget object now gets the focus and, if so, then give focus to the active tab's widget. This all works fine.
    Last edited by agarny; 8th April 2011 at 10:23.

  4. #4
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    Could you share the code please ?

  5. #5
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    This is not code that I use anymore, but I was able to dig out what I think I was referring to in my latest message. HTH, Alan.

    Qt Code:
    1. #ifndef TABWIDGET_H
    2. #define TABWIDGET_H
    3.  
    4. #include "commonwidget.h"
    5.  
    6. #include <QTabWidget>
    7.  
    8. class TabWidget : public QTabWidget, public CommonWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit TabWidget(QWidget *pParent);
    14.  
    15. private Q_SLOTS:
    16. void tabChanged(const int &pIndex);
    17. void getFocus(QWidget *, QWidget *pNew);
    18. };
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. #include "tabwidget.h"
    2.  
    3. #include <QApplication>
    4. #include <QPainter>
    5. #include <QPaintEvent>
    6.  
    7. TabWidget::TabWidget(QWidget *pParent) :
    8. QTabWidget(pParent),
    9. CommonWidget(pParent)
    10. {
    11. // Set some properties
    12. // Note: we give a strong focus policy to the tab widget so that it can also
    13. // get focus by being clicked on
    14.  
    15. setTabsClosable(true);
    16. setFocusPolicy(Qt::StrongFocus);
    17.  
    18. // A connection to handle the change of tab
    19.  
    20. connect(this, SIGNAL(currentChanged(int)),
    21. this, SLOT(tabChanged(const int &)));
    22. connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
    23. this, SLOT(getFocus(QWidget *, QWidget *)));
    24. }
    25.  
    26. void TabWidget::tabChanged(const int &pIndex)
    27. {
    28. // A new tab has been selected, so give the focus to its widget
    29.  
    30. QWidget *crtWidget = widget(pIndex);
    31.  
    32. if (crtWidget)
    33. crtWidget->setFocus();
    34. }
    35.  
    36. void TabWidget::getFocus(QWidget *, QWidget *pNew)
    37. {
    38. // The tab widget (or a part of it) has just received the focus and, here,
    39. // we want to take advantage of this to give the focus to the widget of the
    40. // active tab as a result of the user clicking on the active tab (since this
    41. // won't emit the currentChanged signal). In our case, this means we are
    42. // after pNew being of QTabBar type and that, more importantly, its parent
    43. // is this tab widget
    44.  
    45. if (pNew && (pNew->parentWidget() == this))
    46. // The tab widget didn't have the focus, but the user has just clicked
    47. // on the tab widget's active tab, thus giving the focus to the tab
    48. // widget, so now we need to give the focus to the active tab's widget
    49.  
    50. tabChanged(currentIndex());
    51. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2013
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    hello
    can any body tell me how to move from one tab to another using pushbutton.

  7. #7
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt 4.7.2 - QTabWidget - Give the focus to QTabWidget's current widget

    Did you have a look at the documentation for QTabWidget and that for QTabWidget::setCurrentIndex() in particular?

Similar Threads

  1. QTabWidget Corner Widget Problem
    By isamert in forum Qt Programming
    Replies: 6
    Last Post: 12th February 2011, 15:46
  2. Add multiple widget to QTabWidget Tab
    By Rooster in forum Newbie
    Replies: 5
    Last Post: 28th October 2010, 22:07
  3. How to add close button to each tab widget Qtabwidget?
    By kishore7771 in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2010, 08:42
  4. QTabWidget: how to force "no current widget"?
    By mattc in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2009, 12:18
  5. Force focus to a QTabWidget page's widget
    By thomaspu in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 06:54

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.