2
votes

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?

1
What exactly do you mean by "add button to this tabs/centralWidget"? Where do you want to add one? On one tab page, on all of them, only on some of them? Should the button cover the whole tab page, or shall it be in a specific location? - Tim Meyer
Actually, i dont want only a button. Button is just an example. I want all of the tabs have a button and a listwidget and someother gui elements for further need. I want them in a specific location of course. - thehilmisu

1 Answers

1
votes

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" );