Search code examples
c++qtqt5qtablewidgetqpushbutton

How to add a "new tab" on an existing QTabWidget using a QPushButton


I am trying to add a "new tab" on an existing QTabWidget using a QPushButton as shown below:

tab

The problem I have is that as I push the button nothing happens and no "new tab" is added. I set all the proper connection and slots but something is preventing me from adding it to the QTabWidget. Basically nothing happens as I push the button.

Below the minimal verifiable example:

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void newTab();

private slots:
    void on_addTabBtn_clicked();

private:
    Ui::MainWindow *ui;
};

mainwindow.cpp

#include <QLabel>
#include <QTabBar>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tabWidget->clear();
    ui->tabWidget->addTab(new QLabel("+"), QString("+"));
    connect(ui->tabWidget, &QTabWidget::currentChanged, this, &MainWindow::on_addTabBtn_clicked);
    newTab();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::newTab()
{
    int position = ui->tabWidget->count() - 1;
    ui->tabWidget->insertTab(position, new QLabel("Insert New Tab"), QString("New Tab"));
    ui->tabWidget->setCurrentIndex(position);
    auto tabBar = ui->tabWidget->tabBar();
    tabBar->scroll(tabBar->width(), 0);
}


void MainWindow::on_addTabBtn_clicked()
{
    int index = 0;
    if(index == this->ui->tabWidget->count() - 1) {
        newTab();
    }
}

In case you would also like to see the simple .ui file see below:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>428</width>
    <height>279</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QTabWidget" name="tabWidget">
      <widget class="QWidget" name="tab">
       <attribute name="title">
        <string>Tab 1</string>
       </attribute>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
        <string>Tab 2</string>
       </attribute>
      </widget>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QPushButton" name="addTabBtn">
      <property name="text">
       <string>Add Tab</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>428</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

I tried to solve the problem in many ways and researched what the cause might be. I came across several references such as this one, this and also this one but none of them helped to completely solve the problem.

I am sure that the property of currentChanged is correct, however the callback to trigger the button is not doing the job despite there are no errors in the terminal.

What am I missing? Thanks for pointing to the right direction for solving this issue.


Solution

  • connect the clicked signal of the button to the addTab function/slot of the tabwidget (or to an intermediate function/lambda that calls addTab on the correct destination object).

    For example:

    connect(ui->addTabBtn, &QPushButton::clicked, this, [&] { ui->tabWidget->addTab(new QLabel("+"), QString("+")); });
    

    Or something along those lines.