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

8th April 2011, 10:03
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?...

8th April 2011, 10:34
I have looked into overriding the QTabWidget::focusInEvent, but to no avail.
Can you show the code and tell what is the behavior observed?

8th April 2011, 11:23
Can you show the code and tell what is the behavior observed?The code I have for focusInEvent is as follows:

void TabWidget::focusInEvent(QFocusEvent *pEvent)
// Default handling of the event


// Activate the widget of the given tab index

QWidget *crtWidget = currentWidget();

if (crtWidget)
}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. :)

7th October 2011, 12:41
Could you share the code please ?

8th October 2011, 01:43
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.


#include "commonwidget.h"

#include <QTabWidget>

class TabWidget : public QTabWidget, public CommonWidget

explicit TabWidget(QWidget *pParent);

private Q_SLOTS:
void tabChanged(const int &pIndex);
void getFocus(QWidget *, QWidget *pNew);

#endif and
#include "tabwidget.h"

#include <QApplication>
#include <QPainter>
#include <QPaintEvent>

TabWidget::TabWidget(QWidget *pParent) :
// Set some properties
// Note: we give a strong focus policy to the tab widget so that it can also
// get focus by being clicked on


// A connection to handle the change of tab

connect(this, SIGNAL(currentChanged(int)),
this, SLOT(tabChanged(const int &)));
connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),
this, SLOT(getFocus(QWidget *, QWidget *)));

void TabWidget::tabChanged(const int &pIndex)
// A new tab has been selected, so give the focus to its widget

QWidget *crtWidget = widget(pIndex);

if (crtWidget)

void TabWidget::getFocus(QWidget *, QWidget *pNew)
// The tab widget (or a part of it) has just received the focus and, here,
// we want to take advantage of this to give the focus to the widget of the
// active tab as a result of the user clicking on the active tab (since this
// won't emit the currentChanged signal). In our case, this means we are
// after pNew being of QTabBar type and that, more importantly, its parent
// is this tab widget

if (pNew && (pNew->parentWidget() == this))
// The tab widget didn't have the focus, but the user has just clicked
// on the tab widget's active tab, thus giving the focus to the tab
// widget, so now we need to give the focus to the active tab's widget


21st August 2013, 06:58
can any body tell me how to move from one tab to another using pushbutton.

21st August 2013, 13:53
Did you have a look at the documentation for QTabWidget (http://qt-project.org/doc/qt-5.0/qtwidgets/qtabwidget.html) and that for QTabWidget::setCurrentIndex() (http://qt-project.org/doc/qt-5.0/qtwidgets/qtabwidget.html#currentIndex-prop) in particular?