#ifndef TABWIDGET_H
#define TABWIDGET_H
#include "commonwidget.h"
#include <QTabWidget>
class TabWidget
: public QTabWidget,
public CommonWidget
{
Q_OBJECT
public:
explicit TabWidget
(QWidget *pParent
);
private Q_SLOTS:
void tabChanged(const int &pIndex);
};
#endif
#ifndef TABWIDGET_H
#define TABWIDGET_H
#include "commonwidget.h"
#include <QTabWidget>
class TabWidget : public QTabWidget, public CommonWidget
{
Q_OBJECT
public:
explicit TabWidget(QWidget *pParent);
private Q_SLOTS:
void tabChanged(const int &pIndex);
void getFocus(QWidget *, QWidget *pNew);
};
#endif
To copy to clipboard, switch view to plain text mode
and
#include "tabwidget.h"
#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
TabWidget
::TabWidget(QWidget *pParent
) : CommonWidget(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
setTabsClosable(true);
setFocusPolicy(Qt::StrongFocus);
// A connection to handle the change of tab
connect(this, SIGNAL(currentChanged(int)),
this, SLOT(tabChanged(const int &)));
}
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)
crtWidget->setFocus();
}
{
// 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
tabChanged(currentIndex());
}
#include "tabwidget.h"
#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
TabWidget::TabWidget(QWidget *pParent) :
QTabWidget(pParent),
CommonWidget(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
setTabsClosable(true);
setFocusPolicy(Qt::StrongFocus);
// 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)
crtWidget->setFocus();
}
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
tabChanged(currentIndex());
}
To copy to clipboard, switch view to plain text mode
Bookmarks