Close button only for some tabs in Qt

后端 未结 7 1975
天命终不由人
天命终不由人 2021-02-05 05:22

I am using Qt for an assignment I have for college, and I want to use QTabWidget to display a chat window much like Pidgin\'s. I want to make the \"group chat\" tab

7条回答
  •  天涯浪人
    2021-02-05 05:54

    The best way for adding a pushbutton to some tabs and not in other is to define a subclass of QTabWidget for taking the QTabBar that is a potected! The code below is tested and it works:

    //TabWidget.h
    
    #ifndef TABWIDGET_H
    #define TABWIDGET_H
    #include 
    
    class TabWidget : public QTabWidget {
    public:
        TabWidget(QWidget *parent);
        ~TabWidget();
        QTabBar *tabBar() const;
    };
    #endif  /* TABWIDGET_H */
    
    //TabWidget.cpp
    
    #include "TabWidget.h"
    TabWidget::TabWidget(QWidget * p=0) : QTabWidget(p) {}
    
    TabWidget::~TabWidget() {}
    
    QTabBar * TabWidget::tabBar() const {return QTabWidget::tabBar();}
    

    For using this subclass and create a new tab with a custom button you have to following this instructions ( ui->tabWidget is a QTabWidget with setClosableTabs=false):

    TabWidget *t = (TabWidget *) ui->tabWidget;
    t->addTab(new QWidget, "empty");
    QTabBar *tab = t->tabBar();
    QPushButton *b = new QPushButton();
    b->setText("x");
    tab->setTabButton(tab->count() -1, QTabBar::RightSide, b);
    connect(b,SIGNAL(...),this,SLOT(...));
    

提交回复
热议问题