PDA

View Full Version : QTabWidget does not respect spacing from layout



Anastaziel
6th March 2015, 21:25
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.

anda_skoa
7th March 2015, 08:23
Your code does not show how the tab widget is added to the central widget's layout and how the scroll area is added to the tab widget's layout.

The problem could very well be that your actual code is also missing these lines.

Cheers,
_

Anastaziel
9th March 2015, 09:06
@anda_skoa, thanks for the reply. That is all the code for the example. 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.

How do I close a thread as solved?