PDA

View Full Version : Problems with layout



franco.amato
20th January 2010, 19:33
Hi I would layout some widget inside my class derived from QFrame.
This is the interface:


class PanelInfo : public QFrame
{
Q_OBJECT
public:
PanelInfo(QWidget* parent);
~PanelInfo();

public slots:
void panValueChanged(int);
void volValueChanged(int);

protected:
private:
QLabel* m_durationLabel;
QLabel* m_channelsLabel;
QLabel* m_freqLabel;
QLabel* m_bitsLabel;

QSlider* m_panSlider;
QSlider* m_volSlider;

QDoubleSpinBox* m_freqControl;
};

In the ctor I would layout the widget but the result is not the expected:

PanelInfo::PanelInfo(QWidget* parent /* = 0 */ )
: QFrame(parent)
{
setFrameStyle( QFrame::Box | QFrame::Raised );

setMinimumSize( QSize(120, 240) );
setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );

/* labels */
m_durationLabel = new QLabel("DURATION");
m_channelsLabel = new QLabel("CHANNELS");
m_freqLabel = new QLabel("FREQ");
m_bitsLabel = new QLabel("BITS");

QVBoxLayout* ll = new QVBoxLayout();
ll->setContentsMargins(0, 0, 0, 0);
ll->setSpacing(0); //<---doesn't work in my case
ll->addWidget( m_durationLabel );
ll->addWidget( m_channelsLabel );
ll->addWidget( m_freqLabel );
ll->addWidget( m_bitsLabel );

m_panSlider = new QSlider(Qt::Horizontal);
m_panSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_panSlider->setMinimum( -100 );
m_panSlider->setMaximum( +100 );
m_panSlider->setValue( 0 );

m_volSlider = new QSlider(Qt::Horizontal);
m_volSlider->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_volSlider->setMinimum( 0 );
m_volSlider->setMaximum( +100 );
m_volSlider->setValue( 100 );

/* spinbox */
m_freqControl = new QDoubleSpinBox();
m_freqControl->setMinimum( 0.0 );
m_freqControl->setMaximum( 3.0 );
m_freqControl->setValue( 1.0 );
m_freqControl->setSingleStep( 0.1 );
m_freqControl->setWrapping(1);

QVBoxLayout* vl = new QVBoxLayout();
vl->addLayout(ll);

vl->addWidget(m_panSlider);
vl->addWidget(m_volSlider);

vl->addWidget(m_freqControl);
setLayout(vl);

connect(m_panSlider, SIGNAL( valueChanged(int) ), this, SLOT( panValueChanged(int) ) );
connect(m_volSlider, SIGNAL( valueChanged(int) ), this, SLOT( volValueChanged(int) ) );
connect(m_freqControl, SIGNAL( valueChanged(double) ),this, SLOT( freqChanged( double ) ) );
}

The result is that I have some not desired spaces between the labels also setting setSpacing to 0px. Layout are not completely clear to me.
In the attached image I drawn some red lines indicating the spaces I would delete.

Best

Lykurg
20th January 2010, 20:44
add a spacer before you insert the sliders in the second layout: QBoxLayout::addSpacerItem(). You also can play with the size policy of the widgets. But using a spacer item is the best way for you, I guess.

faldzip
20th January 2010, 23:38
there are no spaces between labels in your code and image. But those labels got stretched to fill whole area in a layout.

franco.amato
21st January 2010, 01:21
there are no spaces between labels in your code and image. But those labels got stretched to fill whole area in a layout.

But I set setSpacing(0), what must I do?
Maybe it's better If I avoid layout

pezmaster31
21st January 2010, 05:38
Did you try Lykurg's suggestion: QBoxLayout::addSpacerItem() ?

If that didn't work for you, try adding a simple, empty QBoxLayout::addStretch() between adding your upper layout and slider widgets. This should work and will also keep you from hardcoding a "magic number" height and width for your spacer.

Lykurg
21st January 2010, 08:18
But I set setSpacing(0), what must I do?
Maybe it's better If I avoid layout

Maybe you want to read about layouts in general and the widgets size policies in the documentation. Your problem is so basic, and your above quoted question shows, that you have a huge lack of understanding the different between "spacing between widgets" and "the spacing (=padding) inside a wiget regarding its size policy".

franco.amato
21st January 2010, 16:42
Did you try Lykurg's suggestion: QBoxLayout::addSpacerItem() ?

If that didn't work for you, try adding a simple, empty QBoxLayout::addStretch() between adding your upper layout and slider widgets. This should work and will also keep you from hardcoding a "magic number" height and width for your spacer.

Yes but QBoxLayout::addSpacerItem() take an argument. Don't know what to pass.
QBoxLayout::addStretch(1) worked. I noticed that if I add addStretch(1 or 5 or 10 ) result doesn't change. Don't understand why
I'm learning better layout as I'm sure they are basics of Qt.