PDA

View Full Version : Question about QWidget::adjustSize()



Vladimir
30th August 2007, 12:56
Hello All,

I have a QFrame with several QLabels in it managed by QVBoxLayout. The QFrame itself is placed on top of QGraphicsView and its size is not managed by any layout. I want to be able to add/remove QLabels and adjust the size of the QFrame accordingly, and I've thought that adjustSize() is what I need. But after adding one more child QLabel and calling adjustSize() the size of the QFrame remains the same ! Moreover its sizeHint() and minimumSizeHint() remains the same too.

What is the correct way to implement size adjusting when adding more child widgets ? Thanks in advance !

jpn
30th August 2007, 13:14
Hello

The layout will automatically adjust size of the container widget to be large enough to contain all added widgets. Are you calling QBoxLayout::addWidget() or QWidget::addWidget()? Notice that the former is not a reimplementation of the latter but an overloaded function.

Actually you are only needed to call QWidget::adjustSize() if you want to make the container widget smaller after hiding its children. This step can also be made automatic by setting layout's size constraint to QLayout::SetFixedSize (http://doc.trolltech.com/latest/qlayout.html#SizeConstraint-enum).

Vladimir
30th August 2007, 13:33
Thanks, after setting layout's size constraint to QLayout::SetFixedSize everything works as expected.

But without it and when using adjustSize I'm getting strange sequence of sizes which you can see in the attached screenshots. The code which produces it:


if(!_messagesFrame && _worldView) {
_messagesFrame = new QFrame(_worldView);
_messagesFrame->setFrameShape(QFrame::StyledPanel);
_messagesFrame->setStyleSheet("border: 2px solid #8f8f91; border-radius: 6px; background-color: #e0e0e0; ");
_messagesFrame->move(15,15);
_messagesLayout = new QVBoxLayout(_messagesFrame);
_messagesLayout->setContentsMargins(2,0,2,0);
}

QWidget* widget = new QWidget(_messagesFrame);
QLabel* label = new QLabel(text, widget);

QHBoxLayout* layout = new QHBoxLayout(widget);
layout->setContentsMargins(0,0,0,0);
layout->addWidget(label);

_messagesLayout->addWidget(widget);
_messagesFrame->show();
_messagesFrame->adjustSize();