PDA

View Full Version : Can't get layout to work - QScrollArea with form layout in tab



xtal256
9th February 2011, 10:29
I am trying to use a QScrollArea as a tab page, but for some reason I cannot get the child widgets to layout correctly using a QFormLayout.
I am not using Qt Designer in this case as i am dynamically adding rows to the form layout.

Here is the code. AbstractPropertyPage is the class that is added to the tab widget. For the purpose of this post, i have omitted all but the relevant code, but i have other subclasses of AbstractPropertyPage which add specific properties to the page.

class AbstractPropertyPage : public QScrollArea{
Q_OBJECT
protected:
QScrollArea* scrollArea;
QWidget* scrollAreaContents;
QFormLayout* formLayout;

public:
AbstractPropertyPage(QWidget* parent = 0);

void addRow(String labelText, QWidget* widget) {
formLayout->addRow(labelText, widget);
}
//...
};

AbstractPropertyPage::AbstractPropertyPage(QWidget * parent) :
QWidget(parent) {
scrollAreaContents = new QWidget(this);
formLayout = new QFormLayout(scrollAreaContents);
formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedField sGrow);
this->setWidget(scrollAreaContents);
}

//...

The subclasses then add rows:


somePropertyWidget = makeLineEdit();
addRow(tr("Some Property"), somePropertyWidget);

These widgets are then added to the tab widget:

void PropertiesWindow::createPages() {
//...
tabWidget->addTab(new SomePropertyPage());
//...
}

I have also tried different approaches, such as subclassing QWidget instead of QScrollArea and add a QScrollArea to the widget. The widget would have a layout (e.g. vertical layout) such that the scroll area would fill the entire widget.

I tried it in Designer, laying it out how it should look. As i could not set a QScrollArea as the tab widget (it automatically put a QWidget in), i did the second approach. But the generated code looked identical to mine.

I don't know what i am doing wrong.

wysota
9th February 2011, 21:10
Try setting the "widgetResizable" property of the scroll area to true.

xtal256
10th February 2011, 00:51
That was the only thing i did not try, as it defaults to false which is what i want.
Setting it to true seemed to work, that is - the widgets are laid out in the form layout and the scrollbar appears when needed. But there is a strange artefact in the top left which looks like a widget that does not have a layout.
http://img651.imageshack.us/img651/8563/forml.th.png (http://img651.imageshack.us/i/forml.png/)

stefanadelbert
11th February 2011, 03:44
As you say, it seems like a text edit that is a child of the QTabWidget (or at least a child of the "Window" page widget), but has not actually been added to the layout. Try calling findChildren (http://doc.qt.nokia.com/4.6.2/qobject.html#findChildren) on the QTabWidget and list the children to try and figure out where that line edit comes from.

xtal256
13th February 2011, 09:59
Hmm, now i can't even get any widgets to show. All i get is a small white square in the upper left corner and a background grey everywhere else. And i am using the same code as i posted earlier (i can't seem to edit it, but there are some errors in it). I forget what i did to get what i posted in the screenshot, but it was the closest i ever got to what i want.

xtal256
13th February 2011, 23:40
Sorry, i forgot, it was the setWidgetResizable(true) bit that got the layout to look like the image.
I will now try findChildren and see if i can find out what that lone widget is. And stefanadelbert is right, it is a line edit. I initially didn't think that because it didn't have the text cursor, but that is because these controls are read-only.

I think i can solve this issue now, the widget appears to be added incorrectly to the layout by my code. Shouldn't be too hard to figure out why.

The real question is why setWidgetResizable(true) was needed. Doesn't this force widgets to resize in favour of adding a scrollbar (what i want the opposite)?

wysota
14th February 2011, 00:06
The real question is why setWidgetResizable(true) was needed.
Because otherwise your widget has the size of the scrollarea (or a bit smaller actually) and never grows regardless of the fact that you keep pushing widgets into its layout (the scrollarea forbids the layout to resize the widget) and scrollbars never appear. You want widgetResizable to be true most of the time.