Work with QTabWidget



  • Hi! I'm developing the simple application and use QTabWidget in it. I want to make the dynamic adding/closing of the tabs. With setTabsClosable() - I've add the close buttons on the tabs, but the don't react on clicks. How can I add signal and slot to that buttons?



  • QTabWidget emits the tabCloseRequested() signal, which can be connected to to actually remove the tab using removeTab(). Be aware that the page widget itself is not deleted automatically.



  • Thanks
    And is there any ways to get the number of the tab, on which close button was presse, or I need to figure it by my own?





  • I've added the tabCloseRequested() signal to the removeTab() slot, but I don't understand how it works. Because when I write
    @connect(tabWidget, SIGNAL(tabCloseRequested(index)), this, SLOT(removeTab(index)));@
    where "index" is the index of the page, it gives me the mistake "no such SIGNAL tabCloseRequested(index)", but when I live as
    @connect(tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));@
    it woks. But now when I press on close button, it closes all tabs that follows the tab I'm closing.
    How does it work?



  • A signal, as well as a slot, is always identified by its signature, <code>tabCloseRequested(int)</code>. You cannot pass actual values, <code>tabCloseRequested(index)</code>.

    Although beeing ordinary methods, slots have to be marked as such, <code>public slots:</code>, which is not true for QTabWidget::removeTab() and you cannot connect to.

    The initial post might have been a bit misleading. You can connect to the tabCloseRequested() signal, but not to removeTab(), due to not beeing a slot.

    The common use case is to add a (private) slot to the embracing class, which is responsible for closing the tab and deleting the page widget.
    @
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow() : QMainWindow()
    {
    tabWidget_ = new QTabWidget;
    tabWidget_->addTab(new QLabel("TabA"), "TabA");
    tabWidget_->addTab(new QLabel("TabB"), "TabB");
    tabWidget_->addTab(new QLabel("TabC"), "TabC");
    tabWidget_->setTabsClosable(true);

        connect(tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab_(int)));
    
        setCentralWidget(tabWidget_);
    }
    

    private slots:
    void closeTab_(int index)
    {
    // Remove the tab using removeTab(). Be aware that the
    // page widget itself is not deleted!
    tabWidget_->removeTab(index);

        // OR (do NOT do both)
    
        // Delete the page widget, which automatically removes
        // the tab as well.
        delete tabWidget_->widget(index);
    }
    

    private:
    QTabWidget *tabWidget_;
    };
    @



  • I did that, but when I delete one tab, following tabs are deleted too!
    I don't understand why such thing appears. I have almost the same code as you, but the promblome is still there. Maybe I miss some thing?



  • I've found the problem! ;)
    @tabWidget_->removeTab(index);@ - here we remove the tab from the tabWidget_, and indexes of tabs are updated. Now we have anothe tab on the plase of the removed one.
    So, when I was trying to delete the page widget itself, in fact I was deleting the next tab. That on that moment was on the place of the removed tab.

    Thanks for help!!!



  • Well, that's what I meant with <code>do NOT do both</code> ;-)

    You're welcome.



  • Hi all,

    I am using tab widget in my application and I need to update the tab text color in order to show notification to the user. But I dint any way to do this. My requirement is to show alert to the user for the tab which is not currently selected. I have try many things to implement this behaviour but get failed every time. I have also use the qtabbar stylesheet but even that not provide any way to update the random tab style as there are only limited stated provided by the QTabbar stylesheet.
    I also find a method setTabTextColor in QTabbar class but dont find any way to use it. Please suggest me the way to implement the desired behaviour as I have spent much time on this.

    Thanks in advance
    Deepak Jangra



  • QTabWidget::tabBar() (and thus QTabBar::setTabTextColor()) is protected, so you'll need to subclass QTabWidget in order to make it publicly available.
    @
    class TabWidget : public QTabWidget
    {
    public:
    TabWidget(QWidget *parent = 0) : QTabWidget(parent) {}

    void setTabTextColor(int index, const QColor &color)
    {
        tabBar()->setTabTextColor(index, color);
    }
    

    };

    ...

    TabWidget *tabWidget = new TabWidget;
    tabWidget->setTabTextColor(0, Qt::red);
    @


Log in to reply
 

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