dictoon
21st April 2011, 18:00
Hi,
Is there a simple and robust way to add margins around the child widget of a QScrollArea in Qt 4.7? Something like scrollarea->widget()->setContentsMargins(20) would have been nice but such a functionality seems to be absent in Qt.
Here's what I've tried: I created a second widget with a QGridLayout into it to act as a wrapper around the first widget. The QGridLayout has the desired margins set with QGridLayout::setContentsMargins(). I also set the size constraint on the layout to QLayout::SetFixedSize:
// Create a wrapping QWidget with margins.
QWidget* widget_wrapper = new QWidget();
widget_wrapper->setLayout(new QGridLayout());
widget_wrapper->layout()->setSizeConstraint(QLayout::SetFixedSize);
widget_wrapper->layout()->setContentsMargins(20, 20, 20, 20);
widget_wrapper->layout()->addWidget(widget);
// Create the scroll area and attach the wrapping QWidget to it.
QScrollArea* scroll_area = new QScrollArea();
scroll_area->setAlignment(Qt::AlignCenter);
scroll_area->setWidget(widget_wrapper);
This almost works, but not quite. One situation where it doesn't work properly is when the user wants to zoom on the inner widget: to make that happen, I resize the inner widget using QWidget::setFixedSize().
Can this approach work transparently? Am I using the right size constraint on the QGridLayout? Are there other, simpler approaches?
Thanks for all the input.
Cheers,
Franz
Added after 1 hour 55 minutes:
Problem solved. I needed to force a layout update (scroll_area->widget()->layout()->activate()) after having resized the inner widget in order to ensure that the scroll area child widget is resized as well.
Is there a simple and robust way to add margins around the child widget of a QScrollArea in Qt 4.7? Something like scrollarea->widget()->setContentsMargins(20) would have been nice but such a functionality seems to be absent in Qt.
Here's what I've tried: I created a second widget with a QGridLayout into it to act as a wrapper around the first widget. The QGridLayout has the desired margins set with QGridLayout::setContentsMargins(). I also set the size constraint on the layout to QLayout::SetFixedSize:
// Create a wrapping QWidget with margins.
QWidget* widget_wrapper = new QWidget();
widget_wrapper->setLayout(new QGridLayout());
widget_wrapper->layout()->setSizeConstraint(QLayout::SetFixedSize);
widget_wrapper->layout()->setContentsMargins(20, 20, 20, 20);
widget_wrapper->layout()->addWidget(widget);
// Create the scroll area and attach the wrapping QWidget to it.
QScrollArea* scroll_area = new QScrollArea();
scroll_area->setAlignment(Qt::AlignCenter);
scroll_area->setWidget(widget_wrapper);
This almost works, but not quite. One situation where it doesn't work properly is when the user wants to zoom on the inner widget: to make that happen, I resize the inner widget using QWidget::setFixedSize().
Can this approach work transparently? Am I using the right size constraint on the QGridLayout? Are there other, simpler approaches?
Thanks for all the input.
Cheers,
Franz
Added after 1 hour 55 minutes:
Problem solved. I needed to force a layout update (scroll_area->widget()->layout()->activate()) after having resized the inner widget in order to ensure that the scroll area child widget is resized as well.