[SOLVED]Add closable tab
-
wrote on 13 Jan 2013, 11:54 last edited by
Hi to all,
in a recent discussion linked "here":http://qt-project.org/forums/viewthread/23697/, i've defined a subclass of QTabWidget.
Now I'm defining a method for adding a closable tab, because by using the method setTabsClosable, the button is added to all tabs but I want to create some tab with the button. I've defined this method:@
void TabWidget::addClosableTab(QString name, QString imagepath){
addTab(new QWidget, name);
QTabBar *tab = tabBar();
QToolButton *tool = new QToolButton();
tool->setIcon(QIcon(imagepath));
tab->setTabButton(QTabWidget::count() -1, QTabBar::RightSide, tool);
}
@i've defined this connect in the constructor but it doesn't work with the button defined (if i use setClosableTabs, the connect works):
@
connect(tabBar(), SIGNAL(tabCloseRequested(int)), this, SLOT(deleteTab(int)));
@ -
wrote on 13 Jan 2013, 13:24 last edited by
I assume you mean that connect() does actually work, but no signal is emitted.
If you are using your own close buttons you will have to emit tabCloseRequested() signal on your own as well (basically forward the button's clicked() signal, take a look at "_q_close_Tab()":http://code.woboq.org/qt5/qtbase/src/widgets/widgets/qtabbar.cpp.html#603).
It is also possible to reverse the operation, make tabs closable globally and explicitly make tabs non-closable (which does not require you to subclass QTabWidget).
@
tabWidget->setTabsClosable(true);
...
int tabIndex = tabWidget->addTab(...);
QTabBar *tabBar = tabWidget->tabBar();
tabBar->setTabButton(tabIndex,
static_castQTabBar::ButtonPosition(tabBar
->style()
->styleHint(QStyle::SH_TabBar_CloseButtonPosition, 0, tabBar)),
0);
@ -
wrote on 14 Jan 2013, 08:44 last edited by
bq. I assume you mean that connect() does actually work, but no signal is emitted.
Yes!
bq. If you are using your own close buttons you will have to emit tabCloseRequested() signal on your own as well (basically forward the button’s clicked() signal, take a look at _q_close_Tab() [code.woboq.org]).
I've solved by connecting the clicked signal to the _q_close_Tab()! It works!
bq. @QTabBar *tabBar = tabWidget->tabBar();@
This method is protected. It requires a subclass.
Thank you so much!
Now it is perfect! -
wrote on 14 Jan 2013, 09:20 last edited by
You're welcome!
Feel free to prepend to initial post title with '[Solved] ...' to indicate that there is a solution inside.
[quote author="TFabbri" date="1358153097"]I've solved by connecting the clicked signal to the _q_close_Tab()! It works![/quote]Be aware that you now depend on the Qt private API. Your code may (or may not) stop working with any Qt version other than the currently used (although it is, however, quite unlikely).[quote author="TFabbri" date="1358153097"]This method is protected. It requires a subclass.[/quote]No. QTabWidget::setTabBar() is protected, QTabWidget::tabBar() is not.
-
wrote on 14 Jan 2013, 10:16 last edited by
bq. No. QTabWidget::setTabBar() is protected, QTabWidget::tabBar() is not.
It is protected... "tabBar()":http://doc.qt.digia.com/qt/qtabwidget.html#tabBar
-
wrote on 14 Jan 2013, 10:32 last edited by
[quote author="TFabbri" date="1358158606"]It is protected... "tabBar()":http://doc.qt.digia.com/qt/qtabwidget.html#tabBar[/quote]No, "it isn't":http://qt-project.org/doc/qt-5.0/qtwidgets/qtabwidget.html#tabBar! ;-)
But I must have missed that you are talking about Qt4, where you are indeed right. It was protected in Qt4, but is no longer in Qt5.
1/6