QTabWidget: How to know when last tab is removed ?
-
I have a program in which the user dynamically adds and removes tabs from a QTabWidget. I need the program to take action when the last tab is removed. I did something similar with a QSplitter by installing an event filter on the splitter and then checking four splitter->count()==0 when a QEvent::childRemoved event was sent. Unfortunately the QTabWidget does not seem to receive any childRemoved events. Is there any way of detecting this situation ? Note that the tabs are removed automatically by Qt when their page is deleted.
The code is shown below:
@bool DynamicDataFrame::eventFilter(QObject obj, QEvent event) {
if (obj==splitter) {
QChildEvent childEvent = dynamic_cast<QChildEvent >(event);
if (!placeHolderFrame && childEvent && childEvent->type()==QChildEvent::ChildRemoved) {
if (splitter->count()==0) {
this->deleteLater();
return false;
}
}
}
if (obj==tabArea) {
QChildEvent childEvent = dynamic_cast<QChildEvent >(event);
if (!placeHolderFrame /&& childEvent && childEvent->type()==QChildEvent::ChildRemoved/) {
if (tabArea->count()==0) {
this->deleteLater();
return false;
}
}
}
return false;
}@ -
Hi elidiovaldez5.
Maybe you can try capture signal "QTabWidget::tabCloseRequested()":http://qt-project.org/doc/qt-5.0/qtwidgets/qtabwidget.html#tabCloseRequested and verify if "QTabWidget::count":http://qt-project.org/doc/qt-5.0/qtwidgets/qtabwidget.html#count-prop it's equal to zero.
Hope it's util.Regards.
-
you can listen to the "currentChanged() signal":http://qt-project.org/doc/qt-4.8/qtabwidget.html#currentChanged .
If -1 is emitted then there is no tab left. -
Thanks very much. I'll give both of them a try.