Search code examples
qtqt5qtabwidget

QTabWidget does not respect spacing from layout


I have this test case:

// Scroll
QScrollArea *sa = new QScrollArea(ui->centralWidget);
sa->setWidgetResizable( true );

// Layout for widgets
QVBoxLayout *vl_2 = new QVBoxLayout();
vl_2->setSpacing(0);

// Widget to attach the scroll to and the layout
QWidget *widget = new QWidget()
widget->setLayout(vl_2);
sa->setWidget(widget);

// Test widgets
QComboBox *cb_1 = new QComboBox();
QComboBox *cb_2 = new QComboBox();
vl_2->addWidget( cb_1 );
vl_2->addWidget( cb_2 );

And the widgets have 0 space between them.

But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);.

// TabWidget
QTabWidget *run_results = new QTabWidget(ui->centralWidget);
run_results->resize( this->size().width() -20, this->size().height() -80 );
run_results->show();

// Scroll
QScrollArea *sa = new QScrollArea(ui->centralWidget);
sa->setWidgetResizable( true );

// Layout for widgets
QVBoxLayout *vl_2 = new QVBoxLayout();
vl_2->setSpacing(0);

// Widget to attach the scroll to and the layout
QWidget *widget = new QWidget()
widget->setLayout(vl_2);
sa->setWidget(widget);

// Add the scroll to as the TabWidget tab.
run_results->addTab(sa, "test");

// Test widgets
QComboBox *cb_1 = new QComboBox();
QComboBox *cb_2 = new QComboBox();
vl_2->addWidget( cb_1 );
vl_2->addWidget( cb_2 );

Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?

I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.


Solution

  • I understand where I went wrong.

    In the first case I add my scrollarea as a sub-widget to the centralwidget. In the second example I add the scrollarea as the centralwidget which expands it to the whole tabwidget.

    I resolved the second case by first adding a holder QWidget as the tabwidget and then adding the scrollarea as a sub-widget to it.