PDA

View Full Version : How to keep widget's size to one which is just sufficiennt to display contents?



yogeshm02
9th December 2006, 16:43
Hi

Here is the constructor for main widget:


GMessenger::GMessenger() : QWidget(0, Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint){
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
m_pLayout = new QVBoxLayout();
setLayout(m_pLayout);
}


Here is the constructor for child widget:


GMessenger::ProgressJobNotifier::ProgressJobNotifi er(const QString &title) : QFrame(0){
setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

...

setLayout(m_pVLayout);
}



QFrame derived child widget increases/decreases its size when a child widget is added to it, while QWidget derived top level widget increases its size when a child widget is added but not decreases its size when a child widget is removed (Which is what i want). So help please!

jacek
9th December 2006, 17:18
QWidget derived top level widget increases its size when a child widget is added but not decreases its size when a child widget is removed (Which is what i want).
Are you sure? Judging from the title of your post, you want the opposite.

QWidget::adjustSize()

wysota
9th December 2006, 19:13
You might be looking for QLayout::setSizeConstraint(QLayout::SetFixedSize).

yogeshm02
10th December 2006, 07:57
QLayout::setSizeConstraint(QLayout::SetFixedSize) is working :)
But, it seems like I'll have to invest more time understanding behaviour of QLayout::setSizeConstraint(QLayout::SetFixedSize) and setSizePolicy(QSizePolicy::Policy, QSizePolicy::Policy)

Bye

wysota
10th December 2006, 10:04
The difference is simple - sizepolicy constrains the size of a widget when that widget is in a layout (so it doesn't work for top level widgets) and setSizeConstraint controls the parent of the layout (or rather the layout itself).