PDA

View Full Version : a tight QFrame is all i need



baray98
6th January 2008, 03:57
I was tryin to create my widget as follows



MyWidget::MyWidget(QString description, double value,
QString unit,QWidget* parent)
:QFrame(parent)
{
//ctor
QHBoxLayout* lo = new QHBoxLayout (this);
setLayout(lo);
setContentsMargins(1,1,1,1);

QLabel* lblDescription = new QLabel (description,this);
lo->addWidget(lblDescription);

QDoubleSpinBox* sbValue = new QDoubleSpinBox(this);
sbValue->setMaximum(MaxValue);
sbValue->setValue(value);
lo->addWidget(sbValue);

QLabel* lblUnit = new QLabel (unit,this);
lo->addWidget(lblUnit);
}


I want my frame to be just enough for line edit,and two labels (since they are laid out horizontally) no more no less, This will save me some screen space. I will be re using this widget a lot in one page (widget) and they will be laid out vertically.

Now, my question is that, is there any easyway to do the tight fitting of my frame to just the three widgets?. right now there is a gap between the 2 of my MyWidget laid out Vertically.

baray98

momesana
6th January 2008, 08:33
Try QWidget::setContentsMargins(int,int,int,int) and QLayout::setSpacing(int).

wysota
6th January 2008, 09:53
setContentsMargins() is not the way to go. Try QLayout::setMargin() and QLayout::setSpacing() instead. Contents margins are 0 by default, so setting them to 1 will only make an empty border around your widget.

baray98
6th January 2008, 20:44
void QLayout::setContentsMargins ( int left, int top, int right, int bottom ) works for me

thanks guys you rock!!!
baray98

wysota
6th January 2008, 21:14
Really? Can you post a screenshot?

jpn
7th January 2008, 07:53
I guess a negative content margin cuts the margin set by the layout. :) Of course, as Wysota already mentioned, the proper way would be to just remove the margin set by the layout.