How handle close tab event?
-
I have:
connect(tabWidget, &QTabWidget::tabCloseRequested, this, [this](int index) { onCloseTab(index); }); void TabWindow::onCloseTab(int index) { setWindowTitle("abc"); }But is not called
I also not found close events other widgets like QPlainTextEdit -
Hi
did you set
http://doc.qt.io/qt-5/qtabwidget.html#tabsClosable-prop
Also, this is a signal, not event.
CloseEvent are virtual function and you must subclass to override.Just tried fast sample with tabCloseRequested and it was called.
So we have to investigate why in your sample its not called. -
Hi,
Just to be sure, you did as @mrjj suggested and call setTabClosable ?
-
#include "mainwindow.h" #include <QTabWidget> #include <QVBoxLayout> #include <QWidget> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QTabWidget *tabWidget = new QTabWidget(); tabWidget->setTabsClosable(true); tabWidget->setMovable(true); tabWidget->addTab(new QWidget(),"1"); tabWidget->addTab(new QWidget(),"2"); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(tabWidget); QWidget *centralWidget = new QWidget; centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); } MainWindow::~MainWindow() {} -
Solution:
connect( tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(slotCloseTab()) ); void MainWindow::slotCloseTab() { QWidget *tab = dynamic_cast<QWidget*>( tabWidget->currentWidget() ); disconnect( tab, 0, 0, 0 ); tab->close(); delete tab; tab = NULL; }by [https://www.qtcentre.org/threads/46333-tabsClosable-not-working-!-how-to-close-tabs] but added delete tab
-
Solution:
connect( tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(slotCloseTab()) ); void MainWindow::slotCloseTab() { QWidget *tab = dynamic_cast<QWidget*>( tabWidget->currentWidget() ); disconnect( tab, 0, 0, 0 ); tab->close(); delete tab; tab = NULL; }by [https://www.qtcentre.org/threads/46333-tabsClosable-not-working-!-how-to-close-tabs] but added delete tab
@AndrzejB
Hi
oK. tried your code withQTabWidget *tabWidget = new QTabWidget(); tabWidget->setTabsClosable(true); tabWidget->setMovable(true); tabWidget->addTab(new QWidget(), "1"); tabWidget->addTab(new QWidget(), "2"); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(tabWidget); QWidget *centralWidget = new QWidget; centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); connect(tabWidget, &QTabWidget::tabCloseRequested, this, [this](int index) { qDebug() << "called"; });and that was called so i wondered if it was other tabWidget you connected or something like that.
anyway, if using slotCloseTab works, i guess it fine.
Just odd your first code/try didnt work. -
You should rather use QTabWidget::widget and use the int parameter of the signal to get the widget that was requested to be closed. It might not be the one that's currently shown.