[SOLVED]Add closable tab



  • 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)));
    @



  • 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);
    @



  • 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!



  • 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.



  • bq. No. QTabWidget::setTabBar() is protected, QTabWidget::tabBar() is not.

    It is protected... "tabBar()":http://doc.qt.digia.com/qt/qtabwidget.html#tabBar



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.