[Solved]Subclass QTabWidget
-
Hi to all,
I want to subclass qTabWidget for accessing to qTabbar member for adding some tab with a push button.
I've followed this old "discussion":http://qt-project.org/forums/viewthread/9548.
Now, I want to define a slot in my custom widget for removing the tab with close button.
The subclass is now the following:@class TabWidget : public QTabWidget {
Q_OBJECT
public:
TabWidget();
TabWidget(QWidget *parent);
~TabWidget();
QTabBar *tabBar() const;
public slots:
void deleteTab(int index);
};
@But during the compile process I obtain this errors:
@
..C2/TabWidget.cpp:10: undefined reference tovtable for TabWidget' ..C2/TabWidget.cpp:10: undefined reference to
vtable for TabWidget'
..C2/TabWidget.cpp:12: undefined reference tovtable for TabWidget' ..C2/TabWidget.cpp:12: undefined reference to
vtable for TabWidget'
@Where's the problem? I've used two separate files (.cpp + .h) and I've executed qmake.
Thank you so much -
From the output: "TabWidget.cpp".... lines 10 and 12 to be more specific.
-
@TabWidget::TabWidget(){} // line 10
TabWidget::TabWidget(QWidget * p=0) : QTabWidget(p) {} // line 12
TabWidget::~TabWidget(){} // line 14
QTabBar * TabWidget::tabBar() const {return QTabWidget::tabBar();}@
-
I see what is wrong with the code:
line 12 contains QWidget * p = 0 which is not correct. "Default parameters" should be done on the header file:
@//.h
class TabWidget : public QTabWidget {
Q_OBJECT
public:
TabWidget();
TabWidget(QWidget *parent = 0); // Default parameter should be placed here
~TabWidget();
QTabBar *tabBar() const;
public slots:
void deleteTab(int index);
};
@@//.cpp
TabWidget::TabWidget(){} // line 10TabWidget::TabWidget(QWidget * p) : QTabWidget(p) {} // changed line 12
TabWidget::~TabWidget(){} // line 14
QTabBar * TabWidget::tabBar() const {return QTabWidget::tabBar();}@
And I also found something odd... You have "multiple default constructors"
@TabWidget(); //default constructor, cause of error in line 10.
TabWidget(QWidget *parent = 0); //another default constructor@A word of advice, some compiler might throw warnings, some might throw errors. It is better to remove these lines:
@TabWidget(); ///found in the headerTabWidget::TabWidget(){} //line 10, found in the .cpp file@
-
That error usually is caused by moc not doing its thing and that by qmake not having noticed that this file needs to be moc'ed.
So rerun qmake and try again.
-
Now it works.
I don't know why before qmake doesn't work.
Thank you so much!