Domanda

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?

È stato utile?

Soluzione

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" );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top