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.
Q_OBJECT
protected:
QFormLayout* formLayout;
public:
AbstractPropertyPage
(QWidget* parent
= 0);
void addRow
(String labelText,
QWidget* widget
) { formLayout->addRow(labelText, widget);
}
//...
};
AbstractPropertyPage
::AbstractPropertyPage(QWidget* parent
) : scrollAreaContents
= new QWidget(this);
formLayout = new QFormLayout(scrollAreaContents);
formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
this->setWidget(scrollAreaContents);
}
//...
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::AllNonFixedFieldsGrow);
this->setWidget(scrollAreaContents);
}
//...
To copy to clipboard, switch view to plain text mode
The subclasses then add rows:
somePropertyWidget = makeLineEdit();
addRow(tr("Some Property"), somePropertyWidget);
somePropertyWidget = makeLineEdit();
addRow(tr("Some Property"), somePropertyWidget);
To copy to clipboard, switch view to plain text mode
These widgets are then added to the tab widget:
void PropertiesWindow::createPages() {
//...
tabWidget->addTab(new SomePropertyPage());
//...
}
void PropertiesWindow::createPages() {
//...
tabWidget->addTab(new SomePropertyPage());
//...
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks