How to code my c++ custom tabbar inherit from QTabBar which add a '+' button after the last tab?

51 views Asked by At

I want to add a '+' button after tabbar in QT by inherit QTabBar, But I don't know how to code my custom tabBar class(which virtual function should I override).

I want a c++ code.

Could you please give me some suggestions about it?

Thank you so much.

enter image description here

Here is my source code, it it not worked as I want.

#include <QTabBar>
#include <QTabWidget>
#include <QToolButton>

class TabBarPlus : public QTabBar {
public:
    TabBarPlus(QWidget *parent = nullptr);
protected:
    void resizeEvent(QResizeEvent *) override;
    QSize tabSizeHint(int index) const override;
    QSize minimumTabSizeHint(int index) const override;
    void changeEvent(QEvent *) override;

private:
    QToolButton* _pAddTabButton;
};

class TabWidget : public QTabWidget{
public:
    TabWidget(QWidget* parent = nullptr);
private:
    TabBarPlus* tabBar;
};
TabBarPlus::TabBarPlus(QWidget *parent): QTabBar(parent) {
    setElideMode(Qt::ElideRight);
    _pAddTabButton = new QToolButton();
    _pAddTabButton->setParent(this);
    _pAddTabButton->setText("+");
    _pAddTabButton->setFixedSize(30,20);
}

QSize TabBarPlus::minimumTabSizeHint(int index) const {
    double height = QTabBar::minimumTabSizeHint(index).height();
    return QSize(60, height);
}

QSize TabBarPlus::tabSizeHint(int index) const {
    double height = QTabBar::tabSizeHint(index).height();
    return QSize(60, height);
}

void TabBarPlus::changeEvent(QEvent * event) {
    QTabBar::changeEvent(event);
}

void TabBarPlus::resizeEvent(QResizeEvent * event){
    double width = QTabBar::sizeHint().width();
    double height = 0.0;
    if(count()>0){
        height = QTabBar::tabSizeHint(0).height();
    }
    resize(width + _pAddTabButton->sizeHint().width(), height);
    _pAddTabButton->move(width, 0);
}

/**************************************************************
 **************************************************************
 **************************************************************/

TabWidget::TabWidget(QWidget* parent) : QTabWidget(parent) {
    resize(400, 600);
    tabBar = new TabBarPlus();
    this->setTabBar(tabBar);
    this->addTab(new QWidget(), "test1");
    this->addTab(new QWidget(), "test2");
    this->addTab(new QWidget(), "test3");
}

Here is the tab page when I run my code. There is space before the '+' button enter image description here

and there is a symbol in the front of the tab when I clicked the last tab

enter image description here

I don't know how to change my code to fix this bug.

1

There are 1 answers

2
Sig Moid On

This is hard without seeing your source code, but I would add the "+" button as a separate button widget to your tabs, and put all of that into the header of your app. I did something similar when building a QT5 web browser, though I did that in PyQt5. Here's how I did it (it's open source, so you can steal the approach):

self.tabs = TabWidget()
self.add_tab(self.default_page)

# URL Bar

# Additional Buttons

self.add_tab_btn = QPushButton("  ↻  ")
self.add_tab_btn.setMinimumHeight(30)
self.add_tab_btn.clicked.connect(lambda: self.add_tab(self.default_page))
self.add_tab_btn.setFont(button_font)

I haven't worked with Qt in C++, but I assume the implementation would look more or less the same.