I am not sure what is going on in your code. Here is a very simple QMainWindow-based app:
// TabApp.h
#ifndef TABAPP_H
#define TABAPP_H
#include <QtWidgets/QMainWindow>
{
Q_OBJECT
public:
~TabApp();
private slots:
void onTabWidgetTabCloseRequested( int index );
private:
void addTab();
private:
};
#endif // TABAPP_H
// TabApp.h
#ifndef TABAPP_H
#define TABAPP_H
#include <QtWidgets/QMainWindow>
class QTabWidget;
class TabApp : public QMainWindow
{
Q_OBJECT
public:
TabApp(QWidget *parent = 0);
~TabApp();
private slots:
void onTabWidgetTabCloseRequested( int index );
private:
void addTab();
private:
QTabWidget * mpTabWidget;
};
#endif // TABAPP_H
To copy to clipboard, switch view to plain text mode
// TabApp.cpp
#include "TabApp.h"
#include <QTabWidget>
#include <QDebug>
{
mpTabWidget->setTabsClosable( true );
addTab();
addTab();
connect( mpTabWidget, &QTabWidget::tabCloseRequested, this, &TabApp::onTabWidgetTabCloseRequested );
setCentralWidget( mpTabWidget );
}
TabApp::~TabApp()
{
}
void TabApp::onTabWidgetTabCloseRequested( int index )
{
qDebug() << "Index:" << index;
qDebug() << "Text:" << mpTabWidget->tabText( index );
}
void TabApp::addTab()
{
int count = mpTabWidget->count();
mpTabWidget->addTab( pTab, tabText );
}
// TabApp.cpp
#include "TabApp.h"
#include <QTabWidget>
#include <QDebug>
TabApp::TabApp(QWidget *parent)
: QMainWindow(parent)
{
mpTabWidget = new QTabWidget( this );
mpTabWidget->setTabsClosable( true );
addTab();
addTab();
connect( mpTabWidget, &QTabWidget::tabCloseRequested, this, &TabApp::onTabWidgetTabCloseRequested );
setCentralWidget( mpTabWidget );
}
TabApp::~TabApp()
{
}
void TabApp::onTabWidgetTabCloseRequested( int index )
{
qDebug() << "Index:" << index;
qDebug() << "Text:" << mpTabWidget->tabText( index );
}
void TabApp::addTab()
{
int count = mpTabWidget->count();
QString tabText = QString( "Widget %1" ).arg( count + 1 );
QWidget * pTab = new QWidget( this );
mpTabWidget->addTab( pTab, tabText );
}
To copy to clipboard, switch view to plain text mode
// main.cpp
#include "TabApp.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
TabApp w;
w.show();
return a.exec();
}
// main.cpp
#include "TabApp.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TabApp w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
If I click the close button on the second tab ("Widget 2"), qDebug() prints:
Index: 1
Text: "Widget 2"
If I click the close button the first tab ("Widget 1"), qDebug() prints:
Index: 0
Text: "Widget 1"
One thing I did learn from this is that I was wrong about the tab being closed automatically. When I click the close button, my slot gets called, but the tab does not close automatically. So you can do what you intended - to put up a prompt and allow the user to choose whether the tab should be closed or not.
You should probably compare your code to this and see if there are any significant differences (aside from the fact that you define your UI in a Qt Designer file and I have created mine in code - but that makes no difference at runtime).
1. Find a book and learn it.
2. Done.
Looked for a book that had this example in it, didn't find one. Had to write it myself.
Bookmarks