I have a QTabWidget with dynamically added and deleted tabs. I also have a QToolButton set as a corner widget to handle add events on its clicked() signal.
When adding work, the removal of all tabs from the widget stops perfectly, which leads to the disappearance of the button. I tried using style sheets to create an invisible first tab with a width of 0, but this approach did not help me. Probably because I have setTabsClosable(true) on all tabs, which makes space for the close button.
I also thought that the button disappears when QTabBar collapses its height to 0 when there are no tabs. I tried tabBar()->setMinimumHeight(30); but this workaround also didn't work. Any ideas?
Here is my sample code reproducing the problem:
#include "mainwindow.h" #include "ui_mainwindow.h" #include "QToolButton" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->tabWidget->setTabsClosable(true); ui->tabWidget->tabBar()->setMinimumHeight(30); QToolButton *btn = new QToolButton(ui->tabWidget); btn->setText("Add new"); btn->setCursor(Qt::ArrowCursor); btn->setAutoRaise(true); ui->tabWidget->setCornerWidget(btn, Qt::TopLeftCorner); qDebug() << ui->tabWidget->cornerWidget(Qt::TopLeftCorner); connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); } MainWindow::~MainWindow() { delete ui; } //SLOT void MainWindow::closeTab(int index) { ui->tabWidget->removeTab(index); if(ui->tabWidget->count() == 0) qDebug() << ui->tabWidget->cornerWidget(Qt::TopLeftCorner); }
source share