Question

I have created a QTabWidget and its tab count is changing dynamically when a signal is triggered. I have added tabs like:

QWidget *centralWidget = new QWidget();
ui->tabWidget->addTab(centralWidget, "tab header");

Everything is OK by now, but the problem is that I want to add buttons to these tabs. Is there a way to do it?

Was it helpful?

Solution

You can set any QWidget subclass as central widget, or you can add any QWidget subclass to your central widget.

For example, if you create a Qt Designer Form Class (that's what Qt Creator calls a class consisting of a .cpp, a .h and a .ui file) called MyCentralWidget, you can do:

#include "MyCentralWidget.h"

// ...

QWidget *centralWidget = new MyCentralWidget();
ui->tabWidget->addTab( centralWidget, "tab header" );

You can then use the Qt Designer (or Qt Creator) to design MyCentralWidget in any way you want.

Another example is adding the widget to a layout within the central widget:

#include "MyCentralWidget.h"
#include <QtGui/QGridLayout>

// ...

QWidget *centralWidget = new QWidget();
centralWidget->setLayout( new QGridLayout() );
centralWidget->layout()->addWidget( new MyCentralWidget() );
ui->tabWidget->addTab( centralWidget, "tab header" );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top