문제

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?

도움이 되었습니까?

해결책

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" );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top