QTabWidget QToolButton corner widget disappears

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); } 
+4
source share
1 answer

I solved it. You need to fix the minimum height for both QToolButton and QTabBar .

So far, this code works for me on python:

 def initialise() # Initialise your Tab Widget however you need self.tabCloseRequested.connect(self.removeTab) def removeTab(self,index): h = self.cornerWidget().height() self.removeTab(index) self.update() if self.count() == 0: self.cornerWidget().setMinimumHeight(h) self.setMinimumHeight(h) 
+5
source

Source: https://habr.com/ru/post/1496119/


All Articles