Connect to QTabWidget's tabBar()
-
Hi,
I am trying to build a widget containing a tabbed view, like a web browser. I thought QTabWidget would be a nice choice, and went on built one. According to the docs, if I want the tabs to be closable, I need to call setTabsClosable(true), and then connect the tab bar's tabCloseRequested(int) to somewhere, and then handle the close event.
Now the problem: The compiler refuse to accept the connecting statement.
@
error: no matching function for call to 'UJ::Qelly::TabWidget::connect(QTabBar*, const char*, UJ::Qelly::TabWidget* const, const char*)'
/usr/local/Cellar/qt/4.7.4/include/QtCore/qobject.h:209: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
/usr/local/Cellar/qt/4.7.4/include/QtCore/qobject.h:314: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
make: *** [TabWidget.o] Error 1
@I just don't see the problem. UJ::Qelly::TabWidget is a subclass of QTabWidget, which is a subclass of QObject, is it not? And then I tried this:
@
connect((QObject *)tabBar(), SIGNAL(tabCloseRequested(int)),
this, SLOT(onTabCloseRequested(int)));
@And the compiler shut up. And everything worked (at least everything I tested worked). But this does not seem to be a very nice solution (not exactly comfortable with C-style type casting, you know). Am I doing something wrong here? What should I do to connect the signal correctly?
Thanks.
(TabWidget.h)
@
#ifndef TABWIDGET_H
#define TABWIDGET_H#include <QTabWidget>
class QTabBar;namespace UJ
{namespace Qelly
{class TabWidget : public QTabWidget
{
Q_OBJECTpublic:
explicit TabWidget(QWidget *parent = 0);signals:
void tabWillClose(QWidget *tab);private slots:
void onTabCloseRequested(int index);};
} // namespace Qelly
} // namespace UJ
#endif // TABWIDGET_H
@TabWidget.cpp
@
#include "TabWidget.h"namespace UJ
{namespace Qelly
{TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent)
{
setDocumentMode(true);
setTabsClosable(true);
connect(tabBar(), SIGNAL(tabCloseRequested(int)),
this, SLOT(onTabCloseRequested(int)));
}void TabWidget::onTabCloseRequested(int index)
{
QWidget *w = widget(index);
removeTab(index);
emit tabWillClose(w);
w->deleteLater();
}} // namespace Qelly
} // namespace UJ
@
-
I just found that QTabWidget also has tabCloseRequested(int), and connecting to this one (instead of QTabBar's signal) solves this particular problem. But I still don't understand why the initial code causes an error.
-
You're missing
@
#include <QTabBar>
@in your TabWidget.cpp file.
-
You are correct. I'm such an idiot. LOL
Thanks!