QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QWidget* widget = new QWidget(parent); widget->setSizePolicy(sizePolicy);
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); sizePolicy.setHeightForWidth(true); QLabel* label = new QLabel(parent); label->setSizePolicy(sizePolicy);This example sets the QSizePolicy of a QLabel object to Minimum for both horizontal and vertical directions, meaning it will not try to expand to fill available space. Additionally, setHeightForWidth is set to true, meaning the widget will maintain its aspect ratio when resized. QSizePolicy is part of the Qt Core module.